OpenCV学习笔记(五十七)——在同一窗口显示多幅图片

闲话少说,今天不介绍复杂的算法了,来个简单的,大家写文章做图经常用Matlab,在Matlab里经常在一个窗口里打开多幅图片。遗憾的是OpenCV没有集成这样的功能,但这难不倒大家,让我试试用ROI来解决这个问题。

没啥好讲的,上代码好了:

[cpp]  view plain copy
  1. void imshowMany(const std::string& _winName, const vector<Mat>& _imgs)  
  2. {  
  3.     int nImg = (int)_imgs.size();  
  4.       
  5.     Mat dispImg;  
  6.   
  7.     int size;  
  8.     int x, y;  
  9.   
  10.     // w - Maximum number of images in a row   
  11.     // h - Maximum number of images in a column   
  12.     int w, h;  
  13.     // scale - How much we have to resize the image  
  14.     float scale;  
  15.     int max;  
  16.   
  17.     if (nImg <= 0)   
  18.     {  
  19.         printf("Number of arguments too small....\n");  
  20.         return;  
  21.     }  
  22.     else if (nImg > 12)  
  23.     {  
  24.         printf("Number of arguments too large....\n");  
  25.         return;  
  26.     }  
  27.       
  28.     else if (nImg == 1)  
  29.     {  
  30.         w = h = 1;  
  31.         size = 300;  
  32.     }  
  33.     else if (nImg == 2)  
  34.     {  
  35.         w = 2; h = 1;  
  36.         size = 300;  
  37.     }  
  38.     else if (nImg == 3 || nImg == 4)  
  39.     {  
  40.         w = 2; h = 2;  
  41.         size = 300;  
  42.     }  
  43.     else if (nImg == 5 || nImg == 6)  
  44.     {  
  45.         w = 3; h = 2;  
  46.         size = 200;  
  47.     }  
  48.     else if (nImg == 7 || nImg == 8)  
  49.     {  
  50.         w = 4; h = 2;  
  51.         size = 200;  
  52.     }  
  53.     else  
  54.     {  
  55.         w = 4; h = 3;  
  56.         size = 150;  
  57.     }  
  58.   
  59.     dispImg.create(Size(100 + size*w, 60 + size*h), CV_8UC3);  
  60.   
  61.     for (int i= 0, m=20, n=20; i<nImg; i++, m+=(20+size))  
  62.     {  
  63.         x = _imgs[i].cols;  
  64.         y = _imgs[i].rows;  
  65.   
  66.         max = (x > y)? x: y;  
  67.         scale = (float) ( (float) max / size );  
  68.   
  69.         if (i%w==0 && m!=20)  
  70.         {  
  71.             m = 20;  
  72.             n += 20+size;  
  73.         }  
  74.   
  75.         Mat imgROI = dispImg(Rect(m, n, (int)(x/scale), (int)(y/scale)));  
  76.         resize(_imgs[i], imgROI, Size((int)(x/scale), (int)(y/scale)));  
  77.     }  
  78.   
  79.     namedWindow(_winName);  
  80.     imshow(_winName, dispImg);  
  81. }  

附上效果图一张:


工程的下载链接为http://download.csdn.net/detail/yang_xian521/4531610

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值