I have a simple OpenCV program found on the web. I am trying to compile it in Qt creator. The source code is the following (main.cpp):
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, COLOR_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.0, 1.0);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
Here's my .pro file:
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
CONFIG += link_pkgconfig
PKGCONFIG += opencv
SOURCES += main.cpp
INCLUDEPATH += /usr/local/include/opencv2
LIBS += -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_highgui -lopencv_shape -lopencv_videoio
When I build the project, I get the following linker error (plus other similar ones):
error: undefined reference to `cv::VideoCapture::VideoCapture(int)'
However, when using a simple CMake file, it builds and runs perfectly. My CMakeLists.txt is pretty simple:
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage main.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
Are there some other paths I need to set up?
First of all, check that you have installed all the dependencies of OpenCV. Probably you will need to install FFmpeg
too.
Then replace:
LIBS += -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_highgui -lopencv_shape -lopencv_videoio
By this:
LIBS += -L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs -lopencv_shape -lopencv_videoio
You also need to add the specific OpenCV configuration:
CONFIG += opencv
Or alternatively:
unix: CONFIG += link_pkgconfig
unix: PKGCONFIG += opencv
It works for me with those small changes.
I m also working on Qt with Opencv and I added opencv into Qt using these lines to .pro
file:
INCLUDEPATH += /usr/local/include/opencv
LIBS += `pkg-config --cflags --libs opencv`
Also you should check the opencv installation via on terminal:
pkg-config --modversion opencv
If it gives error then something wrong with the installation.
Edit: If you installed opencv4.xx, you should change the word "opencv"
on commands above with the "opencv4"
Problem solved.
The reason this happened to me is that first I did
sudo apt install libopencv-highgui-dev
and
sudo apt install libopencv-dev
and then compiled OpenCV 2.4.13 from sources and installed it (because I needed exactly this version).
With cmake, it linked to OpenCV 4.1.0, but with Qt Creator it linked to OpenCV 2.4.13. The solution was to uninstall OpenCV and install version 2.4.13 only.
try this:
CONFIG += opencv
INCLUDEPATH += /usr/local/include/opencv4
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.