图像直方图实现

/********************一维直方图实现*************************************/
#include <tchar.h> 
#include <cv.h>
#include <highgui.h>
#include <opencv/cv.h>
#include <opencv/cvaux.h>
#include <opencv/cxcore.h>
#include <opencv/highgui.h>
#include <iostream>
using namespace std;
using namespace cv;

#define cvQueryHistValue_1D( hist, idx0 ) \
	((float)cvGetReal1D((hist)->bins, (idx0)))


int _tmain(int argc, _TCHAR* argv[])
{
	IplImage * src = cvLoadImage("baboon.jpg");
	IplImage* gray_plane = cvCreateImage(cvGetSize(src), 8, 1);
	cvCvtColor(src, gray_plane, CV_BGR2GRAY);

	int hist_size = 256;    //直方图尺寸 
	int hist_height = 256;
	float range[] = { 0, 255 };  //灰度级的范围 
	float* ranges[] = { range };  //创建一维直方图,统计图像在[0 255]像素的均匀分布 
	CvHistogram* gray_hist;
	gray_hist = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);  //计算灰度图像的一维直方图 

	printf("dim:%d\n", gray_hist->mat.dims);               //访问直方图
	printf("num of bin:%d\n", gray_hist->mat.dim[0].size);
	printf("low of dim:%f\n", gray_hist->thresh[0][0]);
	printf("up  of dim:%f\n", gray_hist->thresh[0][1]);


	cvCalcHist(&gray_plane, gray_hist, 0, 0);
	for (int i = 0; i < gray_hist->mat.dim->size; i++){  //访问直方图
		printf("%d_bin:%f--", i + 1, cvQueryHistValue_1D(gray_hist, i));
		printf("%d_bin:%f\n", i + 1, *(gray_hist->mat.data.fl + i));
	}

	cvNormalizeHist(gray_hist, 1.0);  	//归一化直方图 

	int scale = 2; 	//创建一张一维直方图的“图”,横坐标为灰度级,纵坐标为像素个数(*scale) 
	IplImage* hist_image = cvCreateImage(cvSize(hist_size*scale, hist_height), 8, 3);
	cvZero(hist_image);

	float max_value = 0;
	cvGetMinMaxHistValue(gray_hist, 0, &max_value, 0, 0);  //统计直方图中的最大直方块 

	for (int i = 0; i<hist_size; i++){
		float bin_val = cvQueryHistValue_1D(gray_hist, i);	     //像素i的概率 
		int intensity = cvRound(bin_val*hist_height / max_value);  //要绘制的高度 
		cvRectangle(hist_image, cvPoint(i*scale, hist_height - 1),
			cvPoint((i + 1)*scale - 1, hist_height - intensity), CV_RGB(255, 255, 255));
	}

	cvNamedWindow("GraySource", 1);
	cvShowImage("GraySource", gray_plane);
	cvNamedWindow("H-S Histogram", 1);
	cvShowImage("H-S Histogram", hist_image);

	cvWaitKey(0);
	return 0;
}



/****************三通道的灰度直方图****************************/
#include <opencv/cv.h>
#include <opencv/cvaux.h>
#include <opencv/cxcore.h>
#include <opencv/highgui.h>
#include <iostream>
using namespace std;
using namespace cv;

#define cvQueryHistValue_1D( hist, idx0 ) \
	((float)cvGetReal1D((hist)->bins, (idx0)))
#define cvCvtPixToPlane cvSplit;


int main( )
{
	IplImage * src= cvLoadImage("pic.jpg");
	IplImage* r_plane = cvCreateImage(cvGetSize(src),8,1);
	IplImage* g_plane = cvCreateImage(cvGetSize(src),8,1);
	IplImage* b_plane = cvCreateImage(cvGetSize(src),8,1);
	IplImage* gray_plane = cvCreateImage(cvGetSize(src),8,1);
	cvCvtPixToPlane(src,b_plane,g_plane,r_plane,0);
	cvCvtColor(src,gray_plane,CV_BGR2GRAY);

	int hist_size = 256;
	int hist_height = 256;
	float range[] = {0,255};
	float* ranges[]={range};
	CvHistogram* r_hist = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1);
	CvHistogram* g_hist = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1);
	CvHistogram* b_hist = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1);
	CvHistogram* gray_hist = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1);


	cvCalcHist(&gray_plane,gray_hist,0,0);
	cvNormalizeHist(gray_hist,1.0);
	cvCalcHist(&r_plane,r_hist,0,0);
	cvNormalizeHist(r_hist,1.0);
	cvCalcHist(&g_plane,g_hist,0,0);
	cvNormalizeHist(g_hist,1.0);
	cvCalcHist(&b_plane,b_hist,0,0);
	cvNormalizeHist(b_hist,1.0);
	double scale = 1.5;
	IplImage* hist_image = cvCreateImage(cvSize(hist_size*scale*2,hist_height*2),8,3);
	cvZero(hist_image);


	//创建直方图,一维, 每个维度上均分 
	float max_value = 0;
	cvGetMinMaxHistValue(r_hist, 0,&max_value,0,0);
	for(int i=0;i<hist_size;i++)
	{
		float bin_val = cvQueryHistValue_1D(r_hist,i);
		int intensity = cvRound(bin_val*hist_height/max_value);
		cvRectangle(hist_image,
			cvPoint(i*scale,hist_height-1),
			cvPoint((i+1)*scale - 1, hist_height - intensity),
			CV_RGB(255,0,0));
	}


	cvGetMinMaxHistValue(g_hist, 0,&max_value,0,0);
	for(int i=0;i<hist_size;i++)
	{
		float bin_val = cvQueryHistValue_1D(g_hist,i);
		int intensity = cvRound(bin_val*hist_height/max_value);
		cvRectangle(hist_image,
			cvPoint(i*scale+hist_size*scale,hist_height-1),
			cvPoint((i+1)*scale - 1+hist_size*scale, hist_height - intensity),
			CV_RGB(0,255,0));
	}

	cvGetMinMaxHistValue(b_hist, 0,&max_value,0,0);
	for(int i=0;i<hist_size;i++)
	{
		float bin_val = cvQueryHistValue_1D(b_hist,i);
		int intensity = cvRound(bin_val*hist_height/max_value);
		cvRectangle(hist_image,
			cvPoint(i*scale,hist_height-1+hist_height),
			cvPoint((i+1)*scale - 1, hist_height - intensity+hist_height),
			CV_RGB(0,0,255));
	}

	cvGetMinMaxHistValue(gray_hist, 0,&max_value,0,0);
	for(int i=0;i<hist_size;i++)
	{
		float bin_val = cvQueryHistValue_1D(gray_hist,i);
		int intensity = cvRound(bin_val*hist_height/max_value);
		cvRectangle(hist_image,
			cvPoint(i*scale+hist_size*scale,hist_height-1+hist_height),
			cvPoint((i+1)*scale - 1+hist_size*scale, hist_height - intensity+hist_height),
			CV_RGB(100,100,100));
	}


	cvNamedWindow( "Source", 1 );
	cvShowImage( "Source", src );

	cvNamedWindow( "H-S Histogram", 1 );
	cvShowImage( "H-S Histogram", hist_image );

	cvWaitKey(0);
}





                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值