-
OTSU求阈值程序
writen by
wqj1212@yahoo.com.cn
int otsu (IplImage *image, int rows, int cols, int x0, int y0, int dx, int dy, int vvv)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
unsigned char *np; // 图像指针
int thresholdValue=1; // 阈值
int ihist[256]; // 图像直方图,256个点
![](/Images/OutliningIndicators/InBlock.gif)
int i, j, k; // various counters
int n, n1, n2, gmin, gmax;
double m1, m2, sum, csum, fmax, sb;
![](/Images/OutliningIndicators/InBlock.gif)
// 对直方图置零![](https://www.cnblogs.com/Images/dot.gif)
memset(ihist, 0, sizeof(ihist));
![](/Images/OutliningIndicators/InBlock.gif)
gmin=255; gmax=0;
// 生成直方图
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*for (i = y0 + 1; i < y0 + dy - 1; i++) {
np = &image[i*cols+x0+1];
for (j = x0 + 1; j < x0 + dx - 1; j++) {
ihist[*np]++;
if(*np > gmax) gmax=*np;
if(*np < gmin) gmin=*np;
np++; /* next pixel
}
}*/
for(j=y0;j<dy;j++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
for(i=0;i<dx;i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
unsigned char temp=CV_IMAGE_ELEM(image,uchar,j,i);
ihist[temp]++;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
// set up everything
sum = csum = 0.0;
n = 0;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (k = 0; k <= 255; k++)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
sum += (double) k * (double) ihist[k]; /**//* x*f(x) 质量矩*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
n += ihist[k]; /**//* f(x) 质量 */
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (!n)
{
// if n has no value, there is problems![](https://www.cnblogs.com/Images/dot.gif)
fprintf (stderr, "NOT NORMAL thresholdValue = 160\n");
return (160);
}
![](/Images/OutliningIndicators/InBlock.gif)
// do the otsu global thresholding method
fmax = -1.0;
n1 = 0;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (k = 0; k < 255; k++)
{
n1 += ihist[k];
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (!n1)
{ continue; }
n2 = n - n1;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (n2 == 0)
{ break; }
csum += (double) k *ihist[k];
m1 = csum / n1;
m2 = (sum - csum) / n2;
sb = (double) n1 *(double) n2 *(m1 - m2) * (m1 - m2);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* bbg: note: can be optimized. */
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (sb > fmax)
{
fmax = sb;
thresholdValue = k;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
// at this point we have our thresholding value
![](/Images/OutliningIndicators/InBlock.gif)
// debug code to display thresholding values
if ( vvv & 1 )
fprintf(stderr,"# OTSU: thresholdValue = %d gmin=%d gmax=%d\n",thresholdValue, gmin, gmax);
![](/Images/OutliningIndicators/InBlock.gif)
return(thresholdValue);
}
-
相关阅读:
asp.net Core 中间件Hello world
MVC2 ,MVC3 ,MVC4,MVC5的区别,EF的各个版本的区别;LocalDB是个啥
EF6 MVC5译文
发布一个C++版本的ORM库SmartDB
ORM
Git图形化界面客户端大汇总
Model工具
HTTP协议的8种请求类型介绍
五大主流数据库模型
店铺装修——进阶模块介绍
-
原文地址:https://www.cnblogs.com/wqj1212/p/1009655.html
Copyright © 2020-2023
润新知