Repository: opencv
Issue: Stitcher generate result file 158 bytes long without error
Labels: bug,auto-transferred,priority: normal,affected: 2.4,category: stitching

Transferred from http://code.opencv.org/issues/3122

```
|| Nikolay Rogoshchenkov on 2013-06-28 21:22
|| Priority: Normal
|| Affected: branch 'master' (2.4.9)
|| Category: stitching
|| Tracker: Bug
|| Difficulty: 
|| PR: 
|| Platform: x64 / Windows
```
## Stitcher generate result file 158 bytes long without error

```
*Subj!*

Command line to reproduce: 
<pre>
openCV_stitch_three.exe  b1.bmp b2.bmp b3.bmp
</pre>

Example code:
<pre>
int main(int argc, char* argv[])
{
    int retval = parseCmdArgs(argc, argv);
    if (retval) return -1;

    Mat pano;

    printf("Channels image1: %d\n", imgs[0].type());
    printf("Channels image2: %d\n", imgs[1].type());

    int width = 170;

    cv::Rect rect1 = cvRect( imgs[0].cols-width, 0, width, imgs[0].rows); //second half of the first image
    cv::vector<cv::Rect> roi1;
    roi1.push_back(rect1);

    cv::Rect rect12 =  cvRect(0, 0,width, imgs[1].rows);  //first half of the second image
    cv::Rect rect23 = cvRect(imgs[1].cols -width, 0, width, imgs[1].rows);  //second half of the first image
    cv::vector<cv::Rect> roi2;
    roi2.push_back(rect12);
    roi2.push_back(rect23);

    cv::Rect rect3 = cvRect(0, 0, width, imgs[2].rows); //first half of the third image
    cv::vector<cv::Rect> roi3;
    roi3.push_back(rect3);

    cv::vector<cv::vector<cv::Rect>> rois;
    rois.resize(3);
    rois[0] = roi1;
    rois[1] = roi2;
    rois[2] = roi3;
    //cv::imshow("debug_img1", imgs[0]);
    Mat tmp1, tmp2, tmp3;
    cvtColor(imgs[0], tmp1, CV_BGR2GRAY); 
    cvtColor(imgs[1], tmp2, CV_BGR2GRAY); 
    cvtColor(imgs[2], tmp3, CV_BGR2GRAY); 
    /*lensCorrectorBarrel(tmp1.data, tmp1.cols, tmp1.rows);
    lensCorrectorBarrel(tmp2.data, tmp2.cols, tmp2.rows);
    lensCorrectorBarrel(tmp3.data, tmp3.cols, tmp3.rows);
    imwrite("corr1.tif", tmp1);
    imwrite("corr2.tif", tmp2);
    imwrite("corr3.tif", tmp3);*/
    /*namedWindow( "debug_img_corr1", CV_WINDOW_AUTOSIZE );
    namedWindow( "debug_img_corr2", CV_WINDOW_AUTOSIZE );
    cv::imshow("debug_img_corr1", tmp1);
    cv::imshow("debug_img_corr2", tmp2);*/

    cvtColor(tmp1, imgs[0], CV_GRAY2BGR); 
    cvtColor(tmp2, imgs[1], CV_GRAY2BGR); 
    cvtColor(tmp3, imgs[2], CV_GRAY2BGR); 

    //char cc = cvWaitKey(0);

    /*cv::rectangle( imgs[0], rect1, CV_RGB(255,0,0), 1);
    cv::rectangle( imgs[1], rect12, CV_RGB(255,0,0), 1);
    cv::rectangle( imgs[1], rect23, CV_RGB(255,0,0), 1);
    cv::rectangle( imgs[2], rect3, CV_RGB(255,0,0), 1);
    cv::imshow("debug_img1", imgs[0]);
    cv::imshow("debug_img2", imgs[1]);
    cv::imshow("debug_img3", imgs[2]);
    char c = cvWaitKey(0);*/

    Stitcher stitcher = Stitcher::createDefault(false);
    stitcher.setPanoConfidenceThresh(0.07);
    stitcher.setWaveCorrection(false);

    //detail::BestOf2NearestMatcher *matcher = new detail::BestOf2NearestMatcher(false, 0.001/*=match_conf*/);
    //detail::SurfFeaturesFinder *featureFinder = new detail::SurfFeaturesFinder(100);
    //stitcher.setFeaturesMatcher(matcher);
    //stitcher.setFeaturesFinder(featureFinder);
    Stitcher::Status status = stitcher.stitch(imgs, rois, pano);
    //Stitcher::Status status = stitcher.composePanorama(imgs, pano);

    if (status != Stitcher::OK)
    {
        cout << "Can't stitch images, error code = " << status << endl;
        return -1;
    }

    imwrite(result_name, pano);

    char ac = cvWaitKey(0);
    if(ac == 'ESC')
        return 0;
}
</pre>

Thank you.
```
## History
##### Nikolay Rogoshchenkov on 2013-06-28 21:27

```
How can I attach additional files? With good images, for example.
```
##### Alexey Spizhevoy on 2013-07-01 06:27

```
There is the 'Files' section available when one is composing a new message.
```
##### Nikolay Rogoshchenkov on 2013-07-01 12:12

```
Since archieve with good images > 5MB, you can download it here: http://nikkadim.com/good_images.tar.gz ~6.9MB
Thank you.
```
##### Alexey Spizhevoy on 2013-07-02 08:32

```
The code works fine when the images are passed in the correct order. Otherwise the application generates trash (as expected), but sometimes even without error message -- which isn't OK. I will add an error notification.
```
##### Nikolay Rogoshchenkov on 2013-07-04 22:25

```
Alexey Spizhevoy wrote:
> The code works fine when the images are passed in the correct order. Otherwise the application generates trash (as expected), but sometimes even without error message -- which isn't OK. I will add an error notification.

So, you wanna say, that's order in my example is wrong?
```
##### Alexey Spizhevoy on 2013-07-05 07:52

```
If I pass "a1.bmp a2.bmp a3.bmp" (you haven't attached any b*.bmp) then your code works, in some other cases it fails.
-   Affected version changed from 2.4.5 (latest release) to branch
    'master' (2.4.9)
```
##### Nikolay Rogoshchenkov on 2013-07-05 19:19

```
This problem.tar.gz archive contains:
<pre>
- openCV_stitch_three.exe
- b1.bmp
- b2.bmp
- b3.bmp
- result.tif
</pre>

Command line to reproduce: 
<pre>
openCV_stitch_three.exe  b1.bmp b2.bmp b3.bmp
</pre>

In this case we have result.tif with size 158 bytes (header).

Thank you.
-   File problem.tar.gz added
```
##### Alexey Spizhevoy on 2013-07-09 08:21

```
I've checked: "openCV_stitch_three.exe b1.bmp b2.bmp b3.bmp" failed, but "openCV_stitch_three.exe b1.bmp b3.bmp b2.bmp" generated good panorama.
```
##### Nikolay Rogoshchenkov on 2013-07-11 02:37

```
Alexey Spizhevoy wrote:
> I've checked: "openCV_stitch_three.exe b1.bmp b2.bmp b3.bmp" failed, but "openCV_stitch_three.exe b1.bmp b3.bmp b2.bmp" generated good panorama.

Failed - you mean you've got error? Or 158 bytes result file?
But problem is the right sequence is b1.bmp b2.bmp b3.bmp, this images from 3 camera in one row.
```
##### Nikita Manovich on 2013-07-11 07:56

```
-   Status changed from New to Open
```
##### Alexey Spizhevoy on 2013-07-11 07:59

```
> Failed - you mean you've got error? Or 158 bytes result file?
158 bytes result.

> But problem is the right sequence is b1.bmp b2.bmp b3.bmp, this images from 3 camera in one row.
Maybe, but it turns out that b1,b3,b2 are matched better, than b1,b2,b3. The images' texture is very specific.
```
##### Nikolay Rogoshchenkov on 2013-07-11 14:41

```
Alexey Spizhevoy wrote:
> > Failed - you mean you've got error? Or 158 bytes result file?
> 158 bytes result.
> 
> > But problem is the right sequence is b1.bmp b2.bmp b3.bmp, this images from 3 camera in one row.
> Maybe, but it turns out that b1,b3,b2 are matched better, than b1,b2,b3. The images' texture is very specific.

Ok, is it normal to receive 158 byte file without error from stitching module? Probably not.
```
##### Alexey Spizhevoy on 2013-07-12 08:57

```
I've already said before that silent stitching failure is not OK. The "pull request":https://github.com/Itseez/opencv/pull/1079 is waiting to be merged.
```
##### Nikolay Rogoshchenkov on 2013-07-12 11:39

```
Thank you! Sorry, I did not notice that.
This update will be available only in 2.4.9, right?
```
##### Nikolay Rogoshchenkov on 2013-07-12 13:47

```
Alexey, i think the problem with 158 bytes size file is more deeper.

I've compiled  OpenCV246 with CUDA and TBB support, and wrote a simple program (see attachment) for stitching 6 images. 
But each time when I start this program I've got different results:
!http://nikkadim.com/stitch_six.png!
Each time when stitching tooks >10 secs I had result image size 158 bytes, than I restart program with the same input image data I had a good results (with calculation time ~5 sec).
-   File main.cpp added
```
##### Nikolay Rogoshchenkov on 2013-07-12 13:48

```
input files available here: http://nikkadim.com/input_imges.zip ~5.5Mb
```
##### Alexey Spizhevoy on 2013-07-12 14:27

```
The fix will be available in the next major release. What version the master branch will become isn't determined yet. But the issue with error message isn't critical anyway, however if you need the fix you can build OpenCV from the master branch when the pull request is merged.

As for the new info, do you have the same problem without CUDA support?
```
##### Nikolay Rogoshchenkov on 2013-07-13 21:19

```
Alexey Spizhevoy wrote:
> As for the new info, do you have the same problem without CUDA support?
Thank you, Alexey!
I did not try, but anyway, I did not use CUDA instructions in program, you can see it. Should I anyway try to compile OpenCV without CUDA?
```
##### Alexey Spizhevoy on 2013-07-15 12:09

```
I couldn't reproduce the problem. But I guess the behavior you observed was due to the FLANN matcher (used in BestOf2NearestMatcher) and it's non-deterministic actually.
```
##### Nikolay Rogoshchenkov on 2013-07-15 22:01

```
Alexey Spizhevoy wrote:
> I couldn't reproduce the problem. But I guess the behavior you observed was due to the FLANN matcher (used in BestOf2NearestMatcher) and it's non-deterministic actually.
If needed I can share the dll's, exe and input files to reproduce the issue. 

Also I have noticed, that this issue also reproduced on my desktop PC (Intel Core i7 950/24GB DDR3/Win7 x64). I can reproduce this with 32-bit and 64-bit version of program.
How can I also help to catch the root of case? May try to play with BestOf2NearestMatcher params?

Thank you, Alexey.
```
##### Nikolay Rogoshchenkov on 2013-10-30 20:00

```
any updates?
```
##### Nikolay Rogoshchenkov on 2015-04-07 16:19

```
Same with 2.4.11 for both x86 and x64 arch's.
Resulting pano has size = (1,1), but calculation time about right
```