opencv cvSplit cvMerge

/* Splits a multi-channel array into the set of single-channel arrays or
   extracts particular [color] plane */
CVAPI(void)  cvSplit( const CvArr* src, CvArr* dst0, CvArr* dst1,
                      CvArr* dst2, CvArr* dst3 );

作用:将多通道图像分离为单通道图像

src:多通道源图像

dst0、dst1、dst2、dst3:单通道结果图像

源图像和结果图像的大小相同,深度类型相同,仅有通道类型不同


##############################################################

/* Merges a set of single-channel arrays into the single multi-channel array
   or inserts one particular [color] plane to the array */
CVAPI(void)  cvMerge( const CvArr* src0, const CvArr* src1,
                      const CvArr* src2, const CvArr* src3,
                      CvArr* dst );

作用:将单通道图像合并为多通道图像

src0、src1、src2、src3:单通道源图像

dst:多通道结果图像

源图像和结果图像的大小相同,深度类型相同,仅有通道类型不同


###########################################################


程序:将图像lena.jpg分离为三个单通道图像c1、c2、c3,再将其合并为单通道图像(蓝、绿、红)

void splitAndMerge(void)
{
	IplImage *img=cvLoadImage("lena.jpg");
	IplImage *c1=cvCreateImage(cvGetSize(img), img->depth, 1);
	IplImage *c2=cvCreateImage(cvGetSize(img), img->depth, 1);
	IplImage *c3=cvCreateImage(cvGetSize(img), img->depth, 1);
	IplImage *rImg=cvCreateImage(cvGetSize(img), img->depth, 3);
	IplImage *gImg=cvCreateImage(cvGetSize(img), img->depth, 3);
	IplImage *bImg=cvCreateImage(cvGetSize(img), img->depth, 3);

	if (NULL == img || NULL == c1 || NULL == c2 || NULL == c3 || NULL == rImg || NULL == gImg || NULL == bImg)
		exit(0);

	cvSplit(img, c1, c2, c3, NULL);

	cvNamedWindow("img");
	cvShowImage("img", img);

	cvZero(rImg);
	cvZero(gImg);
	cvZero(bImg);
	cvMerge(c1, NULL, NULL, NULL, bImg);
	cvMerge(NULL, c2, NULL, NULL, gImg);
	cvMerge(NULL, NULL, c3, NULL, rImg);

	cvNamedWindow("rImg");
	cvNamedWindow("gImg");
	cvNamedWindow("bImg");
	cvShowImage("rImg", rImg);
	cvShowImage("gImg", gImg);
	cvShowImage("bImg", bImg);
	cvWaitKey(0);

	cvReleaseImage(&img);
	cvReleaseImage(&c1);
	cvReleaseImage(&c2);
	cvReleaseImage(&c3);
	
	cvReleaseImage(&rImg);
	cvReleaseImage(&gImg);
	cvReleaseImage(&bImg);

	cvDestroyAllWindows();
}




你可能感兴趣的:(opencv,图像处理)