//时间:2015年6月17日
//功能:画图像的直方图
//函数:CvHistogram * cvCreateHist(int dims , int * sizes , int type , float ** ranges = NULL , int uniform = 1)
// void cvCalcHist(IplImage ** inage , CvHistgram * hist , int sccumulate = 0 , conat * mask = NULL)
#include <iostream>
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include <afx.h>
using namespace std;
int main()
{
const char * m_PicName = "华山论剑.jpg";
const char * m_RoiPicName = "华山论剑.jpg";
IplImage * mp_Pic = cvLoadImage( m_PicName , CV_LOAD_IMAGE_UNCHANGED );
//载入检测
if ( 0 == mp_Pic )
{
cout<<"载入图片失败!"<<endl;
return -1;
}
int m_hist_size = 255;
float m_range_0[] = {0,255};
float * mp_ranges = { m_range_0 };
float m_max_value = 0 , m_min_value = 0;
int m_max_idx = 0 , m_min_idx = 0;
double m_mean = 0 , m_variance = 0;
IplImage * mp_Pic_Gary = NULL;
//将读入图像转换为灰度图像
mp_Pic_Gary = cvCreateImage( cvSize (mp_Pic->width , mp_Pic->height) , IPL_DEPTH_8U , 1);
cvCvtColor( mp_Pic , mp_Pic_Gary , CV_RGB2GRAY );
//设置感兴趣区域
cvSetImageROI( mp_Pic_Gary , cvRect(500,500,1000,1000) );
//创建图像存放直方图和直方图结构体
IplImage * mp_Pic_Hist = cvCreateImage( cvSize(320 , 200) , 8 , 1 );
CvHistogram * m_Hist = cvCreateHist( 1 , &m_hist_size , CV_HIST_ARRAY , &mp_ranges , 1 );
//统计感兴趣区域的直方图,并计算最大值和最小值坐标
cvCalcHist( &mp_Pic_Gary , m_Hist , 0 , NULL );
cvGetMinMaxHistValue( m_Hist , &m_min_value , &m_max_value , &m_min_idx , &m_max_idx );
//缩放最大值和最小值以融入图像内
cvScale(m_Hist->bins , m_Hist->bins , ((double)mp_Pic_Hist->height)/m_max_value , 0);
cvSet( mp_Pic_Hist , cvScalarAll(255) , 0 );
//建立一个比例因子,按比例缩放
int m_bin_w = cvRound( (double)mp_Pic_Hist->width/m_hist_size );
//把直方图划入图像中,并计算均值
for ( int i = 0; i<m_hist_size; i++ )
{
cvRectangle( mp_Pic_Hist , cvPoint( i * m_bin_w , mp_Pic_Hist->height) , cvPoint( (i+1)*m_bin_w , mp_Pic_Hist->height - cvRound( cvGetReal1D(m_Hist->bins , i) ) ) , cvScalarAll(0) , -1 , 8 , 0 );
float * m_bins = cvGetHistValue_1D( m_Hist , i );
m_mean += m_bins[0];
}
m_mean /= m_hist_size;
//计算方差
for ( int i = 0; i<m_hist_size; i++ )
{
float * m_bins = cvGetHistValue_1D( m_Hist , i );
m_variance += ( m_bins[0] - m_mean ) * ( m_bins[0] - m_mean );
}
m_variance /= m_hist_size;
cvNamedWindow(" 原图 " , 0);
cvShowImage( " 原图 " , mp_Pic );
cvNamedWindow(" 灰度图 " , 0);
cvShowImage( " 灰度图 " , mp_Pic_Gary );
CvFont * mp_Font = new CvFont;
cvInitFont( mp_Font , CV_FONT_HERSHEY_SIMPLEX , 0.8f , 0.8f , 0 , 2 );
char m_Result[] = "图形直方图矩阵的均值和方差分别为:";
//CString m_Str;
//m_Str.Format( "%f" , m_mean );
//m_Result += m_Str;
cvPutText( mp_Pic_Hist , m_Result , cvPoint(80,80) , mp_Font , CV_RGB(255 , 0 , 0) );
cvNamedWindow(" 直方图 " , 0);
cvShowImage( " 直方图 " , mp_Pic_Hist );
cvWaitKey( 0 );
cvReleaseImage( &mp_Pic );
cvReleaseImage( &mp_Pic_Gary );
cvReleaseImage( &mp_Pic_Hist );
cvDestroyAllWindows;
return 0;
}
//功能:画图像的直方图
//函数:CvHistogram * cvCreateHist(int dims , int * sizes , int type , float ** ranges = NULL , int uniform = 1)
// void cvCalcHist(IplImage ** inage , CvHistgram * hist , int sccumulate = 0 , conat * mask = NULL)
#include <iostream>
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include <afx.h>
using namespace std;
int main()
{
const char * m_PicName = "华山论剑.jpg";
const char * m_RoiPicName = "华山论剑.jpg";
IplImage * mp_Pic = cvLoadImage( m_PicName , CV_LOAD_IMAGE_UNCHANGED );
//载入检测
if ( 0 == mp_Pic )
{
cout<<"载入图片失败!"<<endl;
return -1;
}
int m_hist_size = 255;
float m_range_0[] = {0,255};
float * mp_ranges = { m_range_0 };
float m_max_value = 0 , m_min_value = 0;
int m_max_idx = 0 , m_min_idx = 0;
double m_mean = 0 , m_variance = 0;
IplImage * mp_Pic_Gary = NULL;
//将读入图像转换为灰度图像
mp_Pic_Gary = cvCreateImage( cvSize (mp_Pic->width , mp_Pic->height) , IPL_DEPTH_8U , 1);
cvCvtColor( mp_Pic , mp_Pic_Gary , CV_RGB2GRAY );
//设置感兴趣区域
cvSetImageROI( mp_Pic_Gary , cvRect(500,500,1000,1000) );
//创建图像存放直方图和直方图结构体
IplImage * mp_Pic_Hist = cvCreateImage( cvSize(320 , 200) , 8 , 1 );
CvHistogram * m_Hist = cvCreateHist( 1 , &m_hist_size , CV_HIST_ARRAY , &mp_ranges , 1 );
//统计感兴趣区域的直方图,并计算最大值和最小值坐标
cvCalcHist( &mp_Pic_Gary , m_Hist , 0 , NULL );
cvGetMinMaxHistValue( m_Hist , &m_min_value , &m_max_value , &m_min_idx , &m_max_idx );
//缩放最大值和最小值以融入图像内
cvScale(m_Hist->bins , m_Hist->bins , ((double)mp_Pic_Hist->height)/m_max_value , 0);
cvSet( mp_Pic_Hist , cvScalarAll(255) , 0 );
//建立一个比例因子,按比例缩放
int m_bin_w = cvRound( (double)mp_Pic_Hist->width/m_hist_size );
//把直方图划入图像中,并计算均值
for ( int i = 0; i<m_hist_size; i++ )
{
cvRectangle( mp_Pic_Hist , cvPoint( i * m_bin_w , mp_Pic_Hist->height) , cvPoint( (i+1)*m_bin_w , mp_Pic_Hist->height - cvRound( cvGetReal1D(m_Hist->bins , i) ) ) , cvScalarAll(0) , -1 , 8 , 0 );
float * m_bins = cvGetHistValue_1D( m_Hist , i );
m_mean += m_bins[0];
}
m_mean /= m_hist_size;
//计算方差
for ( int i = 0; i<m_hist_size; i++ )
{
float * m_bins = cvGetHistValue_1D( m_Hist , i );
m_variance += ( m_bins[0] - m_mean ) * ( m_bins[0] - m_mean );
}
m_variance /= m_hist_size;
cvNamedWindow(" 原图 " , 0);
cvShowImage( " 原图 " , mp_Pic );
cvNamedWindow(" 灰度图 " , 0);
cvShowImage( " 灰度图 " , mp_Pic_Gary );
CvFont * mp_Font = new CvFont;
cvInitFont( mp_Font , CV_FONT_HERSHEY_SIMPLEX , 0.8f , 0.8f , 0 , 2 );
char m_Result[] = "图形直方图矩阵的均值和方差分别为:";
//CString m_Str;
//m_Str.Format( "%f" , m_mean );
//m_Result += m_Str;
cvPutText( mp_Pic_Hist , m_Result , cvPoint(80,80) , mp_Font , CV_RGB(255 , 0 , 0) );
cvNamedWindow(" 直方图 " , 0);
cvShowImage( " 直方图 " , mp_Pic_Hist );
cvWaitKey( 0 );
cvReleaseImage( &mp_Pic );
cvReleaseImage( &mp_Pic_Gary );
cvReleaseImage( &mp_Pic_Hist );
cvDestroyAllWindows;
return 0;
}