欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

OpenCV提取显示一张图片的R,G,B颜色分量调试记录

程序员文章站 2022-07-14 16:37:19
...

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

// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "cv.h"  
#include "highgui.h" 
//#include <opencv2/highgui_c.h> 
#include <cxcore.h> 
#include <opencv2/opencv.hpp>

#pragma warning(disable:4996)

int _tmain(int argc, _TCHAR* 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();
}

遇到下面问题:

OpenCV提取显示一张图片的R,G,B颜色分量调试记录

缺少相应的dll动态库文件,加入相应的动态库

OpenCV提取显示一张图片的R,G,B颜色分量调试记录

输出如下错误:

OpenCV提取显示一张图片的R,G,B颜色分量调试记录

缺少bmp文件加入相应的文件

程序运行结果如下:

OpenCV提取显示一张图片的R,G,B颜色分量调试记录

r

OpenCV提取显示一张图片的R,G,B颜色分量调试记录

g:

OpenCV提取显示一张图片的R,G,B颜色分量调试记录

b:

OpenCV提取显示一张图片的R,G,B颜色分量调试记录

相关标签: rgb