opencv单个窗口中显示多个图像

在一个窗口显示多个图像子窗口。函数showManyImages。

#include 
#include 
#include 



// 单窗口显示多幅图像的函数
void showMultiImages(char* title, int nArgs, ...) 
{

	Mat DispImage;
	int size;    
	int ind;      
	int x, y;    
	int w, h;    
	int r, c;

	float scale;

	int max;

	int space;

	if(nArgs <= 0) {
		printf("Number of arguments too small..../n");
		return;
	}
	else if(nArgs > 12) {
		printf("Number of arguments too large..../n");
		return;
	}
	else if (nArgs == 1) {
		r = c = 1;
		size = 300;
	}
	else if (nArgs == 2) {
		r = 2; c = 1;
		size = 300;
	}
	else if (nArgs == 3 || nArgs == 4) {
		r = 2; c = 2;
		size = 300;
	}
	else if (nArgs == 5 || nArgs == 6) {
		r = 3; c = 2;
		size = 200;
	}
	else if (nArgs == 7 || nArgs == 8) {
		r = 4; c = 2;
		size = 200;
	}
	else {
		r = 4; c = 3;
		size = 150;
	}

	DispImage = Mat( Size(20*(r) + size*r, 20*(c) + size*c), CV_8UC3 );

	va_list args;
	va_start(args, nArgs);

	space = 20;
	for (ind = 0, x = space, y = space; ind < nArgs; ind++, x += (space + size)) {

		Mat img = va_arg(args, Mat);

		if(img.data == NULL) {
			printf("Invalid arguments");
			return;
		}

		w = img.cols;
		h = img.rows;

		max = (w > h)? w: h;

		scale = (float) ( (float) max / size );

		if( ind % r == 0 && x!= space) {
			x  = space;
			y += space + size;
		}

		Mat roi = Mat(DispImage,Rect(x, y, (int)( w/scale ), (int)( h/scale )));
		resize(img, roi,Size((int)( w/scale ), (int)( h/scale )));
	}

	imshow( title, DispImage);

	waitKey(10);
	va_end(args);

}


int main( int argc, char** argv ) 
{
	Mat pic = imread("1.bmp");
	showMultiImages("img",7,pic,pic,pic,pic,pic,pic,pic);
}


你可能感兴趣的:(opencv)