1.打开“QtCreator”,新建一个非Qt工程(Non-Qt Project):opencv_test;
2.将“/home/davidhopper/opencv/opencv-3.3.0/samples/data”目录(/home/davidhopper/是我存放opencv源代码的目录,读者需要根据实际情况加以修改)中的“HappyFish.jpg”复制到“opencv_test”目录;
3.在“opencv_test.pro”文件中加入如下语句:
QMAKE_LIBDIR += /usr/local/lib
LIBS += -lopencv_core \
-lopencv_highgui \
-lopencv_imgcodecs \
-lopencv_imgproc
4.“main.cpp”文件内容如下:
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
String imageName( "./HappyFish.jpg" ); // by default
if( argc > 1)
{
imageName = argv[1];
}
Mat image;
image = imread( imageName, IMREAD_COLOR ); // Read the file
if( image.empty() ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", 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;
}
5.点击左侧工具栏上的“项目->Build & Run->Desktop Qt 5.9...->Run”,在右侧的“运行设置”页中找到“Command line arguments”文本框,输入:“./HappyFish.jpg”;
6.按Ctrl+B构建项目,按F5调试程序。
7.说明:该测试程序的编译命令为:
g++ main.cpp -o main -g -std=c++11 -Wall -W -fPIC -I/usr/local/include/ -L/usr/local/lib/ -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc