安装Opencv之后,首先要写一份代码测试一下是否ok。
本文代码图像边沿检测的:
#include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> using namespace cv; int main( int, char** argv ) { Mat src,gray,Kernelx,Kernely; src = imread( argv[1] ); cvtColor( src, gray, CV_RGB2GRAY ); namedWindow("srcImage", 1); namedWindow("dstImage", 1); Kernelx = (Mat_<double>(3,3) << 1, 1, 1, 0, 0, 0, -1, -1, -1); Kernely = (Mat_<double>(3,3) << -1, 0, 1, -1, 0, 1, -1, 0, 1); Mat grad_x, grad_y; Mat abs_grad_x, abs_grad_y, grad; filter2D(gray, grad_x, CV_16S , Kernelx, Point(-1,-1)); filter2D(gray, grad_y, CV_16S , Kernely, Point(-1,-1)); convertScaleAbs( grad_x, abs_grad_x ); convertScaleAbs( grad_y, abs_grad_y ); addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad ); imshow("dstImage", grad); waitKey(); return 0; }
使用编译命令:
g++ -o sample sample.cpp `pkg-config --cflags --libs opencv`
测试运行结果:
./sample lena_hui.jpg
问题解决:
1 报错: error while loading shared libraries: libopencv_core.so.3.2: cannot open shared object file: No such file or directory
解决方案: export LD_LIBRARY_PATH=$LD_LIBARARY_PATH:/usr/local/lib 加入到bashrc中。