使用opencv-3.4.6, Ubuntu系统。
步骤:
- 安装opencv,
过程可以参考: https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html
- 编程
opencv-3.4.6的官方例子都是用c++代码编写的,但是由于项目需要在c代码中调用opencv库,所以这里使用opencv 的c接口。 所有的C api 都可以在opencv 官网教程中查到。如果不知道使用哪个函数,可以在c++ sample中查看c++ 接口,然后根据名字去查找对应的c接口,一般是对应好的。比如,c++中的接口 cv::rectangle( ) 对应的c 接口是 cvRectangle。
c sample 如下:
//test.c
#include <opencv2/imgcodecs/imgcodecs_c.h>
#include <opencv2/highgui/highgui_c.h>
#include <stdio.h>
int main(int argc, char** argv)
{
//only support png image
if ( argc != 2 )
{
printf("usage: test <Image_Path>\n");
return -1;
}
CvMat* mat = cvLoadImageM(argv[1], CV_LOAD_IMAGE_COLOR);
if (!mat) {
printf("cvLoadImageM error\n");
return -1;
}
cvNamedWindow("image", CV_WINDOW_AUTOSIZE);
/* CvRect r = cvRect(20, 20, 400, 400); //x,y,w,h */
/* CvScalar c = cvScalar(255, 255, 0, 0); */
/* cvRectangleR(mat, r, c, 5, 8, 0); */
CvPoint p1 = cvPoint(20, 20); //x,y
CvPoint p2 = cvPoint(420, 420); //x,y
CvScalar c = cvScalar(255, 255, 0, 0);
cvRectangle(mat, p1, p2, c, 5, 8, 0);
cvShowImage ("image", mat);
cvWaitKey(0);
}
对比着看c++代码:
//displayimage.cpp
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.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 );
Rect r = Rect(10, 20, 300, 300);
rectangle(image, r, Scalar(255, 0, 0), 1, 8, 0);
imshow("Display Image", image);
waitKey(0);
return 0;
}
- 编译
使用cmake 来编译, CMakeList.txt如下L:
#CMakeLists.txt
# cmake needs this line
cmake_minimum_required(VERSION 2.8)
#SET(CMAKE_C_COMPILER "/home/public/local/bin/gcc")
#SET(CMAKE_CXX_COMPILER "/usr/bin/g++")
# Define project name
project(displayimage)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
if(CMAKE_VERSION VERSION_LESS "2.8.11")
# Add OpenCV headers location to your include paths
include_directories(${OpenCV_INCLUDE_DIRS})
endif()
# Declare the executable target built from your sources
add_executable(test test.c)
add_executable(displayimage displayimage.cpp)
# Link your application with OpenCV libraries
target_link_libraries(test ${OpenCV_LIBS})
target_link_libraries(displayimage ${OpenCV_LIBS})
在 CMakeList.txt 所在目录编译:
cmake .
make
- 运行
./test ./test.png
可以看到图片被画框:
- 说明
实验过程中发现opencv无法加载jpg图片.
后来发现是因为在安装opencv之后升级过一次libjpeg库,从8升级到9, 但是opencv中的信息没有更新过来.后来重新编译opencv并安装,就可以正常加载jpg图片了.