OpenCV 图像合并
opencv中的cvShowImage函数只能显示单张图像,如果我们需要在一个窗口中显示多张图像,该怎么办?一种解决办法是,我们先将多张图像合并成一张图像,然后调用cvShowImage,达到一个窗口显示多张图像的效果。
下面是基于opencv实现的水平方向的图像合并,两张图像大小相等,参数一为rgb三通道图像,参数二为单通道图像。
IplImage *CombineImage(IplImage *rgbImg, IplImage *greyImg){if (rgbImg == NULL || greyImg == NULL)return NULL;int width = rgbImg->width;int height = rgbImg->height;IplImage *combImg = cvCreateImage(cvSize(width * 2, height), 8, 3);IplImage *cvtImg = cvCreateImage(cvSize(width, height), 8, 3);cvCvtColor(greyImg, cvtImg, CV_GRAY2BGR);for (int row = 0; row < combImg->height; row++){for (int col = 0; col < combImg->width; col++){if (col < width){CV_IMAGE_ELEM(combImg, unsigned char, row, 3 * col) = CV_IMAGE_ELEM(rgbImg, unsigned char, row, 3 * col);CV_IMAGE_ELEM(combImg, unsigned char, row, 3 * col + 1) = CV_IMAGE_ELEM(rgbImg, unsigned char, row, 3 * col + 1);CV_IMAGE_ELEM(combImg, unsigned char, row, 3 * col + 2) = CV_IMAGE_ELEM(rgbImg, unsigned char, row, 3 * col+ 2);}else{CV_IMAGE_ELEM(combImg, unsigned char, row, 3 * col) = CV_IMAGE_ELEM(cvtImg, unsigned char, row, 3 * (col-width));CV_IMAGE_ELEM(combImg, unsigned char, row, 3 * col + 1) = CV_IMAGE_ELEM(cvtImg, unsigned char, row, 3 * (col - width) + 1);CV_IMAGE_ELEM(combImg, unsigned char, row, 3 * col + 2) = CV_IMAGE_ELEM(cvtImg, unsigned char, row, 3 * (col - width) + 2);}}}cvReleaseImage(&cvtImg);return combImg;}