我正在尝试在Qt Creator中设置OpenCV,但我遇到了一些问题。我在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
我想阅读并在此代码中显示图片
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();
}
当我运行我的项目时,它会显示以下消息:
启动/ home / vasan / Qt / build-OpenCVWithQt-Desktop-Debug / OpenCVWithQt ...
该计划意外完成 / home / vasan / Qt / build-OpenCVWithQt-Desktop-Debug / OpenCVWithQt退出代码为0
答案 0 :(得分:3)
我也一直试图为Qt Creator设置OpenCV几天了。我建议你尝试
中的程序https://code.google.com/p/qt-opencv-multithreaded/wiki/Documentation
它很容易遵循,它只是工作。当你到达1.3节时,运行你手头的任何代码示例。我用过:
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;
}
希望有所帮助!
答案 1 :(得分:2)
它对我有用:
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
答案 2 :(得分:0)
您的代码是正确的。我认为问题出现是因为你正在使用阴影构建。因此,您应该将图像放在阴影构建文件夹(包含可执行文件的文件夹)中,而不是放在项目文件夹中(仅包含代码文件)。
另一个提示是在 if 的情况下插入 return 语句,这样程序在找不到图像时就不会退出。
if( matInput.empty())
{
qDebug() << "Can't load image";
return;
}
另外,请务必添加以下标题
#include <QDebug>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>