用opencv2的C api 给图片画矩形框

使用opencv-3.4.6, Ubuntu系统。
步骤:

  1. 安装opencv,
    过程可以参考: https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html
  2. 编程
    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;
}

  1. 编译
    使用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

  1. 运行
    ./test ./test.png
    可以看到图片被画框:
    在这里插入图片描述
  2. 说明
    实验过程中发现opencv无法加载jpg图片.
    后来发现是因为在安装opencv之后升级过一次libjpeg库,从8升级到9, 但是opencv中的信息没有更新过来.后来重新编译opencv并安装,就可以正常加载jpg图片了.
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值