CSC 370

CSC 370 Assignment: Stereo

Due Tuesday, 4 December

For this assignment, you will try out stereo computations based upon the sum of squared differences (SSD) over a small window. We will use a mex-file (compiled function) called ssd_stereo, to perform the computation. You will experiment with various window sizes and disparity values.

Mex-Files

The lab computers should be set up already, but before you can try this lab at home you will need to set up the mex file. Copy ssd_stereo.cpp into your working directory. Take a look at it, and notice how it is structured. There is no main() function. Instead, when the code is called from Matlab execution begins with mexFunction(). This function checks the arguments passed to it to make sure they are appropriate, allocates space for an answer, and computes the stereo disparities using SSD. Along the way, it makes reference to a number of specially defined functions for interfacing with the Matlab environment, identified by the fact that they begin with either mx or mex. These functions are defined in the header file mex.h, included at the top of the file.

Before you can use the new procedure, you have to compile it using the Matlab command mex. The first step is to configure mex so it knwos what compiler to use. You can do this by typing mex -setup at the command prompt. It will search for all the compilers on your system. Matlab comes with a compiler called lcc, which you can use if you don't have any others installed. However, it isn't a very efficient compiler, so if you have either gcc or the Microsoft Visual C++ compiler, you should use one of those. Once you have set up the compiler, you can compile your file, by providing a full path name to it.

>> mex -setup
Please choose your compiler for building external interface (MEX) files: 
 
Would you like mex to locate installed compilers [y]/n? y
 
Select a compiler: 
[1] Digital Visual Fortran version 6.0 in C:\Program Files\Microsoft Visual Studio 
[2] Lcc C version 2.4 in C:\OTHER\MATLAB13\sys\lcc 
[3] Microsoft Visual C/C++ version 7.0 in C:\Program Files\Microsoft Visual Studio .NET 
[4] Microsoft Visual C/C++ version 6.0 in C:\Program Files\Microsoft Visual Studio 
 
[0] None 
 
Compiler: 3
 
Please verify your choices: 
 
Compiler: Microsoft Visual C/C++ 7.0 
Location: C:\Program Files\Microsoft Visual Studio .NET 
 
Are these correct?([y]/n): y
 
The default options file: 
"C:\Documents and Settings\nhowe\Application Data\MathWorks\MATLAB\R13\mexopts.bat" 
is being updated from C:\OTHER\MATLAB13\BIN\WIN32\mexopts\msvc70opts.bat... 
 
>> mex 'H:\Projects\Matlab\ssd_stereo.cpp'
>>

One final note: On Windows, mex-files compile into .mexw32 files. You can put a .m file of the same name into the same directory, containing comments about the function in question. The help and lookfor commands will reference this .m file, while the .mexw32 file will be used for execution.

Exercise

Once you have compiled ssd_stereo, you can use it as you would any Matlab function. It takes up to four arguments: the first two are the left and right stereo pair images, and must be grayscale. (Although color can give better stereo information, this implementation does not use it.) The third argument gives the window size as a two-element vector (horizontal and vertical square-radii, respectively). The fourth gives the minimum and maximum disparity values to consider. These may be negative, if so desired. Here's an example, showing the creation and viewing of a disparity map, assuming the grayscale left and right images are stored in img1 and img2.

>> disp = ssd_stereo(img1,img2,[1 1],[-12 0]);
>> imagesc(disp)
>> colorbar
>> axis image
>> 

Try the function on the two stereo pairs below. The disparity ranges in the images are between 0 and -12 pixels. (If we swapped the left and right images, the disparities would then range from 0 to 12.) Try different window sizes, and answer the following questions. Show the Matlab code for all sections, but you don't need to include images except where explicitly requested.

  1. What do the colors in the display mean?
  2. What window size gives the best results for each image? You will have to strike a balance between the smoothness of the result and how faithfully it follows the boundaries of objects in the image. Explain why you think the settings you chose are best, and include the results with your homework. You may use colormap gray to convert to grayscale for printing.
  3. Try a very small window size (e.g., [0 0]). What are the disadvantages of using such a small window?
  4. Try a very large window size (e.g., [40 40]). What are the disadvantages of using such a large window?
  5. Try allowing a greater range of disparities (e.g., [-20 +20]). What are the disadvantages of using this greater range? What percentage of the image's pixels are assigned a different disparity value as compared with the 0 to 12 result?

When you have finished the images above, find one or two stero pair images from the internet and try for the best result you can find. What parameter settings did you use?