一个窗口显示多幅图片

/*一般情况下,我们创建一个窗口,窗口里显示一张图片,为了对比图像,多张图像显示在一个窗口上方便很多。

之前见过他人博客中的原创,以下有一些改动。微笑

以下是多张图片(三通道 ,或者改变参数->单通道)在一个窗口显示的函数:*/

// ==========一个窗口显示多幅图片(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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值