• 学习OpenCV——鼠标事件(画框)


    [cpp] view plain copy
     
     print?
    1. #include "cv.h"  
    2. #include "highgui.h"  
    3.   
    4. bool check_line_state=false;  
    5. IplImage* workImg;  
    6. IplImage* imgshow;  
    7. CvRect ROI_rect;  
    8.   
    9. void on_mouse4(int event, int x,int y,int flags,void* param)  
    10. {  
    11.     int thickness=2;  
    12.     CvPoint p1,p2;  
    13.     if(event==CV_EVENT_LBUTTONDOWN)  
    14.     {  
    15.         //当前鼠标位置(x,y)  
    16.         ROI_rect.x=x;  
    17.         ROI_rect.y=y;  
    18.         check_line_state=true;  
    19.     }  
    20.     else if(check_line_state&&event==CV_EVENT_MOUSEMOVE)//鼠标状态  
    21.     {  
    22.         cvCopy(workImg,imgshow);//这一步很重要,不断更新显示图像,不然画出的图像如图2所示  
    23.         p1=cvPoint(ROI_rect.x,ROI_rect.y);  
    24.         p2=cvPoint(x,y);  
    25.         cvRectangle(imgshow,p1,p2,CV_RGB(0,255,150),thickness,CV_AA,0);  
    26.         cvShowImage("image",imgshow);  
    27.           
    28.     }  
    29.     else if(check_line_state&&event==CV_EVENT_LBUTTONUP)  
    30.     {  
    31.         check_line_state=false;  
    32.         cvWaitKey(20);  
    33.     }  
    34. }  
    35.   
    36. int main()  
    37. {  
    38.     workImg=cvLoadImage("D:/Demo.jpg",1);  
    39.     imgshow=cvCreateImage(cvSize(workImg->width,workImg->height),workImg->depth,workImg->nChannels);  
    40.     cvCopy(workImg,imgshow);  
    41.     //cvFlip(imgshow);  
    42.       
    43.     ROI_rect.x=ROI_rect.y=0;  
    44.     cvNamedWindow("image",CV_WINDOW_AUTOSIZE);  
    45.     cvShowImage("image",imgshow);  
    46.   
    47.     cvSetMouseCallback("image",on_mouse4);  
    48.       
    49.     cvWaitKey();  
    50.     cvDestroyWindow("image");  
    51. }  

    图1:                                                                                               图2:

       

    from: http://blog.csdn.net/yangtrees/article/details/7573919

  • 相关阅读:
    进度条
    html5 表单新增事件
    html5 表单的新增type属性
    html5 表单的新增元素
    html5 语义化标签
    jq 手风琴案例
    codeforces 702D D. Road to Post Office(数学)
    codeforces 702C C. Cellular Network(水题)
    codeforces 702B B. Powers of Two(水题)
    codeforces 702A A. Maximum Increase(水题)
  • 原文地址:https://www.cnblogs.com/GarfieldEr007/p/5401895.html
Copyright © 2020-2023  润新知