OpenCV一个窗口显示多张图片

来源:互联网 发布:分类信息系统源码 编辑:程序博客网 时间:2024/05/01 12:03
[cpp] view plain copy
  1. #include <cv.h>  
  2. #include <highgui.h>  
  3.   
  4. #include <stdio.h>  
  5. #include <stdarg.h>  
  6. #include <time.h>  
  7. #include <iostream>  
  8.   
  9.   
  10. #pragma comment(lib, "cv.lib")  
  11. #pragma comment(lib, "cxcore.lib")  
  12. #pragma comment(lib, "highgui.lib")  
  13.   
  14. void cvShowManyImages(char* title, int nArgs, ...)   
  15. {  
  16.   
  17.     // img - Used for getting the arguments   
  18.     IplImage *img;  
  19.   
  20.     // DispImage - the image in which input images are to be copied  
  21.     IplImage *DispImage;  
  22.   
  23.     int size;  
  24.     int i;  
  25.     int m, n;  
  26.     int x, y;  
  27.   
  28.     // w - Maximum number of images in a row   
  29.     // h - Maximum number of images in a column   
  30.     int w, h;  
  31.   
  32.     // scale - How much we have to resize the image  
  33.     float scale;  
  34.     int max;  
  35.   
  36.     // If the number of arguments is lesser than 0 or greater than 12  
  37.     // return without displaying   
  38.     if(nArgs <= 0)  
  39.     {  
  40.         printf("Number of arguments too small....\n");  
  41.         return;  
  42.     }  
  43.     else if(nArgs > 12)  
  44.     {  
  45.         printf("Number of arguments too large....\n");  
  46.         return;  
  47.     }  
  48.     // Determine the size of the image, and the number of rows/cols  from number of arguments   
  49.     else if (nArgs == 1)  
  50.     {  
  51.         w = h = 1;  
  52.         size = 300;  
  53.     }  
  54.     else if (nArgs == 2)  
  55.     {  
  56.         w = 2; h = 1;  
  57.         size = 300;  
  58.     }  
  59.     else if (nArgs == 3 || nArgs == 4)  
  60.     {  
  61.         w = 2; h = 2;  
  62.         size = 300;  
  63.     }  
  64.     else if (nArgs == 5 || nArgs == 6) {  
  65.         w = 3; h = 2;  
  66.         size = 200;  
  67.     }  
  68.     else if (nArgs == 7 || nArgs == 8)  
  69.     {  
  70.         w = 4; h = 2;  
  71.         size = 200;  
  72.     }  
  73.     else  
  74.     {  
  75.         w = 4; h = 3;  
  76.         size = 150;  
  77.     }  
  78.   
  79.     // Create a new 3 channel image0  
  80.     DispImage = cvCreateImage( cvSize( 100+ size*w, 60 + size*h), 8, 3 );  
  81.   
  82.     // Used to get the arguments passed  
  83.     va_list args;  
  84.     va_start(args, nArgs);  
  85.   
  86.     // Loop for nArgs number of arguments  
  87.     for (i = 0, m = 20, n = 20; i < nArgs; i++, m += (20 + size))   
  88.     {  
  89.         // Get the Pointer to the IplImage  
  90.         img = va_arg(args, IplImage*);  
  91.   
  92.         // Check whether it is NULL or not  
  93.         // If it is NULL, release the image, and return  
  94.         if(img == 0)  
  95.         {  
  96.             printf("Invalid arguments");  
  97.             cvReleaseImage(&DispImage);  
  98.             return;  
  99.         }  
  100.   
  101.         // Find the width and height of the image  
  102.         x = img->width;  
  103.         y = img->height;  
  104.   
  105.         // Find whether height or width is greater in order to resize the image  
  106.         max = (x > y)? x: y;  
  107.   
  108.         // Find the scaling factor to resize the image  
  109.         scale = (float) ( (float) max / size );  
  110.   
  111.         // Used to Align the images  
  112.         if( i % w == 0 && m!= 20)  
  113.         {  
  114.             m = 20;  
  115.             n+= 0 + size;  
  116.         }  
  117.   
  118.         // Set the image ROI to display the current image  
  119.         //cvSetImageROI(DispImage, cvRect(m, n, (int)( x/scale ), (int)( y/scale )));  
  120.         cvSetImageROI(DispImage, cvRect(m, n, (int)( x/scale ), (int)( y/scale )));  
  121.         //      cout<<"x="<<m<<"y="<<n<<endl;  
  122.   
  123.         // Resize the input image and copy the it to the Single Big Image  
  124.         cvResize(img, DispImage);  
  125.   
  126.         // Reset the ROI in order to display the next image  
  127.         cvResetImageROI(DispImage);  
  128.     }  
  129.   
  130.     // Create a new window, and show the Single Big Image  
  131.     //cvNamedWindow( title, 1 );  
  132.     cvShowImage( title, DispImage);  
  133.   
  134.     /*cvWaitKey(0);*/  
  135.     //cvDestroyWindow(title);  
  136.   
  137.     // End the number of arguments  
  138.     va_end(args);  
  139.   
  140.     // Release the Image Memory  
  141.     cvReleaseImage(&DispImage);  
  142. }  
  143. int main(int argc,char **argv)   
  144. {  
  145.     /*   CvCapture *capture;*/  
  146.     int i=0;  
  147.   
  148.     IplImage *frame=cvLoadImage(".\\test.png");  
  149.     cvNamedWindow("video",1);  
  150.     cvResizeWindow("video",700,660);  
  151.   
  152.   
  153.     IplImage *frame_not=cvCreateImage(cvGetSize(frame),frame->depth,frame->nChannels);  
  154.     cvNot(frame,frame_not);  
  155.   
  156.     IplImage *frame_gray=cvCreateImage(cvGetSize(frame),frame->depth,1);  
  157.     IplImage *frame1=cvCreateImage(cvGetSize(frame),frame->depth,frame->nChannels);  
  158.     IplImage *frame_canny=cvCreateImage(cvGetSize(frame),frame->depth,1);  
  159.     IplImage *frame2=cvCreateImage(cvGetSize(frame),frame->depth,frame->nChannels);  
  160.     cvCvtColor(frame,frame_gray,CV_RGB2GRAY);  
  161.     cvCvtColor(frame_gray,frame1,CV_GRAY2BGR);  
  162.     cvCanny(frame_gray,frame_canny,20,75,3);  
  163.     cvCvtColor(frame_canny,frame2,CV_GRAY2BGR);  
  164.   
  165.   
  166.     cvShowManyImages("video",4,frame,frame_not,frame1,frame2);  
  167.     cvWaitKey();  
  168.   
  169.     cvReleaseImage(&frame_not);  
  170.     cvReleaseImage(&frame1);  
  171.     cvReleaseImage(&frame_gray);  
  172.     cvReleaseImage(&frame2);  
  173.     cvReleaseImage(&frame_canny);  
  174.   
  175.     cvDestroyWindow("video");  
  176.   
  177.     return 0;  
  178. }  


【Note:】

注意 这个函数显示的图像是 nChannels = 3 的。


你看见 cvShowManyImages 函数中的

  1. // Create a new 3 channel image0
  2. DispImage = cvCreateImage( cvSize( 100+ size*w, 60 + size*h), 8, 3 );


 

了么~~~吼吼, 如果想要 显示 单通道 的,把 3 改为 1 就OK了~~



==============================================================================

【起初我 cvLoadImage( "tutu"); 读入了之前

【cvLoadImage( ,0) 读入了一张图像,然后用 cvSaveImage(); 保存图像】

得到的一张图像

】 这样是可以显示的。 然后就误导了我以为 可以显示灰度图。。。。。。

后来,发现 cvLoadImage( ,0) 读入的 灰度图 老是 出错。。。。。。



后来发现 【cvLoadImage( ,0) 读入了一张图像,然后用 cvSaveImage(); 保存图像】 之后的图像 nChanels = 3 !


才发现:

cvSaveImage()保存的 图像只能为深度为8U的1通道或者3通道(RGB)的图像。

也就是说 nChanels = 3 , R=G=B 的 3 通道的 灰度图

总之,函数只显示 通道为3 的 图像!


=========================================================================================================================================

从文件中读入一幅图像可以使用imread函数来读取图像,

Mat img=imread(filename);

该语句将filename所指定路径的指定文件读取到img数组中,该函数读取的是默认的三通道图像,读取的顺序默认情况下是BGR顺序,如果想得到单通道(灰度级的)图像,则可以使用下面的方式:

Mat img=imread(filename,0);

0 0
原创粉丝点击