OpenCV Stitching example (Stitcher class, Panorama)

Image size of origin is 320*240. 

Processing time is 30.96 second took.

    The result of stitching

The result is pretty good. but, processing time is too much takes.

My computer spec is that.. (This is vmware system. The main system is mac book air 2013, i7 8bg)

The source code is very easy.
I think if we use stitching algorithm in realtime, we should be programing by GPU.

#include < stdio.h >  
#include < opencv2\opencv.hpp >  
#include < opencv2\stitching\stitcher.hpp >

#ifdef _DEBUG  
#pragma comment(lib, "opencv_core246d.lib")   
#pragma comment(lib, "opencv_imgproc246d.lib")   //MAT processing  
#pragma comment(lib, "opencv_highgui246d.lib")  
#pragma comment(lib, "opencv_stitching246d.lib");

#pragma comment(lib, "opencv_core246.lib")  
#pragma comment(lib, "opencv_imgproc246.lib")  
#pragma comment(lib, "opencv_highgui246.lib")  
#pragma comment(lib, "opencv_stitching246.lib");

using namespace cv;  
using namespace std;

void main()  
 vector< Mat > vImg;
 Mat rImg;

 vImg.push_back( imread("./stitching_img/S1.jpg") );
 vImg.push_back( imread("./stitching_img/S2.jpg") );
 vImg.push_back( imread("./stitching_img/S3.jpg") );
 vImg.push_back( imread("./stitching_img/S4.jpg") );
 vImg.push_back( imread("./stitching_img/S5.jpg") );
 vImg.push_back( imread("./stitching_img/S6.jpg") );

 Stitcher stitcher = Stitcher::createDefault();

 unsigned long AAtime=0, BBtime=0; //check processing time
 AAtime = getTickCount(); //check processing time

 Stitcher::Status status = stitcher.stitch(vImg, rImg);

 BBtime = getTickCount(); //check processing time 
 printf("%.2lf sec \n",  (BBtime - AAtime)/getTickFrequency() ); //check processing time

 if (Stitcher::OK == status) 
  imshow("Stitching Result",rImg);
  printf("Stitching fail.");




N image, realtime stitching.
source code:
how to work:

2 image stitching.
basic principal on vidoe(code and explanation):
basic principal on image(code and explanation):


  1. Saludos muy bueno el artículo. Faltaría la imagen 4.

  2. Anonymous2/2/14 20:22

    great blog mare! thank for your sharing

  3. Thank you very much~!! ^^

  4. The code work well when I stitched using only two of your images, but gave the following errors when used more than 2 image.

    First-chance exception at 0x5baa677a in TEST_CV.exe: 0xC0000005: Access violation reading location 0x00000004.

    First-chance exception at 0x756d9617 in TEST_CV.exe: Microsoft C++ exception: tbb::captured_exception at memory location 0x0012eaf4..

    Unhandled exception at 0x756d9617 in TEST_CV.exe: Microsoft C++ exception: tbb::captured_exception at memory location 0x0012eaf4..

    Please help me in fixing that.

  5. Can you send me the image files? I will test on my computer. Thank you.
    My email is feelmare@gmail.com

  6. Badrullami17/3/14 07:31

    It's great code.... but I have problem with image that captured with fisheye lens.. how to stitch that images? any suggestions?

  7. In my opinion, case of fisheye lens, you should do undistortion processing first.
    refer to this page -> http://www.kscottz.com/fish-eye-lens-dewarping-and-panorama-stiching/

  8. This comment has been removed by the author.

  9. it is great code ,,,, but can you help me how can i stitching video files together ???

  10. Real-time video stitching is little bit different with image.
    Big problem is proccesing time is late.
    In order to implementation, Structure must be sperate of online and offline calculation part.

    Please refer to the source code "stitching_detailed.cpp" of Opencv samples

    I made real-time stitching program but I am difficult to share the code.
    Because the source code is owned by my company.

    refer to this video.

    Thank you.

  11. This comment has been removed by the author.

  12. I have used the same code and it is running fine sometimes. But if I run it continuously more than two times, it is crashing.. giving the following error.
    Access violation reading location 0x0000000000000008. and it the cursor is pointing to
    pChore->m_pFunction(pChore); in Chores.cpp file.

    When I ran using 640x480 size images, the application is generating panorama for 2 images and crashing some times for the same set of images. Moreover it is not able to run for more than 2 images.

    Can you please let me know why it is happening like this?

    1. This comment has been removed by the author.

    2. hi , i faced same problem .... but when i using small images (320*240) for example , it will be correctly done .

  13. it is very informative but is it possible to have this details using python language : my email: mandeepola@ymail.com

  14. This comment has been removed by the author.

  15. Anonymous14/3/16 00:51

    Hi, I have some problems when using the stitcher, maybe you can help me. I guess I'll remit to you personally by mail with the image. Thank you in advance.

    1. I test your image
      and upload code to github

      Check output.jpg file
      and code

      Thank you.

    2. Anonymous15/3/16 00:36

      Thank you for your quick reply. It's strange your code is similar to mine and mine is not working properly. I´m using VS2013 as well, but OpenCV 3.1.0 x64 without Gpu, while you are using OpenCV 2.4.9 with Gpu. Do you know if the reason of my artifacts has to to with any of these three differences?

    3. I think default option is different.

    4. Anonymous15/3/16 04:18

      Default option looks similar (GraphCutSeamFinder(CostColor), BlocksGainCompensator and MultibandBlender after registration). Could it be due to any change in any of these blocks (probably multiband blender), that is messing up everything in the new version of OpenCV?

  16. Anonymous15/4/16 15:17

    Hi, There was a run-time error on my PC. It says, "Unhandled exception at 0x00871BDA (opencv_imgproc2412d.dll) in T3.exe: 0xC000001D: Illegal Instruction." I use VS2012 and opencv2.4.12. Could you please help me?

    1. I don't know about this error.
      Please check build option 32bit, 64bit.
      lib build condition. gpu include?
      function usage different in another opencv version?

    2. you can try in release type, it will be fine!

  17. Anonymous29/4/16 00:10

    i pasted this code in geany editor. compilation error ocured/home/aftab/Pictures/Screenshot from 2016-04-29 00:03:43.png

  18. Hi Mare, thanks for the code. I used your exact code on my machine and fed the same set of input images, but my resulting image wasn't complete like yours. It looks like it did some stitching but not completely. Any ideas what may be wrong? Thanks!

  19. Your code has 6 input images.
    But there are only 5 pictures.
    I tried running the program using your code.
    Pictures 5 and 6 do not appear in the execution window.

    And the input image was replaced with another image. But stitching failed.

    1. It is example of Stitcher in OpenCV library.
      It is very simple code, if you input overlap images.
      If a common feature can not be found in the image, stitching may fail.

  20. This comment has been removed by the author.

  21. Hi you know any example of stitching but with the opossite perspective.
    Something like this:


    My intention to carry out an inspection of labels in metal cans.

    Thanks in advance