Sunday, September 25, 2011

OpenCV 2.3.1 and Microsoft Visual C++ 2010 Express

OpenCV is an open source Computer Vision software package. It includes some stereo calibration and matching functionality, and is therefore of great interest to anyone doing 3D.

I am gonna go through all the steps needed to install the 32 bit debug version of OpenCV 2.3.1 using the pre-compiled libraries and have it running on a Windows 7 64 bit machine with Microsoft Visual C++ 2010 Express under the hood.

Get the OpenCV 2.3.1 Windows Superpack and install it. I am gonna assume it's installed in C:\Program Files\opencv.

You're also gonna need the so-called TBB debug libraries because they are not included in OpenCV 2.3.1 (if you have already installed and setup OpenCV 2.3.1 and you're getting an error message about tbb_debug.dll missing when you start debugging your code, that's your problem). Get the windows TBB binaries and place the tbb30_20110427oss directory under C:\Program Files\opencv\build\common (for example). At some point in the future, OpenCV may come with the debug TBB and all the TBB stuff added here may not be needed anymore.

After having created a new Visual C++ 2010 project (make sure it's a Win32 Console Application), right-click on the name of the project and go into Properties.

Click on Configuration Properties->VC++ Directories, and edit the Include Directories. Add:
C:\Program Files\opencv\build\common\tbb30_20110427oss\include
C:\Program Files\opencv\build\include\opencv2
C:\Program Files\opencv\build\include\opencv
C:\Program Files\opencv\build\include

Click on Configuration Properties->VC++ Directories, and edit the Library Directories. Add:
C:\Program Files\opencv\build\common\tbb30_20110427oss\lib\ia32\vc10
C:\Program Files\opencv\build\x86\vc10\lib

Click on Configuration Properties->Linker->Input, and edit the Additional Dependencies. Add:
opencv_calib3d231d.lib
opencv_contrib231d.lib
opencv_core231d.lib
opencv_features2d231d.lib
opencv_flann231d.lib
opencv_gpu231d.lib
opencv_haartraining_engined.lib
opencv_highgui231d.lib
opencv_imgproc231d.lib
opencv_legacy231d.lib
opencv_ml231d.lib
opencv_objdetect231d.lib
opencv_ts231d.lib
opencv_video231d.lib
tbb_debug.lib
Here, I have put all the OpenCV libraries but you certainly don't need to do that, as you can just add the ones you need for your application.

At this point, you should be able to link your code. The only remaining issue is to let the computer know where the dll libraries are by adding the proper paths to the PATH environment variable.

Click on Configuration Properties->Debugging, and edit the Environment.
Type PATH=C:\Program Files\opencv\build\x86\vc10\bin;C:\Program Files\opencv\build\common\tbb30_20110427oss\bin\ia32\vc10 in the window.

You may also change the PATH outside of Visual Studio by going into the Control Panel->System and Security->System->Advanced System Settings->Environment Variables, and changing the PATH there.

Note about Visual C++ 2008 users: vc10 refers to Visual C++ 2010, vc9 refers to Visual C++ 2008. OpenCV 2.3.1 provides both builds.

Note about building a 64 bit project: Microsoft Visual C++ 2010 Express doesn't come with 64 bit support out of the box. You need to install Windows SDK (Software Development Kit) to have 64 bit support. After you have made Windows SDK the default in Properties->General->Platform Toolset, you should be able to go into the Configuration Manager and switch the project over to 64 bit. I haven't tried but there's no reason you shouldn't be able to link against the 64 bit OpenCV 2.3.1 libraries if you want to.

Friday, September 23, 2011

Fujifilm Finepix Real 3D W3 - Macro Stereo

If you look closely at the manual for the Fuji 3D W3 camera, it will tell you that that the focusing range is from 60 cm (2.0 ft.) to infinity. It will also tell you that the recommended range for taking 3D pictures is actually from 1.3 m (4.3 ft.) to infinity.

The Fuji 3D W3 has a 3D Macro mode that enables you to take pictures from 38 cm to 70 cm (1.2 ft. to 2.3 ft.) [wide angle] and from 1.1 m to 2.3 m (3.6 ft. to 7.5 ft.) [telephoto]. Doing macro stereo photography with a camera having a 75 mm lens separation (the Fuji 3D W3) is not a good idea. Why? It's because, unlike our eyes, the lenses do not converge toward the object to be photographed, and you end up with a left and right images that can not be fused by our eyes. But then, how do you do macro stereo (hypo stereo) with the Fuji 3D W3 camera? Well, you don't ... unless you have an optical device to reduce the lens separation (see Cyclopital3D) or a slide bar (more on this later) and a tripod.

You probably know that your Fuji 3D W3 camera has an Advanced 3D mode with one of the two options (Individual Shutter 3D) enabling you to take (with the left lens) the left shot and right shot (or the other way around) in succession, the stereo pair being saved as a MPO file. Now, if you also select Macro mode, you are gonna be able to take pictures of objects from 8 cm to 80 cm (0.3 ft. to 2.6 ft.) [wide angle] and from 60 cm to 3 m (2.0 ft. to 9.8 ft.) [telephoto]. What I am getting at is that if you use a slide bar to take the left and right photo in Advanced Individual Shutter 3D mode, you're gonna be able to do proper macro stereo photography.

The million dollar question is then: By how much should you slide the camera to take the right picture (after having taken the left one)? Well, all I can say with certainty is that Cyclopital3D uses a 25 mm lens separation in their macro close-up attachment. There's probably a magic formula that can give you the correct stereo base for any distance but I certainly don't have it.

Note that no matter what Fuji says, you should not take a picture of something that's too close. 10 to 12 inches is probably the closest you want to go, since it corresponds to the distance of distinct vision for most people.

Manfrotto makes some nice slide bars (used to be sold under the Bogen brand) but you could certainly make your own slide bar if you are of the DIY kind.

Of course, this slide bar business kinda defeats the purpose of having a stereo camera since all you need is a "mono" camera. On the plus side, having a slide bar also enables you to take intermediate frames between the left and right shots for proper lenticular imaging.