char* srcimg = "fj.jpg";
char* smoothtitle = "SmmonthImg";
IplImage* psrcImg = cvLoadImage( srcimg );
if( psrcImg == NULL ) return;
IplImage* pgray_plane = cvCreateImage( cvGetSize( psrcImg ),8,1 );
if( pgray_plane == NULL ) return;
cvCvtColor( psrcImg,pgray_plane,CV_BGR2GRAY );
int hist_size = 256;
int hist_height = 256;
float range[] = { 0,255 };
float* ranges[]={range};
CvHistogram *pHist = cvCreateHist( 1,&hist_size,CV_HIST_ARRAY,ranges,1 );
cvCalcHist( &pgray_plane,pHist );
cvNormalizeHist( pHist,1.0 );
int scale = 1;
IplImage* hist_image = cvCreateImage( cvSize( hist_size*scale,hist_height ),8,3 );
if( hist_image == NULL ) return;
cvZero( hist_image );
float max_value = 0;
cvGetMinMaxHistValue( pHist,0,&max_value );
//分别将每个直方块的值绘制到图中
for(int i=0;i<hist_size;i++)
{
float bin_val = cvQueryHistValue_1D( pHist,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",psrcImg);
cvNamedWindow( "H-S Histogram", 1 );
cvShowImage( "H-S Histogram", hist_image );
cvWaitKey( 0 );
opencv 直方图
最新推荐文章于 2025-04-29 12:39:01 发布