首页 > 代码库 > openCV构造直方图

openCV构造直方图

学习openCV上的一个例子,根据输入的图像构造色相饱和度的直方图

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


void main()
{
	
	
	//以原始通道数读取图片1
	IplImage* img1 = cvLoadImage("D://vc6.0//MSDev98//MyProjects//MachineVision//TestPic//PeppersRGB.bmp",CV_LOAD_IMAGE_UNCHANGED);
	//创建图片1的一个空副本
	IplImage* img2 = cvCreateImage(cvGetSize(img1), 8, 3);

	//从RGB色彩空间转换到HSV色彩空间
	cvCvtColor(img1, img2, CV_BGR2HSV);

	IplImage* h_plane = cvCreateImage(cvGetSize(img1), 8, 1);
	IplImage* s_plane = cvCreateImage(cvGetSize(img1), 8, 1);
	IplImage* v_plane = cvCreateImage(cvGetSize(img1), 8, 1);
	IplImage* planes[] = {h_plane, s_plane};

	//将HSV图像分成H,S,V三个通道分别操作
	cvCvtPixToPlane(img2, h_plane, s_plane, v_plane, 0);


	//建立直方图
	//确定直方图中方块的数量
	int h_bins = 30;
	int s_bins = 32;

	CvHistogram* hist;
	int		hist_size[] = {h_bins, s_bins};

	//设定h和s直方图横坐标的范围
	float	h_ranges[] = {0, 180};
	float	s_ranges[] = {0, 255};
	float*  ranges[] = {h_ranges, s_ranges};

	//创建一个二维直方图,1代表均匀分割
	hist = cvCreateHist(2, hist_size, CV_HIST_ARRAY, ranges, 1);


	//计算直方图
	cvCalcHist(planes, hist, 0, 0);

	//归一化
	cvNormalizeHist(hist, 1.0);

	int scale = 10;
	IplImage* histogram = cvCreateImage(cvSize(h_bins * scale, s_bins * scale), 8, 3);

	cvZero(histogram);

	float max_value = http://www.mamicode.com/0;>



openCV构造直方图