简体   繁体   English

在Qt Creator Ubuntu 12.04中设置OpenCV

[英]Setting up OpenCV in Qt Creator Ubuntu 12.04

I'm trying to set up OpenCV in Qt Creator and I have some problems. 我正在尝试在Qt Creator中设置OpenCV,但遇到一些问题。 I added the OpenCV path in the Qt Creator .pro file 我在Qt Creator .pro文件中添加了OpenCV路径

INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib \
-lopencv_core \
-lopencv_imgproc \
-lopencv_highgui \
-lopencv_ml \
-lopencv_video \
-lopencv_features2d \
-lopencv_calib3d \
-lopencv_objdetect \
-lopencv_contrib \
-lopencv_legacy \
-lopencv_flann

And I want to read and show image in this code 我想阅读并显示此代码中的图像

void MainWindow::on_pushButton_clicked()
{
     cv::Mat matInput = cv::imread("LP.jpg");
     if( matInput.empty())
     {
          std::cout<<"Can't load image "<<std::endl;
     }
     cv::namedWindow("Show");
     cv::imshow("Show", matInput);
     cv::waitKey();
}

When I run my project it shows the following message: 当我运行我的项目时,它显示以下消息:

Starting /home/vasan/Qt/build-OpenCVWithQt-Desktop-Debug/OpenCVWithQt... 正在启动/ home / vasan / Qt / build-OpenCVWithQt-Desktop-Debug / OpenCVWithQt ...
The program has unexpectedly finished. 该程序意外完成。
/home/vasan/Qt/build-OpenCVWithQt-Desktop-Debug/OpenCVWithQt exited with code 0 / home / vasan / Qt / build-OpenCVWithQt-Desktop-Debug / OpenCVWithQt退出,代码为0

I have also been trying to set up OpenCV for Qt Creator for a few days now. 我几天来也一直在尝试为Qt Creator设置OpenCV。 I would recommend you trying the procedure found at 我建议您尝试以下步骤

https://code.google.com/p/qt-opencv-multithreaded/wiki/Documentation https://code.google.com/p/qt-opencv-multithreaded/wiki/Documentation

It is simple to follow, and it just WORKS. 它很容易遵循,而且很有效。 When you get to section 1.3, run any code sample you got at hand. 当您到达1.3节时,运行您手头的任何代码示例。 I used: 我用了:

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file
    image = imread("lena.png", CV_LOAD_IMAGE_COLOR);   // Read the file
    if(! image.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );                   // Show our image inside it.

    waitKey(0);                                          // Wait for a keystroke in the window
    return 0;
}

Hope that helps! 希望有帮助!

It worked for me: 它为我工作:

INCLUDEPATH += /usr/local/include/opencv2
LIBS += -L/usr/local/lib
LIBS += -lopencv_core
LIBS += -lopencv_imgproc
LIBS += -lopencv_highgui
LIBS += -lopencv_ml
LIBS += -lopencv_video
LIBS += -lopencv_features2d
LIBS += -lopencv_calib3d
LIBS += -lopencv_objdetect
LIBS += -lopencv_contrib
LIBS += -lopencv_legacy
LIBS += -lopencv_flann
LIBS += -lopencv_nonfree

Your code is correct. 您的代码是正确的。 I think the problem arise because you are using shadow building. 我认为出现问题是因为您正在使用阴影构建。 For this reason you should put the image in the shadow building folder (the folder that contains the executable) and not in the project folder (that contains only the code files). 出于这个原因,您应该将图像放置在shadow building文件夹(包含可执行文件的文件夹)中,而不是在项目文件夹(仅包含代码文件)中。

Another tip is to insert a return statement in the if case, so program will not quit when it does not find the image. 另一个技巧是在if情况下插入return语句,因此程序在找不到图像时不会退出。

 if( matInput.empty())
 {
      qDebug() << "Can't load image";
      return;
 }

Also, be sure to include the following headers 另外,请确保包含以下标题

#include <QDebug>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM