Friday, September 2, 2016

Image Stitching with OpenCV 3.1.0

With OpenCV, stitching images to make a panorama is very easy. I will go over how to create a beautiful panorama image using OpenCV 3.1.0. Because there is a bug in OpenCV 3.1.0 Stitcher class when OpenCL is enabled, we will need to turn it off when building.

To build OpenCV 3.1.0 from source files, download it first from here or run
$ wget https://github.com/Itseez/opencv/archive/3.1.0.zip

Unzip it
$ unzip 3.1.0.zip
$ cd opencv-3.1.0

Create build folder
$ mkdir build && cd build

Create Makefile
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_EXAMPLES=ON -D CMAKE_BUILD_PREFIX=/usr/local/ -D WITH_OPENCL=OFF ..

If you encounter some ippicv hash error, try with ipp off:
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_EXAMPLES=ON -D CMAKE_BUILD_PREFIX=/usr/local/ -D WITH_OPENCL=OFF -D WITH_IPP=OFF ..

Next, let's build the library!

For Linux systems,
$ make -j `nproc`

For Mac OS X,
$ make -j `sysctl -n hw.ncpu`

After some time, the build should end successfully. Let's test stitch function out. It turns out that there is an example code in the source:
$ vim ../samples/cpp/stitching.cpp
$ vim ../samples/cpp/stitching_detailed.cpp

Actually, we just built this example file, and its executable is located in build/bin directory
$ cd bin/
$ ls cpp-example-stitching*

To test it, simply provide the image files to be stitched
$ ./cpp-example-stitching image1.jpg image2.jpg
$ ./cpp-example-stitching_detailed image1.jpg image2.jpg --features orb

You may want to download the files from here. If successful, the resultant panorama image files should have been produced
$ eog result.jpg

For Mac OS X, run
$ open -a Preview result.jpg

You may also want to run other OpenCV test and example files in the bin folder. To run tests, you will need to copy OpenCV extra data and set OPENCV_TEST_DATA_PATH variable:
$ git clone git://github.com/Itseez/opencv_extra.git
$ export OPENCV_TEST_DATA_PATH=`pwd`/opencv_extra/testdata

Now, you can run test files:
./opencv_test_stitching

Finally, to install it on the system, one needs to run
$ sudo make install

Enjoy your panorama image!

No comments:

Post a Comment