一個窗口顯示多幅圖片

/*一般情況下,我們創建一個窗口,窗口裏顯示一張圖片,爲了對比圖像,多張圖像顯示在一個窗口上方便很多。

之前見過他人博客中的原創,以下有一些改動。微笑

以下是多張圖片(三通道 ,或者改變參數->單通道)在一個窗口顯示的函數:*/

// ==========一個窗口顯示多幅圖片(3通道) ==========

void cvShowManyImages(char* title, int nArgs, ...)   //圖片可以從1到8任意輸入
{  
    // img - Used for getting the arguments 參數  
    IplImage *img;  
  
    // DispImage - the image in which input images are to be copied  
    IplImage *DispImage;  
  
    int size;  
    int i;  
    int m, n;  
    int x, y;  
  
    // w - 行最大數
    // h - 列最大數
    int w, h;  
  
    // scale - How much we have to resize the image  
    float scale;  
    int max;  
  
    // 圖片數量 nArgs<0 或者 nArgs>8,return
    if(nArgs <= 0)  
    {  
        printf("Number of arguments too small....\n");  
        return;  
    }  
    else if(nArgs > 8)  
    {  
        printf("Number of arguments too large....\n");  
        return;  

    }  

   // 圖片數量 nArgs在0到8之間   
    else if (nArgs == 1)  
    {  
        w = h = 1;  
        size = 300;  
    }  
    else if (nArgs == 2)  
    {  
        w = 2; h = 1;  
        size = 300;  
    }  
    else if (nArgs == 3 || nArgs == 4)  
    {  
        w = 2; h = 2;  
        size = 300;  
    }  
    else if (nArgs == 5 || nArgs == 6) {  
        w = 3; h = 2;  
        size = 200;  
    }  
    else
    {  
        w = 4; h = 2;  //最多8副圖
        size = 200;  
    }  
    // 新建3通道圖像  

    DispImage = cvCreateImage( cvSize( 100+ size*w, 60 + size*h), 8, 3 );  //參數3改成1,則顯示單通道

    // Used to get the arguments passed  
    va_list args;  
    va_start(args, nArgs);  
  
    // Loop for nArgs number of arguments  
    for (i = 0, m = 20, n = 20; i < nArgs; i++, m += (20 + size))   
    {  
        // Get the Pointer to the IplImage  
        img = va_arg(args, IplImage*);  
  
        // 爲空,釋放  
        if(img == 0)  
        {  
            printf("Invalid arguments");  
            cvReleaseImage(&DispImage);  
            return;  
        }  
        // 圖想的寬和高
        x = img->width;  
        y = img->height;  
        // Find whether height or width is greater in order to resize the image  
        max = (x > y)? x: y;  
        // Find the scaling factor to resize the image  
        scale = (float) ( (float) max / size );  
        // Used to Align the images  
        if( i % w == 0 && m!= 20)  
        {  
            m = 20;  
            n+= 0 + size;  
        }  

         // Set the image ROI to display the current image  
         cvSetImageROI(DispImage, cvRect(m, n, (int)( x/scale ), (int)( y/scale )));  
  
         // Resize  
         cvResize(img, DispImage);  
  
         // Reset the ROI in order to display the next image  

         cvResetImageROI(DispImage);  

   }  
    // Create a new window, and show the Single Big Image  
    //cvNamedWindow( title, 1 );  
    cvShowImage( title, DispImage);  
  
    //cvWaitKey(0); 
     //cvDestroyWindow(title);  
  
    // End the number of arguments  
    va_end(args);  
  
    // 釋放圖像 
    cvReleaseImage(&DispImage);  
}  

// =================主函數===================

int main(int argc,char** argv)
{
//加載原始彩色圖片
IplImage* src_image = cvLoadImage("F:\\image\\貼片.jpg",
CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);
if (src_image == NULL)  return -1;

//2種方式平滑濾波
IplImage* smooth_image = cvCreateImage(cvGetSize(src_image),IPL_DEPTH_8U,3);
IplImage* smooth2_image = cvCreateImage(cvGetSize(src_image),IPL_DEPTH_8U,3);
cvSmooth(src_image,smooth_image,CV_MEDIAN ,5,5,0,0); //  中值濾波, CV_GAUSSIAN 高斯濾波 或  CV_BILATERAL雙邊濾波
cvSmooth(src_image,smooth2_image,CV_GAUSSIAN ,5,5,0,0);  // 高斯濾波

//一個窗口顯示多張圖片
cvNamedWindow("ShowManyImages",1);  
    cvResizeWindow("ShowManyImages",700,660);
cvShowManyImages("ShowManyImages",3,src_image,smooth_image,smooth2_image);  //顯示多幅圖

cvWaitKey();  
    cvReleaseImage(&src_image);  
    cvReleaseImage(&smooth_image);  

  cvReleaseImage(&smooth2_image);     
 
    cvDestroyWindow("ShowManyImages");    

return 0;
}


//ShowManyImages.jpg


//ShowMangeImages2.jpg

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章