安装好后opencv后执行下面这条语句的时候出错:
pkg-config --cflags opencv
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
经过网上详细查询资料后,是缺失了opencv.pc这个配置信息文件,故解决方法就是添加这个文件然后将其导入到环境变量中,具体操作如下:
首先创建opencv.pc文件,这里要注意它的路径信息:
cd /usr/local/lib
sudo mkdir pkgconfig
cd pkgconfig
sudo touch opencv.pc
然后在opencv.pc中添加以下信息,注意这些信息需要与自己安装opencv时的库路径对应:
prefix=/usr/local
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib
Name: opencv
Description: The opencv library
Version:4.0.1
Cflags: -I${includedir}/opencv4
Libs: -L${libdir} -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core
~
保存退出,然后将文件导入到环境变量:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
至此就配置好opencv.pc啦~
再执行 pkg-config --cflags --libs opencv时输出结果如下:
-I/usr/local/include/opencv4 -L/usr/local/lib \
-lopencv_shape -lopencv_stitching -lopencv_objdetect \
-lopencv_superres -lopencv_videostab -lopencv_calib3d \
-lopencv_features2d -lopencv_highgui -lopencv_videoio \
-lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml \
-lopencv_imgproc -lopencv_flann -lopencv_core
下面是opencv官方例程(opencv-4.0.1/samples/cpp/example_cmake)里的代码文件和Makefile文件:
example.cpp:
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
#include
using namespace cv;
using namespace std;
void drawText(Mat & image);
int main()
{
cout << "Built with OpenCV " << CV_VERSION << endl;
Mat image;
VideoCapture capture;
capture.open(0);
if(capture.isOpened())
{
cout << "Capture is opened" << endl;
for(;;)
{
capture >> image;
if(image.empty())
break;
drawText(image);
imshow("Sample", image);
if(waitKey(10) >= 0)
break;
}
}
else
{
cout << "No capture" << endl;
image = Mat::zeros(480, 640, CV_8UC1);
drawText(image);
imshow("Sample", image);
waitKey(0);
}
return 0;
}
void drawText(Mat & image)
{
putText(image, "Hello OpenCV",
Point(20, 50),
FONT_HERSHEY_COMPLEX, 1, // font face and scale
Scalar(255, 255, 255), // white
1, LINE_AA); // line thickness and type
}
Makefile:
CXX ?= g++
CXXFLAGS += -c -Wall $(shell pkg-config --cflags opencv)
LDFLAGS += $(shell pkg-config --libs --static opencv)
all: opencv_example
opencv_example: example.o; $(CXX) $< -o $@ $(LDFLAGS)
%.o: %.cpp; $(CXX) $< -o $@ $(CXXFLAGS)
clean: ; rm -f example.o opencv_example
我们在该例程下执行make来测试,编译过程如下:
g++ example.cpp -o example.o -c -Wall -I/usr/local/include/opencv4
g++ example.o -o opencv_example -L/usr/local/lib -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core
编译过程没有问题且生成了可执行文件opencv_example,执行起来没有问题,但是报了一点点小错误:
Gtk-Message: 06:57:54.502: Failed to load module "canberra-gtk-module"
这不是程序代码有问题,也不是编译问题,只需要安装一下 libcanberra-gtk-module即可:
sudo apt-get install libcanberra-gtk-module