博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
otsu的自动阈值分割技术
阅读量:4610 次
发布时间:2019-06-09

本文共 1648 字,大约阅读时间需要 5 分钟。

otsu的自动阈值分割技术
/*======================================================================*/
/* OTSU global thresholding routine */
/*======================================================================*/
void otsu (IplImage *image)
{
int w = image->width;
int h = image->height;
unsigned char *np; // 图像指针
unsigned char pixel;
int thresholdValue=1; // 阈值
int ihist[256]; // 图像直方图,256个点
int i, j, k; // various counters
int n, n1, n2, gmin, gmax;
double m1, m2, sum, csum, fmax, sb;
// 对直方图置零...
memset(ihist, 0, sizeof(ihist));
gmin=255; gmax=0;
// 生成直方图
for (i = 0; i < h; i++)
{
np = (unsigned char*)(image->imageData + image->widthStep*i);
for (j = 0; j < w; j++)
{
pixel = np[j];
ihist[ pixel]++;
if(pixel > gmax) gmax= pixel;
if(pixel < gmin) gmin= pixel;
}
}
// set up everything
sum = csum = 0.0;
n = 0;
for (k = 0; k <= 255; k++)
{
sum += k * ihist[k]; /* x*f(x) 质量矩*/
n += ihist[k]; /* f(x) 质量 */
}
if (!n)
{
// if n has no value, there is problems...
//fprintf (stderr, "NOT NORMAL thresholdValue = 160\n");
thresholdValue = 160;
goto L;
}
// do the otsu global thresholding method
fmax = -1.0;
n1 = 0;
for (k = 0; k < 255; k++)
{
n1 += ihist[k];
if (!n1) { continue; }
n2 = n - n1;
if (n2 == 0) { break; }
csum += k *ihist[k];
m1 = csum / n1;
m2 = (sum - csum) / n2;
sb = n1 * n2 *(m1 - m2) * (m1 - m2);
/* bbg: note: can be optimized. */
if (sb > fmax)
{
fmax = sb;
thresholdValue = k;
}
}
L:
for (i = 0; i < h; i++)
{
np = (unsigned char*)(image->imageData + image->widthStep*i);
for (j = 0; j < w; j++)
{
if(np[j] >= thresholdValue)
np[j] = 255;
else np[j] = 0;
}
}
}

转载于:https://www.cnblogs.com/tecsoon/archive/2009/08/14/1546227.html

你可能感兴趣的文章
基础数据结构
查看>>
关闭CENTOS不必要的默认服务
查看>>
showModalDialog改进版,包括Chrome下的特殊处理
查看>>
mysql学习
查看>>
对Jpa中Entity关系映射中mappedBy的理解
查看>>
获取注册表某键下的所有子键
查看>>
java类库
查看>>
spring boot中log4j冲突问题和解决办法
查看>>
python练手习题
查看>>
kmp算法的个人理解
查看>>
python 爬虫 加强记忆
查看>>
[USACO07JAN] Tallest Cow
查看>>
Selenium收藏官方网址
查看>>
[译]ABP vNext微服务演示,项目状态和路线图
查看>>
Easyui 页面訪问慢解决方式,GZIP站点压缩加速优化
查看>>
Web前端面试指导(十四):如何居中一个元素(正常、绝对定位、浮动元素)?
查看>>
ArcFac_C#_DEMO开发
查看>>
iOS各版本特性
查看>>
牛客——倒水问题
查看>>
Git 远程仓库
查看>>