OpenCV笔记(四)

来源:互联网 发布:域名检测 编辑:程序博客网 时间:2024/06/05 04:11
#include<opencv2/opencv.hpp>#include<opencv2/highgui/highgui.hpp>#include<iostream>using namespace cv;#define WINDOW "Image"int g_pVal;//滚动条当前值int g_max;//滚动条最大值Mat redImage;Mat tmpImage;Rect g_rectangle;//保存鼠标选中的矩形框bool g_draw;void callFunc(int a, void* param){Mat Img=*(Mat*)param;int cols=Img.cols;//图片列数int rows=Img.rows;//图片行数int x0=g_rectangle.x;//选中区域的左上角坐标int y0=g_rectangle.y;int x1=x0+g_rectangle.width;//选中区域的右下角坐标int y1=y0+g_rectangle.height;for(int i=x0;i<=x1;i++){//在选中区域中处理图片for(int j=y0;j<=y1;j++){if(Img.at<Vec3b>(j,i)[2]-30>Img.at<Vec3b>(j,i)[0]&&Img.at<Vec3b>(j,i)[2]-30>Img.at<Vec3b>(j,i)[1]){Img.at<Vec3b>(j,i)[2]=g_pVal;}}}imshow(WINDOW,Img);}void mouseCall(int even,int x,int y,int flags,void* param){//鼠标操作的回调函数Mat& image=*(Mat*)param;switch(even){case EVENT_MOUSEMOVE://鼠标移动{if(g_draw){g_rectangle.width=x-g_rectangle.x;g_rectangle.height=y-g_rectangle.y;}}break;case EVENT_LBUTTONDOWN://左键{g_draw=true;g_rectangle=Rect(x,y,0,0);//起点横坐标、纵坐标、宽、高}break;case EVENT_LBUTTONUP://右键{g_draw=false;if(g_rectangle.width<0){g_rectangle.x+=g_rectangle.width;g_rectangle.width=-g_rectangle.width;}if(g_rectangle.height<0){g_rectangle.y+=g_rectangle.height;g_rectangle.height=-g_rectangle.height;}callFunc(0,&image);}break;}}int main(){redImage=imread("redflo.bmp",1);redImage.copyTo(tmpImage);namedWindow(WINDOW,CV_WINDOW_AUTOSIZE);g_pVal=0;g_max=200;g_rectangle=Rect(0,0,0,0);g_draw=false;imshow(WINDOW,tmpImage);setMouseCallback(WINDOW,mouseCall,(void*)&tmpImage);//设置鼠标操作waitKey(0);return 0;}

0 0
原创粉丝点击