获得RGB图像三种颜色分量并进行条件筛选

#include "highgui.h"  
#include"cv.h"  


using namespace std;
using namespace cv;


int main(int argc, char *argv[])
{
	IplImage *img = cvLoadImage("D:\\huo.jpg");
	
	//R、S、B的阈值Rt,St,Bt
	int Rt = 125;
	int St = 55;
	int Bt = 9;

	//获得BGR三种颜色分量
	//IplImage* img1 = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 1);
	//IplImage* img1 = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 3);
	uchar* data = (uchar *)img->imageData;

	int step = img->widthStep / sizeof(uchar);
	int channels = img->nChannels;
	uchar *b_BGR = new uchar[307200], *g_BGR = new uchar[307200], *r_BGR = new uchar[307200];
	for (int i = 0; iheight; i++)
	for (int j = 0; jwidth; j++)
	{
		*b_BGR = data[i*step + j*channels + 0];
		*g_BGR = data[i*step + j*channels + 1];
		*r_BGR = data[i*step + j*channels + 2];
		if (*r_BGR < 200)
		{
			data[i*step + j*channels + 0] = 0;
			data[i*step + j*channels + 1] = 0;
			data[i*step + j*channels + 2] = 0;
		}
		else
		{
			data[i*step + j*channels + 0] = 255;
			data[i*step + j*channels + 1] = 255;
			data[i*step + j*channels + 2] = 255;
		}
		
	}
	cvShowImage("img", img);
	cvWaitKey(0);
	return 0;
}

注意:

1:uchar是一种无符号整型数据,所以可以直接用数字进行对比筛选

2:data[i*step + j*channels + 0]     //BRG三色分量相应位置的颜色数据

3:二值化一定要把三个通道的数据都设为0或255,否则其他两个通道的值不会发生变化

 
  
图1:原图(带有火焰的图像)
 
  
图2:对火焰R分量进行筛选之后的图像

你可能感兴趣的:(opencv,learning,note,opencv,函数,实例,应用)