Showing posts with label OpenCV. Show all posts
Showing posts with label OpenCV. Show all posts

Friday, 4 October 2013

Converting 16-bit images to 8-bit images in OpenCV

Here is a simple example to convert 16-bit images to 8-bit images in OpenCV (for a single pixel). Note that the conversion is lossy.
// the original depth image is 16-bit (IPL_DEPTH_16U)
IplImage *dimg_original = cvLoadImage( filename, CV_LOAD_IMAGE_UNCHANGED );
CvScalar s = cvGet2D(dimg_original, 0, 0);
printf("16-bit depth value is %d, ",(int)s.val[0]);

// convert the original 16-bit depth image into 8-bit
IplImage *dimg = cvCreateImage(cvGetSize(dimg_original), IPL_DEPTH_8U,dimg_original->nChannels);
cvConvertScale(dimg_original, dimg, 1./40); //40 is the scaling factor to map 16-bit into 8-bit
CvScalar s1 = cvGet2D(dimg, 0, 0);
printf("8-bit depth value is %d\n",(int)s1.val[0]);
A few notes:
1. index in cvGet2D() and cvSet2D() starts with zero, and is in column-major order, i.e., cvGet2D(img, y, x)
2. If the scaling factor is greater than 1./256 (note that 65536 / 256 = 256, in the above example we set the factor to 1./40), pixels with larger values will all be mapped to 255 (no difference any more for larger values).
3. This method can also be used to convert 32-bit images to 8-bit images.

Thursday, 3 October 2013

Upgrading/compiling OpenCV on Linux from source code and use with Eclipse

In order to upgrade your OpenCV to the latest version (versions on APT are usually obsolete), or if you modified the source code in OpenCV and need to recompile it, you can follow the following steps as an example:

1. Download the latest source tarball from OpenCV’s website.

2. Extract the .tar.gz file to a directory, say ~/opencv_src. This is just a temporary directory for the source code.

3. Create another directory for build files, say ~/opencv_build. Change to this directory (do not stay in the source folder). Make the build files using CMAKE (you may change the options where necessary):
cmake -D CMAKE_BUILD_TYPE=RELEASE -D WITH_TBB=YES -D TBB_INCLUDE_DIRS=/usr/include/tbb -D CMAKE_INSTALL_PREFIX=/usr -D BUILD_PYTHON_SUPPORT=ON -D WITH_OPENNI=YES ../opencv_src
Note: You could refer to CMakeLists.txt in ~/opencv_src for more options (for example, to install OpenNI support you need to set WITH_OPENNI to YES). Another important thing is to check OpenCV website for required APT packages. Install those packages using apt-get. Otherwise you will get some error information when trying to run the above command.

4. Should the command is executed successfully, you will have the build files. Note that we have set CMAKE_INSTALL_PREFIX to /usr. This is by default /usr/local but if we need to replace the old versions that comes with APT, we need to set the prefix to /usr. This is where apt-get installs programs. For /usr/local, it is usually for self-maintained packages. So actually it is up to you to use /usr or /usr/local.

5. Type “make” and “sudo make install” to install the files to the desired location. If we set prefix to /usr then files will be copied to locations like /usr/lib, /usr/include/opencv. The make command will take a little while.

You should have installed OpenCV now. Following steps are for using OpenCV with Eclipse CDT.

6. Open Eclipse (with CDT installed), create a new C++ project. And then click on Menu -> Project -> Properties -> C/C++ Build -> Settings -> Tool Settings. We need to change:

(1) GCC C++ Compiler -> Includes. Add “/usr/include/opencv”

(2) GCC C++ Linker -> Library search path. Add “/usr/lib”

(3) GCC C++ Linker -> Libraries. Add “opencv_core” and “opencv_highgui” (for a complete list of libraries available, check the libopencv_***.so files located at /usr/lib. When adding their names, simply remove "lib" at front and any extensions. For example, use "opencv_core" for "libopencv_core.so").

7. To give it a go, use the following sample program:
#include <highgui.h>

int main( int argc, char** argv )
{

 IplImage* img = cvLoadImage( "/path/to/an/image.jpg" ); // change this to a valid filename
 cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
 cvShowImage( "Example1", img );
 cvWaitKey(0);
 cvReleaseImage( &img );
 cvDestroyWindow( "Example1" );

}