感觉直方图很重要,所以先挑着看下:
void calcHist( const Mat* images, int nimages,
const int* channels,
InputArray mask,//掩码
OutputArray hist, //输出为二维数组
int dims, //维数
const int* histSize,//尺寸
const float** ranges, //取值范围
bool uniform=true,
bool accumulate=false );
- 1
找寻最值
void minMaxLoc(InputArray src,
CV_OUT double* minVal,
CV_OUT double* maxVal=0,
CV_OUT Point* minLoc=0,
CV_OUT Point* maxLoc=0,
InputArray mask=noArray());
- 1
一维直方图代码
bool Histo()
{
g_src=imread("lena.jpg",0);
imshow("1",g_src);
MatND Hist;//一种直方图数据结构
int channels=0;
int size=256;
float hranges[]={0,255};
const float *ranges[]={hranges};
calcHist(&g_src,1,&channels,Mat(),Hist,1,&size,ranges);
//****************
int scale=1;
Mat g_dst(size*scale,size,CV_8U,Scalar(0));
//***************
double minValue=0;
double maxValue=0;
minMaxLoc(Hist,&minValue,&maxValue,0,0);
//int hpt=saturate_cast<int>(0.9*size);//溢出保护
for (int i=0;i<256;i++)
{
float binValue=Hist.at<float>(i);
int realValue=saturate_cast<int>(binValue*0.9*size/maxValue);//这样最大值为整个高度的90%
rectangle(g_dst,Point(i*scale,size-1),Point((i+1)*scale-1,size-realValue),Scalar(255));
}
imshow("2",g_dst);
waitKey(0);
return 1;
}