这两天 公司有个需求 需要将原来的python写的图像检测功能改成C++版本
之前只是听过opencv 没怎么实际用过,这两天经过一番百度 摸索 终于搭建好了环境
由于之前嵌入式的开发经验,习惯于将这些第三方的库什么的编译成静态库 即.a文件
opencv4 比较折腾,原以为很简单,实际开发过程中 还是很折腾的 ,乘这机会,重新梳理下流程:
Download essential packages:
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
If you need more functions: install following packages:
sudo apt-get install python-dev python-numpy python3-dev python3-numpy
sudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
OpenCV offical site: OpenCV(4.5.5)
下载完之后 解压进入主目录新建build文件夹 新建build.sh 内容如下:(这里是安装到系统环境中了,后面会有另一种编译成静态库的时候是不一样的 这里注意 PREFIX目录会后一定要 空格 然后.. 不然会报错)GTK那个是可以和GUI相关的功能,不需要的话可以不编译,我后面的静态编译就关闭了 不然会报很多找不到GTK函数的错误
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D WITH_GTK=ON \
-D WITH_GTK_2_X=ON \
-D CMAKE_INSTALL_PREFIX=/usr/local ..
保存 添加可执行权限 执行脚本./build.sh
make -j4
sudo make install
这里之后头文件会安装到/usr/local/include/opencv4 此目录下有个opencv2文件夹
由于很多头文件引用找不到这个opencv4 所以openvcv2拷出来放到/usr/local/include目录下
Then create a new file /etc/ld.so.conf.d/opencv.conf content is:
/usr/local/lib
Then execute : sudo ldconfig
Test install write or not:
pkg-config --libs opencv4 和 pkg-config --cflags opencv4
新建一个test.cpp 内容如下
#include
#include
#include "opencv2/imgproc.hpp"
using namespace cv;
using namespace std;
int show_img()
{
Mat img;
img = cv::imread("mth.jpg");
if(img.data == NULL)
{
printf("Image not exist\n");
return -1;
}
cv::namedWindow("edges", cv::WINDOW_AUTOSIZE);
cv::imshow("Display image",img);
waitKey(0);
return 0;
}
int main(int argc, char** argv)
{
show_img();
return 0;
}
New a build.sh file content is :
g++ -std=c++11 ./test.cpp -o test `pkg-config --cflags --libs opencv4`
我的提示了没有GTK 需要安装一些列依赖包
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
If ok execture ./test it will display a image file like this:
这里新建另一个build_staitc.sh脚本 内容如下:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D BUILD_SHARED_LIBS=OFF \
-D BUILD_JPEG=ON \
-D BUILD_PNG=ON \
-D BUILD_TIFF=ON \
-D WITH_GTK=OFF \
-D WITH_GTK_2_X=OFF \
-D CMAKE_INSTALL_PREFIX=/home/jason/Downloads/opencv-4.5.5/ubuntu ..
中间有很多坑 做如下配置即可成功编译 引用
直接复制我的编译配置,./build_static.sh
make -j4 sudo make install
之后可以发现在ubuntu目录下已经生成了个lib库include头文件等需要的东西 如下图:
然后写一个makefile 内容如下 :(ubuntu名字后来被我改成opencv4了 所以下面的目录名字都变了)
CC = g++
EXEC = main
OBJS = test.o
CUR_PATH = $(PWD)
FLAGS = -std=c++11 -O2 -W -Wall
FLAGS += -I../opencv4/include/opencv4
LDFLAGS = -L../opencv4/lib/libopencv_calib3d.a
LDFLAGS += ../opencv4/lib/libopencv_core.a
LDFLAGS += ../opencv4/lib/libopencv_dnn.a
LDFLAGS += ../opencv4/lib/libopencv_core.a
LDFLAGS += ../opencv4/lib/libopencv_features2d.a
LDFLAGS += ../opencv4/lib/libopencv_flann.a
LDFLAGS += ../opencv4/lib/libopencv_gapi.a
LDFLAGS += ../opencv4/lib/libopencv_highgui.a
LDFLAGS += ../opencv4/lib/libopencv_imgcodecs.a
LDFLAGS += ../opencv4/lib/libopencv_imgproc.a
LDFLAGS += ../opencv4/lib/libopencv_ml.a
LDFLAGS += ../opencv4/lib/libopencv_objdetect.a
LDFLAGS += ../opencv4/lib/libopencv_photo.a
LDFLAGS += ../opencv4/lib/libopencv_stitching.a
LDFLAGS += ../opencv4/lib/libopencv_video.a
LDFLAGS += ../opencv4/lib/libopencv_videoio.a
LDFLAGS += ../opencv4/lib/opencv4/3rdparty/libade.a
LDFLAGS += ../opencv4/lib/opencv4/3rdparty/libIlmImf.a
LDFLAGS += ../opencv4/lib/opencv4/3rdparty/libippicv.a
LDFLAGS += ../opencv4/lib/opencv4/3rdparty/libippiw.a
LDFLAGS += ../opencv4/lib/opencv4/3rdparty/libittnotify.a
LDFLAGS += ../opencv4/lib/opencv4/3rdparty/liblibopenjp2.a
LDFLAGS += ../opencv4/lib/opencv4/3rdparty/liblibprotobuf.a
LDFLAGS += ../opencv4/lib/opencv4/3rdparty/liblibtiff.a
LDFLAGS += ../opencv4/lib/opencv4/3rdparty/liblibwebp.a
LDFLAGS += ../opencv4/lib/opencv4/3rdparty/libquirc.a
LDFLAGS += ../opencv4/lib/opencv4/3rdparty/liblibjpeg-turbo.a
LDFLAGS += ../opencv4/lib/opencv4/3rdparty/liblibpng.a
LDFLAGS += -lz -ldl -lm -lpthread -lrt
TARGET = main
SMP_SRCS = test.cpp
OBJS := $(SMP_SRCS:%.c=%.o)
CFLAGS += $(FLAGS)
MPI_LIBS = $(LDFLAGS)
.PHONY : clean all
all: $(TARGET)
$(TARGET):$(OBJS)
@$(CC) $(CFLAGS) -o $@ $^ -Wl,--start-group $(MPI_LIBS) \
-Wl,--end-group,-gc-sections,-g
clean:
#@rm -f $(TARGET_PATH)/$(TARGET)
@rm -f $(OBJS)
@rm -f $(COMM_OBJ)
直接make 就可以了
附上一段检测图片中蓝色部分的代码 静态库去掉GTK 所以 imshow就不能用了 只有命令行,如果是带GTK功能的话 会显示出图片 如下图 (但是最终我们只需要检测结果不需要显示):
int detect_blue( int argc, char** argv )
{
Mat srcImage;
if (argc > 1)
{
srcImage = imread(argv[1]);
}
else
{
srcImage = imread("blue.webp");
}
if (!srcImage.data)
{
cout << "No data"< 0.4)
{
cout<<"Blue image detected"<
今天先写到这
TensorFlow git 地址