OpenCV中的图像处理 —— 图像阈值+图像平滑+形态转换(opencv进行图像处理)
681
2022-05-29
1.确保ubuntu能上网
2.安装cmake
代码: 全选
sudo apt-get install cmake-gui
3.下载OpenCV2.4.9 Linux版源码,不要用最新的3.0.0
http://opencv.org/downloads.html
4.解压opencv源码
代码: 全选
unzip opencv-2.4.9.zip
5.创建一个build目录用于编译和一个output目录用于存放编译完成后的海思平台的opencv:
代码: 全选
xlab@xlab-dev:~/zhouhua/opencv/opencv-2.4.9$ ls
3rdparty LICENSE apps data include modules samples
CMakeLists.txt README.md cmake doc index.rst platforms
xlab@xlab-dev:~/zhouhua/opencv/opencv-2.4.9$ cd ..
xlab@xlab-dev:~/zhouhua/opencv$ ls
build opencv-2.4.9 opencv-2.4.9.zip output
xlab@xlab-dev:~/zhouhua/opencv$ mkdir build
xlab@xlab-dev:~/zhouhua/opencv$ mkdir output
6.执行cmake-gui
代码: 全选
xlab@xlab-dev:~/zhouhua/opencv/opencv-2.4.9$ cmake-gui
点击Browse Source选择~/zhouhua/opencv/opencv-2.4.9
点击Browse Build选择~/zhouhua/opencv/build
然后点击Configure
此时出现的对话框选择最后一项:Specify options for cross-compiling
下一步
Operating System填写arm-hisiv100nptl-linux
C填写arm-hisiv100nptl-linux-gcc
C++填写arm-hisiv100nptl-linux-g++
下一步,然后等待Configuration done
然后在出现的列表中修改CMAKE_INSTALL_PREFIX为~/zhouhua/opencv/output
然后点击Generate
等待Generation done
即可关闭cmake软件。
7.进入build目录执行make
代码: 全选
xlab@xlab-dev:~/zhouhua/opencv/build$ make
提示出错:
代码: 全选
../../lib/libopencv_core.so: undefined reference to `pthread_once'
../../lib/libopencv_core.so: undefined reference to `pthread_spin_lock'
../../lib/libopencv_core.so: undefined reference to `pthread_spin_unlock'
../../lib/libopencv_core.so: undefined reference to `pthread_spin_init'
../../lib/libopencv_core.so: undefined reference to `pthread_spin_trylock'
../../lib/libopencv_core.so: undefined reference to `pthread_spin_destroy'
修改CMakeCache.txt大约200行处
//Flags used by the linker.
CMAKE_EXE_LINKER_FLAGS:STRING= -lpthread -lrt
继续make
可能出现如下错误
代码: 全选
CMake Error at /home/xlab/zhouhua/opencv/opencv-2.4.9/cmake/cl2cpp.cmake:50 (string):
string does not recognize sub-command MD5
make[2]: *** [modules/ocl/opencl_kernels.cpp] Error 1
make[1]: *** [modules/ocl/CMakeFiles/opencv_ocl.dir/all] Error 2
make: *** [all] Error 2
删除/home/xlab/zhouhua/opencv/opencv-2.4.9/cmake/cl2cpp.cmake的第50行的内容即可。
继续make
完成后执行make install
代码: 全选
xlab@xlab-dev:~/zhouhua/opencv/output$ ls
LICENSE bin include lib share
8.得到了include和lib目录就可以编写程序了,来试试最常用的车辆检测吧
编写如下代码
代码: 全选
[cpp] view plain copy
#include"cv.h"
#include"highgui.h"
#include"stdio.h"
/******************fortime mesurement*************************/
#include
structtimeval tpstart,tpend;
unsigned longtimeuses;
voidtimeRec()
{
gettimeofday(&tpstart,0);
}
int timeRep()
{
gettimeofday(&tpend,0);
timeuses=(tpend.tv_sec-tpstart.tv_sec)*1000000+tpend.tv_usec-tpstart.tv_usec;
printf("use time:%uus\n",timeuses);
return timeuses;
}
/********************end**************************************/
int main(intargc, char* argv[])
{
IplImage* img= NULL;
CvMemStorage* storage =cvCreateMemStorage(0);
CvHaarClassifierCascade*cascade = cvLoadHaarClassifierCascade("./model.xml",cvSize(24,24));
//CvHaarClassifierCascade* cascade =(CvHaarClassifierCascade*)cvLoad("./lbpcascade_frontalface.xml", 0,0, 0);
CvSeq* faces;
//加载图像
img = cvLoadImage(argv[1], 0);
printf("img w=%d h=%d\n",img->width, img->height);
//检测并计时
timeRec();
faces = cvHaarDetectObjects(img,cascade, storage, 1.1, 3, 0,cvSize(24,24) );
timeRep();
if (faces->total == 0){
printf("no face!\n");
}
printf("car= %d\n", faces->total);
//释放内存
cvReleaseImage(&img);
printf("car detected! car.jpg!\n");
}
编译成功,然后拷贝车辆分类器文件过来。
再找个图片过来,我这里就用car.jpg了。
将/root/jiang/OpenCV/output/lib下的libopencv_imgproc.so、libopencv_objdetect.so、libopencv_highgui.so和libopencv_core.so复制到u盘,将u盘中这4个动态库做软连接库到/lib目录下。
代码: 全选
ln -s /mnt/udisk/libopencv_imgproc.so /lib/libopencv_imgproc.so
ln -s /mnt/udisk/libopencv_objdetect.so /lib/libopencv_objdetect.so
ln -s /mnt/udisk/libopencv_highgui.so /lib/libopencv_highgui.so
ln -s /mnt/udisk/libopencv_core.so /lib/libopencv_core.so
备注:
删除软链接:
rm -rf /lib/libopencv_core.so 注意不是rm -rf /lib/libopencv_core.so/
然后到car所在的/mnt/udisk目录去执行即可:
# ./car car.jpg
img w=686h=398
use time:18323188us
car = 5
cardetected! in car.jpg!
OpenCV 交通智能体
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。