Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

A Problem with Stitch

Recently, I'm working with opencv Stitch class. I try to use some sample code from opencv's stitch class. I also try to use some code from internet. They all seem to be very easy to understand. But the weird thing is that all of them cannot be run correctly. My operating system is Windows 10, x64. My opencv version is 2.4.9. My IDE is Qt 5.5. Here's the code:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    cv::Mat im1 = cv::imread("C:\\Users\\bian\\Pictures\\Camera Roll\\WIN_20160607_17_04_45_Pro.jpg");
    cv::Mat im2 = cv::imread("C:\\Users\\bian\\Pictures\\Camera Roll\\WIN_20160607_17_05_08_Pro.jpg");
    cv::imshow("im1",im1);
    cv::imshow("im2",im2);
    cv::Mat im1_gray = grayscaleImg(im1);
    cv::Mat im2_gray = grayscaleImg(im2);
    cv::imshow("im1_gray",im1_gray);
    cv::imshow("im2_gray",im2_gray);
    std::vector<cv::Mat> imgs;
    imgs.push_back(im1_gray);
    imgs.push_back(im2_gray);
    cv::Mat pano;
    bool try_use_gpu = true;
    cv::Stitcher stitcher = cv::Stitcher::createDefault(try_use_gpu);
    cv::Stitcher::Status status = stitcher.stitch(imgs,pano);
    if (status != cv::Stitcher::OK)
    {
        std::cout << "Can't stitch images, error code = " << status << std::endl;
    }else{
        cv::imwrite("stitching image.jpg",pano);
        cv::imshow("拼接",pano);
        cv::waitKey(0);
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}
cv::Mat MainWindow::grayscaleImg(cv::Mat& src)
{
    cv::Mat dst;
    if(src.channels()>1){
        cv::cvtColor(src,dst,CV_BGR2GRAY);
        return dst;
    }else{
        return src;
    }
}

Here's the log:

SetProcessDpiAwareness failed: "COM error 0x80070005  (Unknown error 0x0ffffffff80070005)"
init done
opengl support available
Can't stitch images, error code = 1

Here's the code from opencv:

#include "mainwindow.h"
#include <QApplication>
#include <iostream>
#include <fstream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/stitching/stitcher.hpp"

using namespace std;
using namespace cv;

bool try_use_gpu = false;
vector<Mat> imgs;
string result_name = "result.jpg";

void printUsage();
int parseCmdArgs(int argc, char** argv);

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
//    MainWindow w;
//    w.show();
    int retval = parseCmdArgs(argc, argv);
    if (retval) return -1;

    Mat pano;
    Stitcher stitcher = Stitcher::createDefault(try_use_gpu);
    Stitcher::Status status = stitcher.stitch(imgs, pano);

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

    imwrite(result_name, pano);
    return a.exec();
}
void printUsage()
{
    cout <<
        "Rotation model images stitcher.\n\n"
        "stitching img1 img2 [...imgN]\n\n"
        "Flags:\n"
        "  --try_use_gpu (yes|no)\n"
        "      Try to use GPU. The default value is 'no'. All default values\n"
        "      are for CPU mode.\n"
        "  --output <result_img>\n"
        "      The default is 'result.jpg'.\n";
}


int parseCmdArgs(int argc, char** argv)
{
    if (argc == 1)
    {
        printUsage();
        return -1;
    }
    for (int i = 1; i < argc; ++i)
    {
        if (string(argv[i]) == "--help" || string(argv[i]) == "/?")
        {
            printUsage();
            return -1;
        }
        else if (string(argv[i]) == "--try_use_gpu")
        {
            if (string(argv[i + 1]) == "no")
                try_use_gpu = false;
            else if (string(argv[i + 1]) == "yes")
                try_use_gpu = true;
            else
            {
                cout << "Bad --try_use_gpu flag value\n";
                return -1;
            }
            i++;
        }
        else if (string(argv[i]) == "--output")
        {
            result_name = argv[i + 1];
            i++;
        }
        else
        {
            Mat img = imread(argv[i]);
            if (img.empty())
            {
                cout << "Can't read image '" << argv[i] << "'\n";
                return -1;
            }
            imgs.push_back(img);
        }
    }
    return 0;
}

Here's the log:

Rotation model images stitcher.

stitching img1 img2 [...imgN]

Flags:
  --try_use_gpu (yes|no)
      Try to use GPU. The default value is 'no'. All default values
      are for CPU mode.
  --output <result_img>
      The default is 'result.jpg'.
C:\Users\bian\Documents\build-untitled15-Desktop_Qt_5_5_0_MinGW_32bit-Debug\debug\untitled15.exe exited with code -1

Here's the project info of Qt:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = xxxxxxxxxxxxxx
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

INCLUDEPATH += C:\OpenCV\2.4.9\qt\include

LIBS += c:\opencv\2.4.9\qt\lib\libopencv_calib3d249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_contrib249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_core249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_features2d249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_flann249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_gpu249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_highgui249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_imgproc249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_legacy249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_ml249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_objdetect249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_video249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_stitching249.dll.a

I don't know what's the problem here. Is it because my system is 64 bit? Or any other problem.

A Problem with Stitch

Recently, I'm working with opencv Stitch class. I try to use some sample code from opencv's stitch class. I also try to use some code from internet. They all seem to be very easy to understand. But the weird thing is that all of them cannot be run correctly. My operating system is Windows 10, x64. My opencv version is 2.4.9. My IDE is Qt 5.5.

Here's the code:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    cv::Mat im1 = cv::imread("C:\\Users\\bian\\Pictures\\Camera Roll\\WIN_20160607_17_04_45_Pro.jpg");
    cv::Mat im2 = cv::imread("C:\\Users\\bian\\Pictures\\Camera Roll\\WIN_20160607_17_05_08_Pro.jpg");
    cv::imshow("im1",im1);
    cv::imshow("im2",im2);
    cv::Mat im1_gray = grayscaleImg(im1);
    cv::Mat im2_gray = grayscaleImg(im2);
    cv::imshow("im1_gray",im1_gray);
    cv::imshow("im2_gray",im2_gray);
    std::vector<cv::Mat> imgs;
    imgs.push_back(im1_gray);
    imgs.push_back(im2_gray);
    cv::Mat pano;
    bool try_use_gpu = true;
    cv::Stitcher stitcher = cv::Stitcher::createDefault(try_use_gpu);
    cv::Stitcher::Status status = stitcher.stitch(imgs,pano);
    if (status != cv::Stitcher::OK)
    {
        std::cout << "Can't stitch images, error code = " << status << std::endl;
    }else{
        cv::imwrite("stitching image.jpg",pano);
        cv::imshow("拼接",pano);
        cv::waitKey(0);
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}
cv::Mat MainWindow::grayscaleImg(cv::Mat& src)
{
    cv::Mat dst;
    if(src.channels()>1){
        cv::cvtColor(src,dst,CV_BGR2GRAY);
        return dst;
    }else{
        return src;
    }
}

Here's the log:

SetProcessDpiAwareness failed: "COM error 0x80070005  (Unknown error 0x0ffffffff80070005)"
init done
opengl support available
Can't stitch images, error code = 1

Here's the code from opencv:

#include "mainwindow.h"
#include <QApplication>
#include <iostream>
#include <fstream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/stitching/stitcher.hpp"

using namespace std;
using namespace cv;

bool try_use_gpu = false;
vector<Mat> imgs;
string result_name = "result.jpg";

void printUsage();
int parseCmdArgs(int argc, char** argv);

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
//    MainWindow w;
//    w.show();
    int retval = parseCmdArgs(argc, argv);
    if (retval) return -1;

    Mat pano;
    Stitcher stitcher = Stitcher::createDefault(try_use_gpu);
    Stitcher::Status status = stitcher.stitch(imgs, pano);

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

    imwrite(result_name, pano);
    return a.exec();
}
void printUsage()
{
    cout <<
        "Rotation model images stitcher.\n\n"
        "stitching img1 img2 [...imgN]\n\n"
        "Flags:\n"
        "  --try_use_gpu (yes|no)\n"
        "      Try to use GPU. The default value is 'no'. All default values\n"
        "      are for CPU mode.\n"
        "  --output <result_img>\n"
        "      The default is 'result.jpg'.\n";
}


int parseCmdArgs(int argc, char** argv)
{
    if (argc == 1)
    {
        printUsage();
        return -1;
    }
    for (int i = 1; i < argc; ++i)
    {
        if (string(argv[i]) == "--help" || string(argv[i]) == "/?")
        {
            printUsage();
            return -1;
        }
        else if (string(argv[i]) == "--try_use_gpu")
        {
            if (string(argv[i + 1]) == "no")
                try_use_gpu = false;
            else if (string(argv[i + 1]) == "yes")
                try_use_gpu = true;
            else
            {
                cout << "Bad --try_use_gpu flag value\n";
                return -1;
            }
            i++;
        }
        else if (string(argv[i]) == "--output")
        {
            result_name = argv[i + 1];
            i++;
        }
        else
        {
            Mat img = imread(argv[i]);
            if (img.empty())
            {
                cout << "Can't read image '" << argv[i] << "'\n";
                return -1;
            }
            imgs.push_back(img);
        }
    }
    return 0;
}

Here's the log:

Rotation model images stitcher.

stitching img1 img2 [...imgN]

Flags:
  --try_use_gpu (yes|no)
      Try to use GPU. The default value is 'no'. All default values
      are for CPU mode.
  --output <result_img>
      The default is 'result.jpg'.
C:\Users\bian\Documents\build-untitled15-Desktop_Qt_5_5_0_MinGW_32bit-Debug\debug\untitled15.exe exited with code -1

Here's the project info of Qt:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = xxxxxxxxxxxxxx
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

INCLUDEPATH += C:\OpenCV\2.4.9\qt\include

LIBS += c:\opencv\2.4.9\qt\lib\libopencv_calib3d249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_contrib249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_core249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_features2d249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_flann249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_gpu249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_highgui249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_imgproc249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_legacy249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_ml249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_objdetect249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_video249.dll.a\
  c:\opencv\2.4.9\qt\lib\libopencv_stitching249.dll.a

I don't know what's the problem here. Is it because my system is 64 bit? Or any other problem.

I tried with these sets of images, like: Set 1:

image description image description

Set 2:

image description image description

Set 3:

image description image description

While none of them has any good response. In my code, I just need to stitch two images together. So I still didn't get the problem here.