很久沒更新blog裏,看到OpenCV官網作的愈來愈好,內心也是很高興的,真有些衝動未來加入到這個組織裏作些事。估計2.4.3要在國慶左右跟你們見面,讓咱們多期待一下吧。算法
閒話少說,今天不介紹複雜的算法了,來個簡單的,你們寫文章作圖常常用Matlab,在Matlab裏常常在一個窗口裏打開多幅圖片。遺憾的是OpenCV沒有集成這樣的功能,但這難不倒你們,讓我試試用ROI來解決這個問題。.net
沒啥好講的,上代碼好了:code
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); }
附上效果圖一張:blog
工程的下載連接爲http://download.csdn.net/detail/yang_xian521/4531610圖片