Sunday, 27 March 2011

Wrapping and interfacing compiled code into more flexible and user-friendly programs using Zenity and Bash

Subtitles include:

1. How to reconstruct the three-dimensional shape of an object in a scene in a very inexpensive way using shadow-casting
2. How to write and execute a matlab script from a bash script

I've written a bash script - shadowscan.sh - which is a wrapper for a set of programs for the reconstruction 3D surface of an object in a series of photographs using the ingenious shadow-casting algorithm of Jean-Yves Bouguet and Pietro Perona

The script relies heavily on the GUI-style interfacing tools of Zenity and acts as an interface and wrapper for the algorithm as implemented by these guys. It will also write and execute a matlab script for visualising the outputs.

This script should live in a directory with the following compiled programs, all available here:
1. ccalib (performs the camera calibration to retrieve the intrinsic parameters of camera and desk)
2. find_light (performs the light calibration to find light-source coordinates)
3. desk_scan (does the 3d reconstruction)
4. merge (merges 2 3d reconstructions, e.g. from different angles)

and the following matlab script:
1. selectdata.m (available here)

When the makefile is run, it creates the compiled programs in the ./bin directory. It's important to consult the README in the main folder as well as within the 'desk_scan' folder to have a clear idea of what the program does and what the input parameters mean.

My program will check if the camera calibration and light calibration files exist in the folder and if not will carry them out accordingly then several reconstructions are carried out, and the outputs merged. Finally matlab is called from the command line to run 'p_lightscan_data.m' to visualize the results.

when the script finishes there will be the following output files:
1. xdim.txt - max dimension to scan to
2. params - camera calibration parameters
3. data - camera calibration data
4. light.txt - light calibration parameters
5. sample.pts - [x,y,z] of object, in coordinates relative to light source
6. sample_lightscan1.tif - filled colour contour plot of reconstructed object
7. sample_lightscan2.tif - mesh plot of reconstructed object

I've tried to keep it flexible. It will run only the elements it needs to. For example, if the camera or lighting has been carried out before for a different object it will pick up on that. Also, the program can be invoked by passing it two arguments (path to folder where sample images are, and what sequence of contrasts to use for the reconstruction), which avoids having to input with the graphical menus. Example usage:
1. bash shadowscan.sh
2. bash shadowscan.sh /home/daniel/Desktop/shadow_scanning/sample/bottle 30.35.40.45
defined()
{
[ ${!1-X} == ${!1-Y} ]
}
args=("$@") # get arguments passed to the program
# if no arguments, IMAGES and CNTRST will be empty otherwise will be filled with the arguments
defined args && IMAGES=${args[0]} # first = sample image location e.g. /home/daniel/Desktop/shadow_scanning/sample/
defined args && CNTRST=${args[1]} # second = contrst values - no more than four! e.g. 30.35.40.45
# if IMAGES empty, display flash info, and prompt user to direct program to folder
if [ -z $IMAGES ] ; then
zenity --info --title="SHADOWSCAN" --text "This program will create a 3D reconstruction of an object using images of the object scanned by a shadow. It is a bash/matlab wrapper program for code created by http://www.cs.columbia.edu/~pblaer/projects/photo/ which implements the shadow-casting algorithm of Jean-Yves Bouguet and Pietro Perona (http://www.vision.caltech.edu/bouguetj/ICCV98/)"
# folder where sample images are
IMAGES=$(zenity --file-selection --directory --title="Select directory where sample images are")
fi
if [ ! -f ./param ] # if param does not exist, do cam calib
then
# folder where checkerboard images are
CAL_CAM=$(zenity --file-selection --title="Select checkerboard image" --text "")
# get x-dimension size (number of pixels), subtract 20, and write to file
convert $CAL_CAM -format "%[fx: min(w,h)-20]" info: > xdim.txt
# input size of square in calibration photo # 23
SQ_HGHT=$(zenity --title="Camera Calibration" --text "input square size" --entry)
./ccalib $CAL_CAM $SQ_HGHT
fi
if [ ! -f ./light.txt ] # if light.txt does not exist, do light calib
then
# folder where light calibration images are
CAL_LIGHT=$(zenity --file-selection --directory --title="Select directory where light calibration images are")
# input height of the pencil in the light calibration photos # 76
OBJ_HGHT=$(zenity --title="Light Calibration" --text "input object height" --entry)
zenity --info --title="Instructions" --text "For each image, click first on the shadow tip, then on the base"
./find_light param $OBJ_HGHT $CAL_LIGHT/*.pgm > light.txt
fi
# this is the reference at the bottom of the image. This
# should be a row where at no time does the shadow pass
# over an object, it always should project onto the desk plane.
if [ ! -f ./xdim.txt ] ; then # if xdim.txt does not exist
BTM=$(zenity --title="reference value" --text "at the bottom of the image" --entry)
else
BTM=$(head xdim.txt)
fi
# this is the reference at the top of the image. This
# should be a row where at no time does the shadow pass
# over an object, it always should project onto the desk plane.
TOP=20
# if CNTRST empty, prompt user to select which CNTRST to use
if [ -z $CNTRST ] ; then
# user selects contrast levels (1 to 4) to be processed from a limited list
CNTRST=$(zenity --list --text "Select one or more contrast values to process" --title="" --checklist --column "Pick" --column "options" TRUE "30" TRUE "35" FALSE "40" FALSE "45" --separator=".")
fi
# $Var are contrast values, 0 is desk plane
if [ ${#CNTRST} -eq 2 ]; then # 1 contrast
Var1=`echo $CNTRST | awk -F\. '{print $1}'`
./desk_scan light.txt param $Var1 0 $TOP $BTM sample.pts $IMAGES/*.pgm
else
if [ ${#CNTRST} -eq 5 ] ; then # 2 contrasts
Var1=`echo $CNTRST | awk -F\. '{print $1}'`
Var2=`echo $CNTRST | awk -F\. '{print $2}'`
./desk_scan light.txt param $Var1 0 $TOP $BTM sample1.pts $IMAGES/*.pgm
./desk_scan light.txt param $Var2 0 $TOP $BTM sample2.pts $IMAGES/*.pgm
./merge sample1.pts sample2.pts sample.pts
rm sample1.pts sample2.pts
else
if [ ${#CNTRST} -eq 8 ] ; then # 3 contrasts
Var1=`echo $CNTRST | awk -F\. '{print $1}'`
Var2=`echo $CNTRST | awk -F\. '{print $2}'`
Var3=`echo $CNTRST | awk -F\. '{print $3}'`
./desk_scan light.txt param $Var1 0 $TOP $BTM sample1.pts $IMAGES/*.pgm
./desk_scan light.txt param $Var2 0 $TOP $BTM sample2.pts $IMAGES/*.pgm
./desk_scan light.txt param $Var3 0 $TOP $BTM sample3.pts $IMAGES/*.pgm
# merge the 3 data sets
./merge sample1.pts sample2.pts sample12.pts
./merge sample1.pts sample3.pts sample13.pts
./merge sample2.pts sample3.pts sample23.pts
./merge sample12.pts sample13.pts sample12_13.pts
./merge sample12_13.pts sample23.pts sample.pts
rm sample1.pts sample2.pts sample3.pts sample12.pts sample13.pts sample23.pts sample12_13.pts
else
if [ ${#CNTRST} -eq 11 ] ; then # 4 contrasts
Var1=`echo $CNTRST | awk -F\. '{print $1}'`
Var2=`echo $CNTRST | awk -F\. '{print $2}'`
Var3=`echo $CNTRST | awk -F\. '{print $3}'`
Var4=`echo $CNTRST | awk -F\. '{print $4}'`
./desk_scan light.txt param $Var1 0 $TOP $BTM sample1.pts $IMAGES/*.pgm
./desk_scan light.txt param $Var2 0 $TOP $BTM sample2.pts $IMAGES/*.pgm
./desk_scan light.txt param $Var3 0 $TOP $BTM sample3.pts $IMAGES/*.pgm
./desk_scan light.txt param $Var4 0 $TOP $BTM sample4.pts $IMAGES/*.pgm
# merge the 4 data sets
./merge sample1.pts sample2.pts sample12.pts
./merge sample3.pts sample4.pts sample34.pts
./merge sample12.pts sample34.pts sample.pts
rm sample1.pts sample2.pts sample3.pts sample4.pts sample12.pts sample34.pts
fi
fi
fi
fi
# once completed, in matlab run 'p_lightscan_data.m' to visualize the results
# http://ubuntuforums.org/showthread.php?t=825042
filename=p_lightscan_data.m;
cat > $filename << EOF
% p_lightscan_data
% example matlab code to clean up and plot data generated from shadowscan.sh
% Daniel Buscombe March 2011
close all;clear all;clc
pts=importdata('sample.pts');
plot(pts.data(:,1),pts.data(:,2),'k.')
view(2)
[pl,xs,ys] = selectdata('sel','rect');
close
x=pts.data(pl,1); y=pts.data(pl,2); z=pts.data(pl,3);
f=find(z==0); x(f)=[]; y(f)=[]; z(f)=[];
[xi,yi]=meshgrid([min(x):.1:max(x)],[min(y):.1:max(y)]);
zi=griddata(x,y,z,xi,yi);
figure
contourf(xi,yi,-zi-min(-zi(:))), colorbar
print -dtiff -r300 sample_lightscan1.tif
close
figure
mesh(xi,yi,-zi-min(-zi(:)))
print -dtiff -r300 sample_lightscan2.tif
close
EOF
chmod +x $filename
/opt/matlab/bin/matlab -nodesktop -nosplash -r "p_lightscan_data;quit;"
view raw shadowscan.sh hosted with ❤ by GitHub

Saturday, 26 March 2011

Program for spatial correlogram of an image, in C

This program will read an image, convert it to greyscale, crop it to a square, and calculate it's spatial (1D) autocorrelation function (correlogram), printing it to screen.

It uses OpenCV image processing libraries - see here and here

Compilation instructions (in Ubuntu):

 sudo apt-get install libcv-dev libcv1 libcvaux-dev libcvaux1 libhighgui-dev libhighgui1  
pkg-config --libs opencv; pkg-config --cflags opencv
PKG_CONFIG_PATH=/usr/lib/pkgconfig/:${PKG_CONFIG_PATH}
export PKG_CONFIG_PATH
gcc `pkg-config --cflags --libs opencv` -o Rspat Rspat.c



Usage in the terminal:

 ./Rspat IMG_0208.JPG   


The program:
#include <stdio.h>
#include "cv.h"
#include "highgui.h"
int main( int argc, char** argv )
{
time_t t1,t2; // initiate timer
(void) time(&amp;t1);
// define some variables
IplImage *im = 0;
/* check for supplied argument */
if ( argc <> \n" );
return 1;
}
// load image
im = cvLoadImage( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
CvSize sz = cvGetSize(im); // get image dimensions
int st=(sz.width/2)-(sz.height/2);
// sets the Region of Interest
cvSetImageROI(im, cvRect(st, 0, sz.height, sz.height));
// create destination image
IplImage *img1 = cvCreateImage(cvGetSize(im),
im->depth,
im->nChannels);
// copy subimage > crop
cvCopy(im, img1, NULL);
// reset ROI
cvResetImageROI(im);
int M = img1->width; //integer image dimensions
int N = img1->height;
int m; // initiate loop counters
int n;
CvScalar s1; // initiate temporary storage of scalars
CvScalar s2;
CvScalar img1_avg = cvAvg( img1, NULL ); // find average of img1
int l;
// initiate main loop
for( l=1; l<100 img2 =" cvCreateImage(cvGetSize(img1),">depth,
img1->nChannels);
// copy subimage > create shifted
cvCopy(img1, img2, NULL);
// reset ROI
cvResetImageROI(img1);
CvScalar img2_avg = cvAvg( img2, NULL ); // find average of img2
// reset counters
double sum_img1_img2 = 0;
double sum_img1_2 = 0;
double sum_img2_2 = 0;
for( m=0; m<M-l ; ++m ){
for( n=0; n<N-l; ++n ){
s1=cvGet2D(img1,m,n); // get the (m,n) pixel value of img1
s2=cvGet2D(img2,m,n); // get the (m,n) pixel value of img2
sum_img1_img2 = sum_img1_img2 + (s1.val[0]-img1_avg.val[0])*(s2.val[0]-img2_avg.val[0]);
sum_img1_2 = sum_img1_2 + (s1.val[0]-img1_avg.val[0])*(s1.val[0]-img1_avg.val[0]);
sum_img2_2 = sum_img2_2 + (s2.val[0]-img2_avg.val[0])*(s2.val[0]-img2_avg.val[0]);
}
}
double corr = sum_img1_img2/sqrt(sum_img1_2*sum_img2_2); // autocorrelation coefficient, lag l
printf("lag%i,corr=%f\n",l,corr);
(void) time(&t2);
return 0;
}
view raw Rspat.c hosted with ❤ by GitHub

Controlling a serial device with Python

This program to sends a command to serial device, receives data from the device, converts it to numeric format, and graphs the data in real time. It is written in Python using wxPython for the display.

The device is connected to /dev/ttyS0 with a baud rate of 19200, 8-bit data, no parity, and 1 stopbit. You need to edit the line 'serial.Serial' with the relevant parameters to use this code for your device. Editing 'DATA_LENGTH' will change the rate at which the graph scrolls forward. Also, you'd need to change the 'command' that the device understands as 'send me data' (here it is the s character - line 'input'). Knowledge of the format of the returned data is also essential (here I turn ascii into 2 integers) line 'struct.unpack' - see here for help). Otherwise this should work with any device. Tested on Ubuntu 9.10 and 10.4 using serial port and USB-serial converters.
import os
import pprint
import sys
import wx
# use wx with mpl is with the WXAgg backend
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigCanvas, NavigationToolbar2WxAgg as NavigationToolbar
import numpy as npy
import pylab
import serial
import time
import struct
# set connection:
ser = serial.Serial('/dev/ttyUSB0', 19200, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=1, timeout=0.1, xonxoff=1, rtscts=0)
ser.open() # open connection
ser.isOpen() # should be true
DATA_LENGTH = 120 #seconds
REDRAW_TIMER_MS = 60 # milliseconds
def getData():
input = 's'
# send 's' the character to the device
ser.write(input)
out = ''
# give device time to answer
time.sleep(0.02)
while ser.inWaiting() > 0:
out += ser.read(1) #request data (serial.read() only returns 8 bit ASCII strings)
#out += ser.read(ser.inWaiting())
if out != '':
# read in a length-2 string of 8-bit characters and then use the struct module to convert the strings into integers
response=struct.unpack(">bb", out)
# use only first integer in tuple (2nd is empty)
print response[0]
return response[0]
class GraphFrame(wx.Frame):
# the main frame of the application
def __init__(slf):
wx.Frame.__init__(slf, None, -1, "Sensor response", size=(800,600))
slf.Centre()
slf.data = []
slf.create_main_panel()
slf.redraw_timer = wx.Timer(slf)
slf.Bind(wx.EVT_TIMER, slf.on_redraw_timer, slf.redraw_timer)
slf.redraw_timer.Start(REDRAW_TIMER_MS)
def create_main_panel(slf):
slf.panel = wx.Panel(slf)
slf.init_plot()
slf.canvas = FigCanvas(slf.panel, -1, slf.fig)
slf.hbox1 = wx.BoxSizer(wx.HORIZONTAL)
slf.vbox = wx.BoxSizer(wx.VERTICAL)
slf.vbox.Add(slf.canvas, 1, flag=wx.LEFT | wx.TOP | wx.GROW)
slf.panel.SetSizer(slf.vbox)
def init_plot(slf):
slf.dpi = 100
slf.fig = Figure((3.0, 3.0), dpi=slf.dpi)
slf.axes = slf.fig.add_subplot(111)
slf.axes.set_axis_bgcolor('black')
slf.axes.set_title('My data', size=12)
slf.ymin = -50000 # initalise with large values
slf.ymax = 50000
pylab.setp(slf.axes.get_xticklabels(), fontsize=8)
pylab.setp(slf.axes.get_yticklabels(), fontsize=8)
# plot the data as a line series, and save the reference
# to the plotted line series
slf.plot_data = slf.axes.plot(
slf.data,
linewidth=2,
color="white",
)[0]
#count=count+1
#if count>120:
# slf.Destroy()
def draw_plot(slf):
# redraws the plot
xmax = len(slf.data) if len(slf.data) > DATA_LENGTH else DATA_LENGTH
xmin = xmax - DATA_LENGTH
ymin = slf.ymin # ymin and ymax set based on previous entries
ymax = slf.ymax #50000
slf.axes.set_xbound(lower=xmin, upper=xmax)
slf.axes.set_ybound(lower=ymin, upper=ymax)
pylab.setp(slf.axes.get_xticklabels(), visible=True)
slf.plot_data.set_xdata(npy.arange(len(slf.data)))
slf.plot_data.set_ydata(npy.array(slf.data))
slf.canvas.draw()
def on_redraw_timer(slf, event):
newData = getData()
slf.data.append(newData)
slf.draw_plot()
data=slf.data
if len(data)>=6:
# get last 5 values
datanew=[data[len(slf.data)-5],data[len(slf.data)-4],data[len(slf.data)-3],data[len(slf.data)-2],data[len(slf.data)-1]]
slf.ymin = (min(datanew))-(min(datanew)*10)/100 # min of last 5, minus 10%
slf.ymax = (max(datanew))+(max(datanew)*10)/100 # max of last 5, plus 10%
else:
slf.ymin = (min(data))-(min(data)*10)/100 # min minus 10%
slf.ymax = (max(data))+(max(data)*10)/100 # max plus 10%
def on_exit(slf, event):
slf.Destroy()
def flash_status_message(slf, msg, flash_len_ms=1500):
slf.statusbar.SetStatusText(msg)
slf.timeroff = wx.Timer(slf)
slf.Bind(
wx.EVT_TIMER,
slf.on_flash_status_off,
slf.timeroff)
slf.timeroff.Start(flash_len_ms, oneShot=True)
def on_flash_status_off(slf, event):
slf.statusbar.SetStatusText('')
if __name__ == '__main__':
app = wx.PySimpleApp()
app.frame = GraphFrame()
app.frame.Show()
app.MainLoop()

Sunday, 20 March 2011

Using cron to backup Thunderbird emails

My bash script (which I've saved as backup_emails.sh in my home folder and made executable) looks a little like this:

 set -x   
x=`date +%y%m%d.%H%M`
tar zcf thunderb-mail-${x}.tgz ~/.mozilla-thunderbird
mv thunderb-mail-${x}.tgz /media/MY_HARDDRIVE/email_backups/


Then invoke in cron using:

 crontab -e   


add something like:

 30 4 * * * ~/./backup_emails.sh  


which does the backup at 04.30 every day

Make Google Earth Faster on Ubuntu

In terminal type:

 export LIBGL_ALWAYS_INDIRECT=true