歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 在OpenCV中鼠標移動區域局部放大

在OpenCV中鼠標移動區域局部放大

日期:2017/3/1 10:10:33   编辑:Linux編程

這個程序類似於淘寶購物時,在商品圖像上移動鼠標,會出現一個放大鼠標周圍矩形區域內的局部圖像的窗口。話不多說,直接貼代碼。

  1. #include <cv.h>
  2. #include <highgui.h>
  3. #include <stdio.h>
  4. #pragma comment( lib, "cv.lib" )
  5. #pragma comment( lib, "cxcore.lib" )
  6. #pragma comment( lib, "highgui.lib" )
  7. IplImage* org = 0;
  8. IplImage* img = 0;
  9. IplImage* dst = 0;
  10. int foo=60;
  11. void on_mouse( int event, int x, int y, int flags, void* ustc)
  12. {
  13. if( event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))
  14. {
  15. cvCopy(org,img);
  16. CvPoint p0;
  17. CvPoint p1;
  18. if(x<foo)
  19. {
  20. if(y<foo)
  21. {
  22. p0=cvPoint(0,0);
  23. p1=cvPoint(2*foo,2*foo);
  24. }
  25. else if(y>img->height-foo)
  26. {
  27. p0=cvPoint(0,img->height-2*foo);
  28. p1=cvPoint(2*foo,img->height);
  29. }
  30. else
  31. {
  32. p0=cvPoint(0,y-foo);
  33. p1=cvPoint(2*foo,y+foo);
  34. }
  35. }
  36. else if(x>img->width-foo)
  37. {
  38. if(y<foo)
  39. {
  40. p0=cvPoint(img->width-2*foo,0);
  41. p1=cvPoint(img->width,2*foo);
  42. }
  43. else if(y>img->height-foo)
  44. {
  45. p0=cvPoint(img->width-2*foo,img->height-2*foo);
  46. p1=cvPoint(img->width,img->height);
  47. }
  48. else
  49. {
  50. p0=cvPoint(img->width-2*foo,y-foo);
  51. p1=cvPoint(img->width,y+foo);
  52. }
  53. }
  54. else
  55. {
  56. if(y<foo)
  57. {
  58. p0=cvPoint(x-foo,0);
  59. p1=cvPoint(x+foo,2*foo);
  60. }
  61. else if(y>img->height-foo)
  62. {
  63. p0=cvPoint(x-foo,img->height-2*foo);
  64. p1=cvPoint(x+foo,img->height);
  65. }
  66. else
  67. {
  68. p0=cvPoint(x-foo,y-foo);
  69. p1=cvPoint(x+foo,y+foo);
  70. }
  71. }
  72. cvRectangle(img,p0,p1,CV_RGB(0,255,0));
  73. cvSetImageROI(org,cvRect(p0.x,p0.y,p1.x-p0.x,p1.y-p0.y));
  74. cvResize(org,dst);
  75. cvResetImageROI(org);
  76. cvShowImage( "img", img );
  77. cvShowImage("dst",dst);
  78. }
  79. }
  80. int main()
  81. {
  82. org=cvLoadImage("lena.jpg",1);
  83. img=cvCloneImage(org);
  84. dst=cvCreateImage(cvSize(foo*4,foo*4),org->depth,org->nChannels);
  85. cvNamedWindow("img",1);
  86. cvSetMouseCallback( "img", on_mouse, 0 );
  87. cvShowImage("img",img);
  88. cvNamedWindow("dst",1);
  89. cvWaitKey(0);
  90. cvDestroyAllWindows();
  91. cvReleaseImage(&org);
  92. cvReleaseImage(&img);
  93. cvReleaseImage(&dst);
  94. return 0;
  95. }

效果圖如下

Copyright © Linux教程網 All Rights Reserved