Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

So, I re-installed OpenCV since the relevant ticket appeared to be solved. After the installation, I ran the below code (it is just the trimmed version of the original code, it does the same thing) and after approximately 10 seconds the program gave an output (stitched image).

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/stitching.hpp"
#include <iostream>

// a global container to hold the image parts in, which are gonna be stitched later
std::vector<cv::Mat> imageParts;


/*
    User defined function that imitates system("pause") behavior.
*/
void pause()
{
    do {
        std::cout << '\n' << "Press the Enter key to continue.";
    } while (std::cin.get() != '\n');

    return;
}


int main()
{
    // check if the images were passed (using opencv's String class)
    cv::String folderPath("imageParts/*.png"); //select only .png files
    std::vector<cv::String> filenames;

    cv::glob(folderPath, filenames, true); // a function that forms a valid path out of the above two

    for (size_t i = 0; i < filenames.size(); ++i)
    {
        // read the images
        cv::Mat img = cv::imread(filenames[i]);

        // give error if they are not found
        if (!img.data)
        {
            std::cerr << "Problem loading image. Path can be wrong." << std::endl;
            return -1;
        }

        // divide each into three sub images, so that the chances of success are higher
        cv::Rect rect(0, 0, img.cols / 2, img.rows);
        imageParts.push_back(img(rect).clone());
        rect.x = img.cols / 3;
        imageParts.push_back(img(rect).clone());
        rect.x = img.cols / 2;
        imageParts.push_back(img(rect).clone());
    }

    // conduct stitching
    cv::Mat stitchedImage;
    cv::Ptr<cv::Stitcher> stitcher = cv::Stitcher::create(cv::Stitcher::SCANS, false); //use gpu = false 
    cv::Stitcher::Status status = stitcher->stitch(imageParts, stitchedImage);

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

    // write it on the disk and inform the user
    cv::imwrite("stitchedImage.jpg", stitchedImage);
    std::cout << "stitching completed successfully\n" << "stitchedHDD.jpg" << " saved!";

    // hang the console
    pause();

    return 0;
}

It is important to use a function that hangs the console (like system("pause") kind of mechanism), because otherwise it closes it abruptly and you may think it did not stitch anything.

So, I hereby confirm that the issue has been solved by OpenCV.

So, I re-installed OpenCV since the relevant ticket appeared to be solved. After the installation, I ran the below code (it is just the trimmed version of the original code, it does the same thing) and after approximately 10 seconds the program gave an output (stitched image).

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/stitching.hpp"
#include <iostream>

// a global container to hold the image parts in, which are gonna be stitched later
std::vector<cv::Mat> imageParts;


/*
    User defined function that imitates system("pause") behavior.
*/
void pause()
{
    do {
        std::cout << '\n' << "Press the Enter key to continue.";
    } while (std::cin.get() != '\n');

    return;
}


int main()
{
    // check if the images were passed (using opencv's String class)
    cv::String folderPath("imageParts/*.png"); //select only .png files
    std::vector<cv::String> filenames;

    cv::glob(folderPath, filenames, true); // a function that forms a valid path out of the above two

    for (size_t i = 0; i < filenames.size(); ++i)
    {
        // read the images
        cv::Mat img = cv::imread(filenames[i]);

        // give error if they are not found
        if (!img.data)
        {
            std::cerr << "Problem loading image. Path can be wrong." << std::endl;
            return -1;
        }

        // divide each into three sub images, so that the chances of success are higher
        cv::Rect rect(0, 0, img.cols / 2, img.rows);
        imageParts.push_back(img(rect).clone());
        rect.x = img.cols / 3;
        imageParts.push_back(img(rect).clone());
        rect.x = img.cols / 2;
        imageParts.push_back(img(rect).clone());
    }

    // conduct stitching
    cv::Mat stitchedImage;
    cv::Ptr<cv::Stitcher> stitcher = cv::Stitcher::create(cv::Stitcher::SCANS, false); //use gpu = false 
    cv::Stitcher::Status status = stitcher->stitch(imageParts, stitchedImage);

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

    // write it on the disk and inform the user
    cv::imwrite("stitchedImage.jpg", stitchedImage);
    std::cout << "stitching completed successfully\n" << "stitchedHDD.jpg" << " saved!";

    // hang the console
    pause();

    return 0;
}

It is important to use a function that hangs the console (like system("pause") kind of mechanism), because otherwise it closes it abruptly and you may think it did not stitch anything.

Another important point is to not to use gpu, since it never worked for me, saying that CUDA has not implemented this functionality yet.

So, I hereby confirm that the issue has been solved by OpenCV.