opencv学习心得(一)鼠标响应事件绘制轮廓

//#include "gaosi.h"

#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace cv;
using namespace std;
//声明全局变量
/******************************鼠标选取矩形框变量初始化*******************/
CvRect rect;//矩形框
CvPoint origin;//图像像素点坐标的得到
BOOL  window_flag=false;//在网上找的,很有想法,学习之。
/*****************************直方图绘制初始化**********************/

//通过这个函数可以在图像上任意点击,形成矩形框
void OnMouse(int event,int x,int y,int,void*)
{
	if (window_flag)
	{
		rect.x=min(origin.x,x);//在鼠标按下到弹起阶段实现对矩形框的大小确定
		rect.y=min(origin.y,y);
		rect.width=abs(x-origin.x);//实现对矩形框位置的确定
		rect.height=abs(y-origin.y);
		//rect&=Rect(0,0,img.cols,img.rows);
	}
	if (event==CV_EVENT_LBUTTONDOWN)
	{
		window_flag=TRUE;
        origin=Point(x,y);//保存下捕捉的点坐标
		rect=cvRect(x,y,0,0);
		cout<<origin.x<<endl;//输出开始点的坐标

	} 
	else if(event==CV_EVENT_LBUTTONUP)
	{
		window_flag=FALSE;

	}
	
}

int main()
{
	CvCapture *capture;
	IplImage *pFrame;
	capture=cvCaptureFromAVI("E:\\自己编写的程序\\测试视频\\test2.avi");
	//cvNamedWindow("camera");
	setMouseCallback("camera",OnMouse,0);
	while(pFrame=cvQueryFrame(capture))
	{
		cvRectangle(pFrame,cvPoint(rect.x,rect.y),cvPoint(rect.x+rect.width,rect.y+rect.height),CV_RGB(255,5,0),3,8,0);
		cvShowImage("camera",pFrame);
		//imshow("camera",pFrame);
		char c=cvWaitKey(33);
		if(c==27)//ESC键
			return -1;
	}
	return 0;

}
选取轮廓之后再做相关直方图的建立。
原文地址:https://www.cnblogs.com/polly333/p/4498434.html