OpenCV提取显示一张图片(或者视频)的R,G,B颜色分量

时间:2022-07-20 15:52:51

使用OpenCV可以提分别提取显示一张图片(或者视频)的R,G,B颜色分量。效果如下。

原图:

OpenCV提取显示一张图片(或者视频)的R,G,B颜色分量

R:

OpenCV提取显示一张图片(或者视频)的R,G,B颜色分量

G:

OpenCV提取显示一张图片(或者视频)的R,G,B颜色分量

B:

OpenCV提取显示一张图片(或者视频)的R,G,B颜色分量


示例代码如下,貌似很久以前网上找的的,逻辑很清晰,就是把R,G,B三个分量分开,然后显示出来,就不注释了。


#include "cv.h"
#include "highgui.h"
#include <cxcore.h>


void main(int argc,char **argv)
{
IplImage *img=cvLoadImage("test.bmp",1);
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);

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);
cvDestroyAllWindows();
}