/*一般情况下,我们创建一个窗口,窗口里显示一张图片,为了对比图像,多张图像显示在一个窗口上方便很多。
之前见过他人博客中的原创,以下有一些改动。
以下是多张图片(三通道 ,或者改变参数->单通道)在一个窗口显示的函数:*/
// ==========一个窗口显示多幅图片(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;
}
//
//