OpenCV, Qt, imread, namedWindow, imshow does not work

夙愿已清 提交于 2019-12-01 21:53:04

问题


In the .pro file:

QT       += core

QT       -= gui

TARGET    = latihan_2
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

INCLUDEPATH += E:\OpenCV\OpenCV\opencv\build\include

LIBS += E:\OpenCV\OpenCV\opencv\build\x86\mingw\lib\libopencv_core246.dll.a
LIBS += E:\OpenCV\OpenCV\opencv\build\x86\mingw\lib\libopencv_highgui246.dll.a
LIBS += E:\OpenCV\OpenCV\opencv\build\x86\mingw\lib\libopencv_imgproc246.dll.a
LIBS += E:\OpenCV\OpenCV\opencv\build\x86\mingw\lib\libopencv_features2d246.dll.a
LIBS += E:\OpenCV\OpenCV\opencv\build\x86\mingw\lib\libopencv_calib3d246.dll.a

In main.cpp:

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

using namespace cv;

int main(){
    //read image
    Mat image = imread("img.jpg", 1);
    //create image window named "My image"
    namedWindow("My Image", CV_WINDOW_AUTOSIZE);
    //show the image on window
    imshow("My image", image);
    //wait key for 5000ms
    waitKey(5000);
    return 1;
    }

When I hit run, there is no error, but it only shows a black window named qtcreator_process_stub.exe.

Why the "My image" window doesn't come out and shows the img.jpg? I use Qt creator 2.8.1, based on Qt 5.1.1, and openCV-2.4.6.0.


回答1:


You could also display a cv::Mat on a Qt window. I demonstrate how to do that on cvImage. The code below is adapted from cvImage::_open():

std::string filename = ...
cv::Mat mat = cv::imread(filename);

// Since OpenCV uses BGR order, we need to convert it to RGB
// NOTE: OpenCV 2.x uses CV_BGR2RGB, OpenCV 3.x uses cv::COLOR_BGR2RGB
cv::cvtColor(mat, mat, cv::COLOR_BGR2RGB) 

// image is created according to Mat dimensions
QImage image(mat.size().width, mat.size().height, QImage::Format_RGB888);

// Copy cv::Mat to QImage
memcpy(image.scanLine(0), mat.data, static_cast<size_t>(image.width() * image.height() * mat.channels()));

// Display the QImage in a QLabel
QLabel label;
label.setPixmap(QPixmap::fromImage(image));
label.show();



回答2:


First guess is that the image is in the wrong path, so first test should be to specify the full path to the image.

Also check the return value of your program (make sure the it doesn't return some crash error code - be consistent and return 0 for success and other values for fail).

And a little bit of coding that tells you where the code fails doesn't hurt, so check image.data or you can also use image.empty():

if(! image.data )
{
    std::cout << "No image to display";
    //can be any other method to display the error: qDebug, a messagebox... 
    //you can also 
    return 1;   
}
else
{
    //use the image
    //if nothing goes wrong:
    //return 0; 
}



回答3:


Check Projects->Run Settings ->Run in Terminal checkbox. If it is disabled, enable it.




回答4:


I faced same problem and I solved it by fixing path environment variable. In my path environment variable I added some opencv folders wrongly, then I deleted them and add only bin folder for opencv DLLs and then the problem was solved.



来源:https://stackoverflow.com/questions/20013903/opencv-qt-imread-namedwindow-imshow-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!