[Ubuntu 16.04]OpenCV-3.4.2测试¶
编译生成OpenCV C++
库和Python
库后进行代码测试
C++¶
参考:Using OpenCV with gcc and CMake
使用2
种方式测试C++
库
- 编写
makefile
文件配置 - 编写
CMakeLists.txt
文件配置
编写测试文件DisplayImage.cpp
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
if ( argc != 2 )
{
printf("usage: DisplayImage.out <Image_Path>\n");
return -1;
}
Mat image;
image = imread( argv[1], 1 );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
编写makefile文件配置¶
新建makefile
INCLUDE=$(shell pkg-config --cflags opencv)
LIB=$(shell pkg-config --libs opencv)
SOURCE=DisplayImage.cpp
RES=DisplayImage
$(RES):$(SOURCE)
g++ $(SOURCE) $(INCLUDE) $(LIB) -o $(RES)
clean:
rm $(RES)
编译、链接生成可执行文件
$ ls
DisplayImage.cpp lena.jpg makefile
$ make
g++ DisplayImage.cpp -I/home/zj/opencv/opencv/install/include/opencv -I/home/zj/opencv/opencv/install/include -L/home/zj/opencv/opencv/install/lib -lopencv_ml -lopencv_shape -lopencv_objdetect -lopencv_stitching -lopencv_superres -lopencv_dnn -lopencv_videostab -lopencv_video -lopencv_photo -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_flann -lopencv_videoio -lopencv_imgcodecs -lopencv_imgproc -lopencv_core -o DisplayImage
$ ./DisplayImage lena.jpg
问题一:make
编译错误
$ make
makefile:7: *** missing separator. Stop.
参考:makefile:4: *** missing separator. Stop
是因为makefile
需要用tab
键进行缩进而不是空格键,重新用tab
键进行缩进即可
问题二:加载共享库出错
$ ./DisplayImage lena.jpg
./DisplayImage: error while loading shared libraries: libopencv_highgui.so.3.4: cannot open shared object file: No such file or directory
首先查询生成可执行文件的动态链接库路径
$ ldd DisplayImage
linux-vdso.so.1 => (0x00007ffca0a96000)
libopencv_highgui.so.3.4 => not found
libopencv_imgcodecs.so.3.4 => not found
libopencv_core.so.3.4 => not found
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f7a5c64d000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f7a5c437000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7a5c06d000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7a5bd64000)
/lib64/ld-linux-x86-64.so.2 (0x00007f7a5c9cf000)
发现它缺少了libopencv_highgui.so.3.4、libopencv_imgcodecs.so.3.4和libopencv_core.so.3.4
,这些库应该都在OpenCV
的安装路径下
$ locate libopencv_highgui.so.3.4
/home/zj/opencv/opencv/build/lib/libopencv_highgui.so.3.4
/home/zj/opencv/opencv/build/lib/libopencv_highgui.so.3.4.5
/home/zj/opencv/opencv/install/lib/libopencv_highgui.so.3.4
/home/zj/opencv/opencv/install/lib/libopencv_highgui.so.3.4.5
设置环境变量LD_LIBRARY_PATH
,添加相应库路径即可
$ export LD_LIBRARY_PATH=/home/zj/opencv/opencv/install/lib/
编写CMakeLists.txt文件配置¶
新建CMakeLists.txt
$ vim CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
MESSAGE("OpenCV version: ${OpenCV_VERSION}")
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
编译、链接生成可执行文件
$ ls
CMakeLists.txt DisplayImage.cpp lena.jpg
$ cmake .
$ make
$ ./DisplayImage lena.jpg
问题一:CMake
编译错误
$ cmake .
...
...
CMake Error at CMakeLists.txt:3 (find_package):
By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "OpenCV", but
CMake did not find one.
Could not find a package configuration file provided by "OpenCV" with any
of the following names:
OpenCVConfig.cmake
opencv-config.cmake
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
"OpenCV_DIR" to a directory containing one of the above files. If "OpenCV"
provides a separate development package or SDK, be sure it has been
installed.
-- Configuring incomplete, errors occurred!
See also "/home/zj/opencv/cmake_test/CMakeFiles/CMakeOutput.log".
错误信息显示无法发现OpenCV
提供的包配置文件,需要添加OpenCV
安装路径到CMAKE_PREFIX_PATH
或者OpenCV_DIR
参考:OpenCV 3.2.0 CMakeLists.txt question
$ cmake -D CMAKE_PREFIX_PATH=/home/zj/opencv/install3.4 .
-- Found OpenCV: /home/zj/opencv/install3.4 (found version "3.4.5")
OpenCV version: 3.4.5
-- Configuring done
-- Generating done
-- Build files have been written to: /home/zj/opencv/cmake_test
或者把路径添加到配置文件
$ vim CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
set(CMAKE_PREFIX_PATH /home/zj/opencv/install3.4)
find_package( OpenCV REQUIRED )
MESSAGE("OpenCV version: ${OpenCV_VERSION}")
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
重新运行
$ cmake .
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenCV: /home/zj/opencv/install3.4 (found version "3.4.5")
OpenCV version: 3.4.5
-- Configuring done
-- Generating done
-- Build files have been written to: /home/zj/opencv/cmake_test
Python¶
编写测试程序DisplayImage.py
$ vim DisplayImage.py
#-*- coding: utf-8 -*-
import cv2
if __name__ == '__main__':
img = cv2.imread('lena.jpg')
if img is None:
print('Error: No image data\n')
exit(1)
cv2.imshow('Display Image', img)
cv2.waitKey(0)
测试
$ $ ls
DisplayImage.py lena.jpg
$ python DisplayImage.py