1. function prototype:
void cvSplit(const CvArr* src,CvArr *dst0,CvArr *dst1, CvArr *dst2, CvArr *dst3);
In some cases, it is not convenient to process multi-channel images. In this case, you can use cvsplit () to copy each channel to multiple single-channel images. If necessary, cvsplit () the function copies each channel of SRC (source multi-channel image) to dst0, dst1, dst2, and dst3. The target image must match the source image in size and data type. Of course, it should also be a single-channel image.
If the source image has fewer than four channels (this often happens), the unnecessary target parameter passed to cvsplit () can be set to null.
The following procedure is the core part of the procedure for converting a multi-channel image to a single-channel image.
Image1=cvLoadImage("DarkClouds.jpg",1); RedImage=cvCreateImage(cvGetSize(Image1),IPL_DEPTH_8U,1); GreenImage=cvCreateImage(cvGetSize(Image1),IPL_DEPTH_8U,1); BlueImage=cvCreateImage(cvGetSize(Image1),IPL_DEPTH_8U,1); cvSplit(Image1,BlueImage,GreenImage,RedImage,0);
Now we write a complete program to convert an image into a single channel image and display it.
#include "cv.h"#include "highgui.h"int main(void){IplImage *image1 = cvLoadImage("e:\\opencv\\image\\Fruits.jpg",1);IplImage *redImage = cvCreateImage(cvGetSize(image1),IPL_DEPTH_8U,1);IplImage *greenImage = cvCreateImage(cvGetSize(image1),IPL_DEPTH_8U,1);IplImage *blueImage = cvCreateImage(cvGetSize(image1),IPL_DEPTH_8U,1);cvSplit(image1,redImage,greenImage,blueImage,NULL);cvNamedWindow("hello",CV_WINDOW_AUTOSIZE);cvShowImage("hello",image1);cvWaitKey(0);cvShowImage("hello",redImage);cvWaitKey(0);cvShowImage("hello",greenImage);cvWaitKey(0);cvShowImage("hello",blueImage);cvWaitKey(0);return 0;}
Lab result diagram: