本文使用了ROS Kinetic版本集成的的opencv库,实现了多通道3原色R,G,B分量单通道图的提取。
#include <opencv2/highgui.hpp> using namespace cv; int main(int argc,char **argv) - { | IplImage *img=cvLoadImage("/home/user/Pictures/opencvlogo.bmp",1); /*cvCreateImage分配图像头和数据空间*/ | IplImage *channel_r=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1); | IplImage *channel_g=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1); | IplImage *channel_b=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1); | IplImage *img_r=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3); | IplImage *img_g=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3); | IplImage *img_b=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3); | /*提取多通道原图,并填充到单通道图*/ | cvSplit(img,channel_b,channel_g,channel_r,NULL); | /*合并单通道成多通道*/ | cvMerge(channel_b,0,0,0,img_b); | cvMerge(0,channel_g,0,0,img_g); | cvMerge(0,0,channel_r,0,img_r); | cvNamedWindow("ImageOrigin",CV_WINDOW_AUTOSIZE); | cvNamedWindow("Image_R",CV_WINDOW_AUTOSIZE); | cvNamedWindow("Image_G",CV_WINDOW_AUTOSIZE); | cvNamedWindow("Image_B",CV_WINDOW_AUTOSIZE); | cvShowImage("ImageOrigin",img); | cvShowImage("Image_R",img_r); | cvShowImage("Image_G",img_g); | cvShowImage("Image_B",img_b); | cvWaitKey(0); | cvSaveImage("./img_red.bmp",img_r); | cvSaveImage("./img_green.bmp",img_g); | cvSaveImage("./img_blue.bmp",img_b); | | cvWaitKey(0); | cvReleaseImage(&img); | cvReleaseImage(&img_r); | cvReleaseImage(&img_g); | cvReleaseImage(&img_b); | cvReleaseImage(&channel_r); | cvReleaseImage(&channel_g); | cvReleaseImage(&channel_b); | cvDestroyAllWindows(); | | return 0; | }
1)原图如下:
2)处理后提取的各个单通道图如下:
1)IplImage* cvLoadImage( const char* filename, int flags );
2)IplImage* cvCreateImage( CvSize size, int depth, int channels )
3)void cvSplit( const CvArr* src, CvArr* dst0, CvArr* dst1, CvArr* dst2, CvArr* dst3 )
函数将复制src的各个通道到图像dst0,dst1,dst2和dst3中。如果源图像少于4个通道的情况下,那么传递给cvSplit()的不必要的目标参数可设置为NULL。
使用时注意,源图像const CvArr* src应为多通道图像;目标图像CvArr* dst0, CvArr* dst1, CvArr* dst2,和CvArr* dst3应为单通道图像,以上图像的size应该一样大。
4)void cvMerge(const CvArr* src0, const CvArr* src1, const CvArr* src2, const CvArr* src3, CvArr* dst)
合并单通道图成多通道图显示。
http://blog.csdn.net/leixiaohua1020/article/details/19020903