OpenCV实现从图像中选择矩形区域并保存

该博客介绍了如何根据《Learning OpenCV》的example4.1修改代码,实现载入图像后通过鼠标选择矩形区域。用户可以通过点击开始点,拖动鼠标绘制矩形,松开鼠标时矩形变为红色。按下s键,矩形变为绿色并保存所选ROI;按下q键取消选择,红色矩形消失。允许连续选择多个区域,按ESC退出程序。
摘要由CSDN通过智能技术生成

根据《Learning OpenCV》中的example4.1改写:


// An example program in which the
// user can draw boxes on the screen.
//
//#include <cv.h>
//#include <highgui.h>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
// Define our callback which we will install for
// mouse events.
//
void my_mouse_callback(
	int event, int x, int y, int flags, void* param
);

CvRect box;
bool drawing_box = false;
bool isRectDrawn = false;

// A litte subroutine to draw a box onto an image_copy
void draw_box( IplImage* img, CvRect rect ) {
	cvRectangle (
		img,
		cvPoint(box.x,box.y),
		cvPoint(box.x+box.width,box.y+box.height),
		cvScalar(0x00,0x00,0xff) /* blue */
	);
}

void draw_box_green( IplImage* img, CvRect rect ) {
	cvRectangle (
		img,
		cvPoint(box.x,box.y),
		cvPoint(box.x+box.width,box.y+box.height),
		cvScalar(0x00,0xff,0x00) /* green */
	);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值