OpenCV分離RGB像素
#include<highgui.h>
#include<cxcore.h>
int main(){
IplImage *img=cvLoadImage("Baboon.jpg",1);
//void cvSplit( const CvArr* src, CvArr* dst0, CvArr* dst1, CvArr* dst2, CvArr* dst3 );
//IplImage *r,*g,*b,*alpha;
int num=3;
int i=0;
char *window[3]={"b","g","r"};
char *name[3]={"b.jpg","g.jpg","r.jpg"};
IplImage *subGrayImage[3];
IplImage *subColorImage[3];
for(i=0;i<3;i++)
{
subGrayImage[i]=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
subColorImage[i]=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3);
}
cvSplit(img,subGrayImage[0],subGrayImage[1],subGrayImage[2],0);
cvNamedWindow("example",1);
cvShowImage("example", img);
for(i=0;i<3;i++)cvZero(subColorImage[i]);
cvMerge(subGrayImage[0],0,0,0,subColorImage[0]);
cvMerge(0,subGrayImage[0],0,0,subColorImage[1]);
cvMerge(0,0,subGrayImage[0],0,subColorImage[2]);
for(i=0;i<3;i++){
cvSaveImage(name[i],subColorImage[i],NULL);
}
for(i=0;i<3;i++){
cvNamedWindow(window[i],1);
cvShowImage(window[i], subColorImage[i]);
}
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow("example");
for(i=0;i<3;i++){
cvReleaseImage(&subGrayImage[i]);
cvReleaseImage(&subColorImage[i]);
cvDestroyWindow(window[i]);
}
}