int Ostu(cv::Mat &temp,int &Threshvalue)
{
int hist_size=256;
int hist_height=256;
float range[]={0,255};
float *ranges[]={range};
IplImage *src=&IplImage(temp);
CvHistogram* gray_hist=cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1);
cvCalcHist(&src,gray_hist,0,0);
cvNormalizeHist(gray_hist,1.0);
int Width=src->width;
int Height=src->height;
double delta=0.0;
double U_t=0;
for(int i=0;i<256;i++)
{
U_t+=cvQueryHistValue_1D(gray_hist,i)*i;
}
double u=0,w=0;
for(int j=0;j<256;j++)
{
u+=cvQueryHistValue_1D(gray_hist,j)*j;
w+=cvQueryHistValue_1D(gray_hist,j);
double t=U_t*w-u;
double delta_tmp=t*t/(w*(1-w));
if(delta_tmp>delta)
{
delta=delta_tmp;
Threshvalue=j;
}
}
cvReleaseHist(&gray_hist);
return Threshvalue;
}
大津法进行图像二值化opencv
最新推荐文章于 2022-02-08 23:30:22 发布