获取图像像素点的概率分布图

获取图像像素点的概率分布,不细讲,看代码
#include  
#include  
#define cvGetHistValue_1D( hist, idx0 ) ((float*)(cvPtr1D( (hist)->bins, (idx0), 0 )) 
int main()
{
	int hist_size[] = { 16, 16, 16 };
	float range[] = { 0, 255 };
	float *ranges[] = { range };
	int i, j, bin_w;
	float max_value, min_value;
	int min_idx, max_idx;
	char *name[3] = { "b", "g", "r" };
	double mean = 0, variance = 0;
	IplImage* img = cvLoadImage("E:\\VS\\picture\\24.jpg", 1);
	IplImage *pImage = NULL;
	IplImage *pImg[3];
	pImg[0] = cvCreateImage(cvGetSize(img), 8, 1);
	pImg[1] = cvCreateImage(cvGetSize(img), 8, 1);
	pImg[2] = cvCreateImage(cvGetSize(img), 8, 1);
	cvSplit(img, pImg[0], pImg[1], pImg[2], NULL);
	pImage = cvCloneImage(img);

	CvRect rect = cvRect(0, 0, 500, 600);
	//cvSetImageROI(pImage, rect);

	//创建一个图像用来存放直方图  
	IplImage *histImage[3];
	CvHistogram *hist[3];
	for (j = 0; j<3; j++){
		histImage[j] = cvCreateImage(cvSize(320, 200), 8, 3);
		//cvZero(histImage[j]);  
		hist[j] = cvCreateHist(1, &hist_size[j], CV_HIST_ARRAY, ranges, 1);
		//计算直方图并作用到hist变量中  
		cvCalcHist(&pImg[j], hist[j], 0, NULL);
		//得到直方图的最值及标号  
		cvGetMinMaxHistValue(hist[j], &min_value, &max_value, &min_idx, &max_idx);
		//缩放其最大值和最小值让其融入图像  
		cvScale(hist[j]->bins, hist[j]->bins, ((double)histImage[j]->height) / max_value, 0);
		//设置所有的直方图的数值为255  
		cvSet(histImage[j], cvScalarAll(255), 0);
		//建一个比例因子  沿宽度释放  
		bin_w = cvRound((double)histImage[j]->width / hist_size[j]);
		mean = 0;
		for (i = 0; iheight),
				cvPoint((i + 1)*bin_w, histImage[j]->height - cvRound(cvGetReal1D(hist[j]->bins, i))), scalar, -1, 8, 0);

			float *bins = cvGetHistValue_1D(hist[j], i));//***
			//增加均值  
			mean += bins[0];
			//std::cout<

你可能感兴趣的:(直方图)