#include <opencv/cv.h>
#include <opencv/cvaux.h>
#include <opencv/cxcore.h>
#include <opencv/highgui.h>
#include <iostream>
using namespace std;
using namespace cv;
//三通道的灰度直方图
//int main( )
//{
// IplImage * src= cvLoadImage("baboon.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);
//}
//int main( )
//{
// 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 = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1);
// //计算灰度图像的一维直方图
// cvCalcHist(&gray_plane,gray_hist,0,0);
// //归一化直方图
// 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);
//}
//int main( )
//{
// IplImage * src= cvLoadImage("baboon.jpg");
//
// IplImage* hsv = cvCreateImage( cvGetSize(src), 8, 3 );
// IplImage* h_plane = cvCreateImage( cvGetSize(src), 8, 1 );
// IplImage* s_plane = cvCreateImage( cvGetSize(src), 8, 1 );
// IplImage* v_plane = cvCreateImage( cvGetSize(src), 8, 1 );
// IplImage* planes[] = { h_plane, s_plane };
//
// //H 分量划分为16个等级,S分量划分为8个等级
// int h_bins = 16, s_bins = 8;
// int hist_size[] = {h_bins, s_bins};
//
// // H 分量的变化范围
// float h_ranges[] = { 0, 180 };
//
// //S 分量的变化范围
// float s_ranges[] = { 0, 255 };
// float* ranges[] = { h_ranges, s_ranges };
//
// // 输入图像转换到HSV颜色空间
// cvCvtColor( src, hsv, CV_BGR2HSV );
// cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 );
//
// //创建直方图,二维, 每个维度上均分
// CvHistogram * hist = cvCreateHist( 2, hist_size, CV_HIST_ARRAY, ranges, 1 );
// // 根据H,S两个平面数据统计直方图
// cvCalcHist( planes, hist, 0, 0 );
//
// // 获取直方图统计的最大值,用于动态显示直方图
// float max_value;
// cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 );
//
//
// //设置直方图显示图像
// int height = 240;
// int width = (h_bins*s_bins*6);
// IplImage* hist_img = cvCreateImage( cvSize(width,height), 8, 3 );
// cvZero( hist_img );
//
// //用来进行HSV到RGB颜色转换的临时单位图像
// IplImage * hsv_color = cvCreateImage(cvSize(1,1),8,3);
// IplImage * rgb_color = cvCreateImage(cvSize(1,1),8,3);
// int bin_w = width / (h_bins * s_bins);
// for(int h = 0; h < h_bins; h++)
// {
// for(int s = 0; s < s_bins; s++)
// {
// int i = h*s_bins + s;
// // 获得直方图中的统计次数,计算显示在图像中的高度
// float bin_val = cvQueryHistValue_2D( hist, h, s );
// int intensity = cvRound(bin_val*height/max_value);
//
// // 获得当前直方图代表的颜色,转换成RGB用于绘制
// cvSet2D(hsv_color,0,0,cvScalar(h*180.f / h_bins,s*255.f/s_bins,255,0));
// cvCvtColor(hsv_color,rgb_color,CV_HSV2BGR);
// CvScalar color = cvGet2D(rgb_color,0,0);
//
// cvRectangle( hist_img, cvPoint(i*bin_w,height),
// cvPoint((i+1)*bin_w,height - intensity),
// color, -1, 8, 0 );
// }
// }
//
// cvNamedWindow( "Source", 1 );
// cvShowImage( "Source", src );
//
// cvNamedWindow( "H-S Histogram", 1 );
// cvShowImage( "H-S Histogram", hist_img );
//
// cvWaitKey(0);
//}
//书中的例子
int main(int argc, char** argv) {
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* planes[] = { r_plane, g_plane };
//将HSV图像分离到不同的通道中
cvSplit(src, b_plane, g_plane, r_plane, 0);
// 生成二维直方图数据结构
int r_bins = 256, b_bins = 256;
CvHistogram* hist;
{
int hist_size[] = { r_bins, b_bins };
float r_ranges[] = { 0, 255 }; // hue is [0,180]
float b_ranges[] = { 0, 255 };
float* ranges[] = { r_ranges, b_ranges };
hist = cvCreateHist(2, hist_size, CV_HIST_ARRAY, ranges, 1);
}
//计算一张或多张单通道图像image(s) 的直方图
cvCalcHist(planes, hist, 0, 0);
//创建可视化显示直方图内容的图像
int scale = 2;
IplImage* hist_img = cvCreateImage(cvSize(r_bins * scale, b_bins * scale), 8, 3);
cvZero(hist_img);
// 以灰色网格填充直方图
float max_value = 0;
cvGetMinMaxHistValue(hist, 0, &max_value, 0, 0); //发现最大和最小直方块
#define cvQueryHistValue_2D( hist, idx0, idx1 ) cvGetReal2D( (hist)->bins, (idx0), (idx1) )
for (int h = 0; h < r_bins; h++) {
for (int s = 0; s < b_bins; s++) {
float bin_val = cvQueryHistValue_2D(hist, h, s); //查询直方块的值
int intensity = cvRound(bin_val * 255 / max_value);
cvRectangle(hist_img,
cvPoint(h*scale, s*scale), cvPoint((h + 1)*scale - 1, (s + 1)*scale - 1),
CV_RGB(intensity, intensity, intensity), CV_FILLED);
}
}
cvNamedWindow("Source", 1);
cvShowImage("Source", src);
cvNamedWindow("H-S Histogram", 1);
cvShowImage("H-S Histogram", hist_img);
cvWaitKey(0);
}
opencv画直方图
最新推荐文章于 2024-06-17 22:48:33 发布