OpenCV學習筆記(21)在OpenCV窗口中創建滾動條瀏覽大圖

来源:互联网 发布:免费的程序化软件 编辑:程序博客网 时间:2024/06/01 07:58

http://blog.csdn.net/chenyusiyuan/article/details/6565424

很久沒發博客了,趁著近期項目需要,再和大家分享一個小程序,即在OpenCV窗口中創建水平、豎直滾動條來瀏覽大型圖像。如果我們所要顯示的圖像像素較高,例如1440*900的,而顯示器只有 1280*800的分辨率,那麼通過cvNamedWindow創建的最大窗口也只能看到部分圖像,其余的因為超出窗口大小看不到了,也沒有滾動條用來滾動觀察其余圖像,這樣,就不方便我們通過窗口對圖像進行交互操作。通常滾動條是在MFC界面上建立的,這方面的例程很多,例如http://www.opencv.org.cn/forum/viewtopic.php?f=1&t=8349 ;也有使用OpenCV的Trackbar的例子,但Trackbar只有水平滾動、沒有垂直方式,而且是按1為步長,使用時不方便、也佔用了不少窗口空間。下面這段例程展示了怎樣通過 cvSetMouseCallback, cvRect, cvRectangleR, cvResize 等一系列OpenCV函數來創建方便、可隨意定制的滾動條。

 

snap1

snap2

 

[c-sharp] view plaincopy
  1. // Image_ScrollBar.cpp : Defines the entry point for the console application.  
  2. //  
  3.  
  4. #include "stdafx.h"  
  5.  
  6.  
  7. #include <opencv2/highgui/highgui.hpp>  
  8. #include <opencv2/imgproc/imgproc_c.h>  
  9.  
  10. #include <iostream>  
  11. #include <vector>  
  12.   
  13. using namespace std;  
  14.   
  15.   
  16. double mx = 0, my = 0;  
  17. int dx = 0, dy = 0, horizBar_x = 0, vertiBar_y = 0;  
  18. bool clickVertiBar = false, clickHorizBar = false, needScroll = false;  
  19. CvRect rect_bar_horiz, rect_bar_verti;  
  20.   
  21. void help()  
  22. {  
  23.     printf(  
  24.         "/n"  
  25.         "This program demonstrated the use of the cvSetMouseCallback /n"  
  26.         "for viewing large image with scroll bar in a small window/n"  
  27.         "created by OpenCV highgui model. (chenyusiyuan, 2011-06-24)/n"  
  28.         "Call:/n"  
  29.         "./Image_ScrollBar [<img_filename default im.jpg> <window_width default 1400> <window_height default 700>]/n/n"  
  30.         );  
  31. }  
  32.   
  33.   
  34. void mouse_callback( int eventint x, int y, int flags, void* param )  
  35. {  
  36.     if (needScroll)  
  37.     {  
  38.         switchevent )   
  39.         {  
  40.         case CV_EVENT_LBUTTONDOWN:  
  41.             mx = x, my = y;  
  42.             dx = 0, dy = 0;  
  43.             // 按下左鍵時光標定位在水平滾動條區域內  
  44.             if (x >= rect_bar_horiz.x && x <= rect_bar_horiz.x+rect_bar_horiz.width  
  45.                 && y >= rect_bar_horiz.y && y<= rect_bar_horiz.y+rect_bar_horiz.height)  
  46.             {  
  47.                 clickHorizBar = true;  
  48.             }  
  49.             // 按下左鍵時光標定位在垂直滾動條區域內  
  50.             if (x >= rect_bar_verti.x && x <= rect_bar_verti.x+rect_bar_verti.width  
  51.                 && y >= rect_bar_verti.y && y<= rect_bar_verti.y+rect_bar_verti.height)  
  52.             {  
  53.                 clickVertiBar = true;  
  54.             }  
  55.             break;   
  56.         case CV_EVENT_MOUSEMOVE:   
  57.             if (clickHorizBar)  
  58.             {  
  59.                 dx = fabs(x-mx) > 1 ? (int)(x-mx) : 0;  
  60.                 dy = 0;  
  61.             }  
  62.             if (clickVertiBar)  
  63.             {  
  64.                 dx = 0;  
  65.                 dy = fabs(y-my) > 1 ? (int)(y-my) : 0;  
  66.             }  
  67.             mx = x, my = y;  
  68.             break;    
  69.         case CV_EVENT_LBUTTONUP:   
  70.             mx = x, my = y;  
  71.             dx = 0, dy = 0;  
  72.             clickHorizBar = false;  
  73.             clickVertiBar = false;  
  74.             break;    
  75.         default:  
  76.             dx = 0, dy = 0;  
  77.             break;  
  78.         }  
  79.     }  
  80. }  
  81.   
  82. void myShowImageScroll(char* title, IplImage* src_img,   
  83.                   int winWidth = 1400, int winHeight = 700) // 顯示窗口大小默認為 1400×700  
  84. {  
  85.     IplImage* dst_img;  
  86.     CvRect  rect_dst,   // 窗口中有效的圖像顯示區域  
  87.             rect_src;   // 窗口圖像對應於源圖像中的區域  
  88.     int imgWidth = src_img->width,   
  89.         imgHeight = src_img->height,  
  90.         barWidth = 25;  // 滾動條的寬度(像素)  
  91.     double  scale_w = (double)imgWidth/(double)winWidth,    // 源圖像與窗口的寬度比值  
  92.             scale_h = (double)imgHeight/(double)winHeight;  // 源圖像與窗口的高度比值  
  93.   
  94.     if(scale_w<1)   
  95.         winWidth = imgWidth+barWidth;  
  96.     if(scale_h<1)   
  97.         winHeight = imgHeight+barWidth;  
  98.   
  99.     int showWidth = winWidth, showHeight = winHeight; // rect_dst 的寬和高  
  100.     int src_x = 0, src_y = 0;   // 源圖像中 rect_src 的左上角位置  
  101.     int horizBar_width = 0, horizBar_height = 0,  
  102.         vertiBar_width = 0, vertiBar_height = 0;  
  103.   
  104.     needScroll = scale_w>1.0 || scale_h>1.0 ? TRUE : FALSE;  
  105.     // 若圖像大於設定的窗口大小,則顯示滾動條  
  106.     if(needScroll)  
  107.     {  
  108.         dst_img = cvCreateImage(cvSize(winWidth, winHeight),src_img->depth, src_img->nChannels);  
  109.         cvZero(dst_img);  
  110.         // 源圖像寬度大於窗口寬度,則顯示水平滾動條  
  111.         if(scale_w > 1.0)  
  112.         {  
  113.             showHeight = winHeight - barWidth;  
  114.             horizBar_width = (int)((double)winWidth/scale_w);  
  115.             horizBar_height = winHeight-showHeight;  
  116.             horizBar_x = min(  
  117.                 max(0,horizBar_x+dx),  
  118.                 winWidth-horizBar_width);  
  119.             rect_bar_horiz = cvRect(  
  120.                 horizBar_x,   
  121.                 showHeight+1,   
  122.                 horizBar_width,   
  123.                 horizBar_height);  
  124.             // 顯示水平滾動條  
  125.             cvRectangleR(dst_img, rect_bar_horiz, cvScalarAll(255), -1);  
  126.         }   
  127.         // 源圖像高度大於窗口高度,則顯示垂直滾動條  
  128.         if(scale_h > 1.0)  
  129.         {  
  130.             showWidth = winWidth - barWidth;  
  131.             vertiBar_width = winWidth-showWidth;  
  132.             vertiBar_height = (int)((double)winHeight/scale_h);  
  133.             vertiBar_y = min(  
  134.                 max(0,vertiBar_y+dy),   
  135.                 winHeight-vertiBar_height);  
  136.             rect_bar_verti = cvRect(  
  137.                 showWidth+1,   
  138.                 vertiBar_y,   
  139.                 vertiBar_width,   
  140.                 vertiBar_height);  
  141.             // 顯示垂直滾動條  
  142.             cvRectangleR(dst_img, rect_bar_verti, cvScalarAll(255), -1);  
  143.         }  
  144.   
  145.         showWidth = min(showWidth,imgWidth);  
  146.         showHeight = min(showHeight,imgHeight);  
  147.         // 設置窗口顯示區的 ROI  
  148.         rect_dst = cvRect(0, 0, showWidth, showHeight);  
  149.         cvSetImageROI(dst_img, rect_dst);  
  150.         // 設置源圖像的 ROI  
  151.         src_x = (int)((double)horizBar_x*scale_w);  
  152.         src_y = (int)((double)vertiBar_y*scale_h);  
  153.         src_x = min(src_x, imgWidth-showWidth);  
  154.         src_y = min(src_y, imgHeight-showHeight);  
  155.         rect_src = cvRect(src_x, src_y, showWidth, showHeight);  
  156.         cvSetImageROI(src_img, rect_src);  
  157.         // 將源圖像內容復制到窗口顯示區  
  158.         cvCopy(src_img, dst_img);  
  159.   
  160.         cvResetImageROI(dst_img);  
  161.         cvResetImageROI(src_img);  
  162.         // 顯示圖像和滾動條  
  163.         cvShowImage(title,dst_img);  
  164.   
  165.         cvReleaseImage(&dst_img);  
  166.     }  
  167.     // 源圖像小於設定窗口,則直接顯示圖像,無滾動條  
  168.     else  
  169.     {  
  170.         cvShowImage(title, src_img);  
  171.     }     
  172. }  
  173.   
  174. int main(int argc, char** argv)  
  175. {  
  176.     help();  
  177.     const char* filename = argc > 1 ? argv[1] : "im.jpg";  
  178.     int width = 1400, height = 700;  
  179.     if (4==argc)  
  180.     {  
  181.         sscanf( argv[2], "%u", &width );  
  182.         sscanf( argv[3], "%u", &height );  
  183.     }  
  184.   
  185.     cvNamedWindow("Image Scroll Bar", 1);  
  186.     cvSetMouseCallback("Image Scroll Bar", mouse_callback);  
  187.   
  188.     IplImage* image = cvLoadImage( filename, CV_LOAD_IMAGE_COLOR );  
  189.     if( !image )  
  190.     {  
  191.         fprintf( stderr, "Can not load %s and/or %s/n"  
  192.             "Usage: Image_ScrollBar [<img_filename default im.jpg>]/n",  
  193.             filename );  
  194.         exit(-1);  
  195.     }  
  196.   
  197.     while(1)  
  198.     {  
  199.         myShowImageScroll("Image Scroll Bar", image, width, height);  
  200.   
  201.         int KEY = cvWaitKey(10);  
  202.         if( (char) KEY == 27 )  
  203.             break;  
  204.   
  205.     }  
  206.     cvDestroyWindow("Image Scroll Bar");  
  207.   
  208.     return 0;  
  209. }  
  

 

 

程序還只是實現了比較初步的功能,只能通過鼠標拖曳滾動條來瀏覽圖像,不能通過鼠標中鍵滾輪實現上下滾動,這個主要是 cvSetMouseCallback 裡的 Event 不包括滾輪滑動事件,有空再探索下解決辦法。歡迎大家一起交流討論。