Stitching Pipeline Forgets Data From Previous Images

asked 2020-05-12 02:47:19 -0500

updated 2020-11-15 01:07:27 -0500

I'm trying to stitch together images of a warehouse from bottom to top. I'm using the stitching pipeline in an iterative manner to do this; i.e., I add two images at a time to the stitch.

Everything seems to work fine for a few stitches, but eventually there comes a point where the pipeline seems to just discard most of what has been stitched up to that point, and kind of starts over. Here's a progression of the stitching results:

Stitch 1

Stitch 2

Stitch 3

Stitch 4

Stitch 5

Notice how the data in the first stitch is almost completely removed in the second stitch. Similarly, everything until the 4th stitch disappears in the 5th stitch.

Here is the exact code:

int main(int argc, char* argv[]) {
    int retval = parseCmdArgs(argc, argv);
    if (retval) return EXIT_FAILURE;

    vector<Mat> rotated_imgs;
    if(rotate_images) {
        for(int i=0; i<static_cast<int>(imgs.size()); i++) {
            Mat rotated_img;
            cv::rotate(imgs[i], rotated_img, rotate_flag);
            rotated_imgs.push_back(rotated_img);
        }

        imgs = rotated_imgs;
    }

    //![stitching]
    Mat pano;
    Ptr<Stitcher> stitcher = Stitcher::create(mode);
    Stitcher::Status status = stitcher->stitch(imgs, pano);

    if (status != Stitcher::OK) {
        cout << "Can't stitch images, error code = " << int(status) << endl;
        return EXIT_FAILURE;
    }
    //![stitching]


    if(rotate_images) {
        if(rotate_flag == cv::RotateFlags::ROTATE_90_CLOCKWISE) rotate_flag = cv::RotateFlags::ROTATE_90_COUNTERCLOCKWISE;
        else if(rotate_flag == cv::RotateFlags::ROTATE_90_COUNTERCLOCKWISE) rotate_flag = cv::RotateFlags::ROTATE_90_CLOCKWISE;        
        Mat rotated_pano;
        cv::rotate(pano, rotated_pano, rotate_flag);

        pano = rotated_pano;
    }

    imwrite(result_name, pano);
    cout << "stitching completed successfully\n" << result_name << " saved!";
    return EXIT_SUCCESS;
}

Note that I am rotating the images clockwise by 90 degrees prior to stitching (and rotating the result counter-clockwise by 90 degrees after stitching), since the Stitching pipeline works best when stitching together images left-to-right.

Also, I am running the Stitching pipeline in cv::Stitcher::SCANS mode.

The iterative procedure is captured in the Shell script I use to launch this code:

#!/usr/bin/bash
./stitching --mode scans  --rotate clock --output ./results/stitching_scans_rotate_0.png ./inputs-imgs/1.png ./inputs-imgs/2.png

for i in {1..35}; do
    ./stitching --mode scans  --rotate clock --output ./results/stitching_scans_rotate_$(($i)).png ./results/stitching_scans_rotate_$(($i-1)).png ./inputs-imgs/$((2*$i)).png ./inputs-imgs/$((2*$i + 1)).png
done

./stitching --mode scans  --rotate clock --output ./results/stitching_scans_rotate_36.png ./results/stitching_scans_rotate_35.png ./inputs-imgs/71.png ./inputs-imgs/72.png
enter code here

Any ideas why the pipeline is forgetting the images it stitched previously???

edit retag flag offensive close merge delete