(12条消息) Opencv之鼠标响应setMouseCallback()的用法

平时在写代码时可能用到鼠标在图上做标记等其他作用,opencv主要用setMouseCallback()这个函数,下面来讲解:

一、setMouseCallback()

  1. void setMousecallback(const string& winname, MouseCallback onMouse, void* userdata=0)
  2. winname:窗口的名字
  3. onMouse:鼠标响应函数,回调函数。指定窗口里每次鼠标时间发生的时候,被调用的函数指针。 这个函数的原型应该为void on_Mouse(int event, int x, int y, int flags, void* param);
  4. userdate:传给回调函数的参数

再来介绍MouseCallback onMouse的函数原型:

  1. void on_Mouse(int event, int x, int y, int flags, void* param);
  2. event是 CV_EVENT_*变量之一
  3. x和y是鼠标指针在图像坐标系的坐标(不是窗口坐标系)
  4. flags是CV_EVENT_FLAG的组合, param是用户定义的传递到setMouseCallback函数调用的参数。
附常用的event:

#defineCV_EVENT_MOUSEMOVE

#defineCV_EVENT_LBUTTONDOWN 

#defineCV_EVENT_RBUTTONDOWN   

#defineCV_EVENT_LBUTTONUP    

#defineCV_EVENT_RBUTTONUP   

和标志位flags有关的:

#defineCV_EVENT_FLAG_LBUTTON 

注意: flags & CV_EVENT_FLAG_LBUTTON 的意思是 提取flags的CV_EVENT_FLAG_LBUTTON 标志位,!()的意思是 标志位无效
二、程序一(画直线)

用if()else()方法:

  1. #include<opencv2/opencv.hpp>
  2. #include<iostream>
  3. using namespace std;
  4. using namespace cv;

  5. #define WINDOW "原图"
  6. Mat g_srcImage,g_dstImage;
  7. Point previousPoint;
  8. bool P = false;
  9. void On_mouse(int event, int x, int y, int flags, void*);
  10. int main()
  11. {
  12. g_srcImage = imread("D://vvoo//face.jpg", 1);
  13. imshow(WINDOW, g_srcImage);
  14. setMouseCallback(WINDOW, On_mouse, 0);

  15. waitKey(0);
  16. return 0;
  17. }
  18. void On_mouse(int event, int x, int y, int flags, void*)
  19. {
  20. /*if (event == EVENT_LBUTTONUP ||!( flags&EVENT_FLAG_LBUTTON))
  21. {
  22. previousPoint = Point(-1, -1);
  23. }*/
  24. if (event == EVENT_LBUTTONDOWN)
  25. {
  26. previousPoint = Point(x, y);
  27. }
  28. else if (event == EVENT_MOUSEMOVE && (flags&EVENT_FLAG_LBUTTON))
  29. {
  30. Point pt(x, y);
  31. line(g_srcImage, previousPoint, pt, Scalar(0,0,255), 2, 5, 0);
  32. previousPoint = pt;
  33. imshow(WINDOW, g_srcImage);
  34. }
  35. }


参考switch()方法:

  1. void On_mouse(int event, int x, int y, int flags, void*)
  2. {
  3. switch (event)
  4. {
  5. case EVENT_LBUTTONUP:
  6. {
  7. P = false;
  8. }
  9. break;
  10. case EVENT_LBUTTONDOWN:
  11. {

  12. previousPoint = Point(x, y);

  13. P = true;
  14. }
  15. break;

  16. case EVENT_MOUSEMOVE :
  17. {
  18. if(P)
  19. {
  20. Point pt(x, y);
  21. line(g_srcImage, previousPoint, pt, Scalar(0,0,255), 2, 5, 0);
  22. previousPoint = pt;
  23. imshow(WINDOW, g_srcImage);
  24. }
  25. }
  26. break;
  27. }
  28. }


两者的实现方法不同,但是结果都是一样的,方法一没有用到EVENT_LBUTTONUP这个鼠标左键按下响应。

结果:

二、程序2(画矩形)

  1. #include<opencv2/opencv.hpp>
  2. #include<iostream>
  3. using namespace std;
  4. using namespace cv;

  5. Rect select;
  6. bool select_flag = false;
  7. Mat img, showImg;

  8. void A_on_Mouse(int event, int x, int y, int flags, void*param)//实现画矩形框
  9. {
  10. Point p1, p2;
  11. if (event == EVENT_LBUTTONDOWN)
  12. {
  13. select.x = x;
  14. select.y = y;
  15. select_flag = true;
  16. }
  17. else if (select_flag &&event == EVENT_MOUSEMOVE)
  18. {
  19. img.copyTo(showImg);
  20. p1 = Point(select.x, select.y);
  21. p2 = Point(x, y);
  22. rectangle(showImg, p1, p2, Scalar(0, 255, 0), 2);
  23. imshow("img", showImg);
  24. }
  25. else if (select_flag && event == EVENT_LBUTTONUP)
  26. {
  27. select_flag = false;
  28. }
  29. }
  30. void B_on_Mouse(int event, int x, int y, int flags, void*param)//实现画矩形框并截图
  31. {

  32. Point p1, p2;
  33. switch (event)
  34. {

  35. case EVENT_LBUTTONDOWN:
  36. {
  37. select.x = x;
  38. select.y = y;
  39. select_flag = true;
  40. }
  41. break;
  42. case EVENT_MOUSEMOVE:
  43. {
  44. if (select_flag)
  45. {

  46. img.copyTo(showImg);
  47. p1 = Point(select.x, select.y);
  48. p2 = Point(x, y);
  49. rectangle(showImg, p1, p2, Scalar(0, 255, 0), 2);
  50. imshow("img", showImg);
  51. }
  52. }
  53. break;
  54. case EVENT_LBUTTONUP:
  55. {
  56. //显示框出的ROI
  57. //改成Rect roi = Rect(p1, p2);为什么不对?
  58. Rect roi = Rect(Point(select.x, select.y), Point(x, y));
  59. if (roi.width && roi.height)//点一下时会没有反应
  60. {
  61. Mat roiImg = img(roi);
  62. imshow("roi", roiImg);
  63. }
  64. select_flag = false;

  65. }
  66. break;
  67. }
  68. }

  69. int main()
  70. {
  71. img = imread("D://vvoo//face.jpg", 1);
  72. showImg = img.clone();
  73. select.x = select.y = 0;
  74. imshow("img", showImg);


  75. while (1)
  76. {
  77. int key = waitKey(10);
  78. switch (key)
  79. {
  80. case 'a':
  81. setMouseCallback("img", A_on_Mouse, 0);
  82. break;
  83. case 'b':
  84. setMouseCallback("img", B_on_Mouse, 0);
  85. break;
  86. }
  87. if (key == 27||key=='q')
  88. break;
  89. }
  90. waitKey(0);
  91. return 0;
  92. }

结果:

根据按键实现两种功能,这里只截图了第二种画矩形框并把它截图功能:

三、参考资料

2.http://www.xuebuyuan.com/1528309.html

3.http://www.zybang.com/question/151ef07151a0e64dd54811485a418dcd.html

(0)

相关推荐