在.pro
文件中:
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
在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;
}
当我点击运行时,没有错误,但它只显示一个名为qtcreator_process_stub.exe
的黑色窗口。
为什么“我的图像”窗口没有显示出来并显示img.jpg? 我使用基于Qt 5.1.1的Qt creator 2.8.1和openCV-2.4.6.0。
答案 0 :(得分:3)
您还可以在Qt窗口上显示cv::Mat
。我演示了如何在cvImage上执行此操作。检查cvImage::_open()
:
void cvImage::_open()
{
// Display dialog so the user can select a file
QString filename = QFileDialog::getOpenFileName(this,
tr("Open Image"),
QDir::currentPath(),
tr("Files (*.png *.jpg *.tiff *.bmp)"));
if (filename.isEmpty()) // Do nothing if filename is empty
return;
cv::Mat img = cv::imread(filename.toStdString());
if (img.empty())
return;
// Since OpenCV uses BGR order, we need to convert it to RGB
cv::cvtColor(img, img, CV_BGR2RGB);
// _image is created according to Mat dimensions
if (_image)
{
delete _image;
}
_image = new QImage(img.size().width, img.size().height, QImage::Format_RGB888);
// Copy cv::Mat to QImage
memcpy(_image->scanLine(0), (unsigned char*)img.data, _image->width() * _image->height() * img.channels());
// Set the filename as the window title
setWindowTitle(filename);
// Resize the window to fit video dimensions
resize(img.size().width, img.size().height);
// Mouse move events will occur only when a mouse button is pressed down,
// unless mouse tracking has been enabled:
QWidget::setMouseTracking(true);
// Trigger paint event to redraw the window
update();
}
答案 1 :(得分:1)
首先猜测图像是在错误的路径上,因此首先应该测试图像的完整路径。
同时检查程序的返回值(确保它不会返回一些崩溃错误代码 - 保持一致并返回0表示成功,其他值表示失败)。
一些编码告诉你代码失败的地方并没有什么坏处,所以检查image.data或者你也可以使用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;
}
答案 2 :(得分:0)
检查项目 - &gt;运行设置 - &gt;在终端中运行复选框。如果已禁用,请启用它。
答案 3 :(得分:0)
我遇到了同样的问题,并通过修复路径环境变量解决了它。在我的路径环境变量中,我错误地添加了一些opencv文件夹,然后删除了它们,仅添加了opencv DLL的bin文件夹,然后问题得以解决。