标签:
前面介绍了如何批量的读取图片,从而也会有批量的显示一堆图片,那么在平时我们显示图片的时候,会发现都是一个图片一个窗口,会出来很多,这时候就会想到matlab中我们经常会使用subplot显示多张图片在一个窗口,之前http://blog.csdn.net/qq_20823641/article/details/51910066这篇文章也提供了一种方法,可以参考一下,经过进一步学习,从简单到复杂,再一次认为subplot(),感觉越来越有意思,虽然不是最好的,但是是入门比较好的,其中很多部分都可以进行优化,以后会在精通部分进行展示,这里已入门为主。
一、版本1
<span style="font-size:18px;">int main(void) { vector<Mat> imgs(2); imgs[0] = imread("cm.png"); imgs[1] = imread("cm.png"); Mat dispImg; int x, y; x = imgs[0].cols; y = imgs[0].rows; int max; max = (x > y)? x: y; int dstsize=max; dispImg.create(Size(dstsize*(1+1)+100,dstsize), CV_8UC3); int nImg = (int)imgs.size(); for(int i=0;i<nImg;i++) { int m=20+i*max; int n=20; Mat imgROI = dispImg(Rect(m, n, (int)x, (int)y)); resize(imgs[i], imgROI, Size((int)x, (int)y)); } namedWindow("winName"); imshow("winName", dispImg); waitKey(); return 0; }</span>
二、版本2
<span style="font-size:18px;">int main(void) { vector<Mat> imgs(6); imgs[0] = imread("cm.png"); imgs[1] = imread("cm.png"); imgs[2] = imread("lina.png"); imgs[3] = imread("dr.png"); imgs[4] = imread("pom.png"); imgs[5] = imread("qop.png"); imshowMany("DOTA2_Hero", imgs); waitKey(); return 0; } void imshowMany(const std::string& _winName, const vector<Mat>& _imgs) { int nImg = (int)_imgs.size(); Mat dispImg; int size; int x, y; // w - Maximum number of images in a row // h - Maximum number of images in a column int w, h; // scale - How much we have to resize the image float scale; int max; if (nImg <= 0) { printf("Number of arguments too small....\n"); return; } else if (nImg > 12) { printf("Number of arguments too large....\n"); return; } else if (nImg == 1) { w = h = 1; size = 300; } else if (nImg == 2) { w = 2; h = 1; size = 300; } else if (nImg == 3 || nImg == 4) { w = 2; h = 2; size = 300; } else if (nImg == 5 || nImg == 6) { w = 3; h = 2; size = 200; } else if (nImg == 7 || nImg == 8) { w = 4; h = 2; size = 200; } else { w = 4; h = 3; size = 150; } dispImg.create(Size(100 + size*w, 60 + size*h), CV_8UC3); for (int i= 0, m=20, n=20; i<nImg; i++, m+=(20+size)) { x = _imgs[i].cols; y = _imgs[i].rows; max = (x > y)? x: y; scale = (float) ( (float) max / size ); if (i%w==0 && m!=20) { m = 20; n += 20+size; } Mat imgROI = dispImg(Rect(m, n, (int)(x/scale), (int)(y/scale))); resize(_imgs[i], imgROI, Size((int)(x/scale), (int)(y/scale))); } namedWindow(_winName); imshow(_winName, dispImg); }</span>
http://blog.csdn.net/yang_xian521/article/details/7915396
http://kanwoerzi.iteye.com/blog/1304073
三、Matlab辅助 matlab就一个subplot()就搞定了,参考这个最后给出了matlab的代码和图片http://blog.csdn.net/qq_20823641/article/details/51910066
Opencv图像识别从零到精通(11)---一个窗口多图显示
标签:
原文地址:http://blog.csdn.net/qq_20823641/article/details/51966819