OTSU方法计算图像二值化的自适应阈值
来源:互联网 发布:知乎南京哪家皮肤科好 编辑:程序博客网 时间:2024/04/29 21:15
OTSU方法计算图像二值化的自适应阈值(转载)
*
OTSU 算法可以说是自适应计算单阈值(用来转换灰度图像为二值图像)的简单高效方法。下面的代码最早由 Ryan Dibble提供,此后经过多人Joerg.Schulenburg, R.Z.Liu 等修改,补正。
转自:http://forum.assuredigit.com/display_topic_threads.asp?ForumID=8&TopicID=3480
算法对输入的灰度图像的直方图进行分析,将直方图分成两个部分,使得两部分之间的距离最大。划分点就是求得的阈值。
parameter: *image --- buffer for image
rows, cols --- size of image
x0, y0, dx, dy --- region of vector used for computing threshold
vvv --- debug option, is 0, no debug information outputed
*/
/*======================================================================*/
/* OTSU global thresholding routine */
/* takes a 2D unsigned char array pointer, number of rows, and */
/* number of cols in the array. returns the value of the threshold */
/*======================================================================*/
int otsu (unsigned char *image, int rows, int cols, int x0, int y0, int dx, int dy, int vvv)
{
unsigned char *np; // 图像指针
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 = 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 */
}
}
// set up everything
sum = csum = 0.0;
n = 0;
for (k = 0; k <= 255; k++) {
sum += (double) k * (double) 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);
return (160);
}
// 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 += (double) k *ihist[k];
m1 = csum / n1;
m2 = (sum - csum) / n2;
sb = (double) n1 *(double) n2 *(m1 - m2) * (m1 - m2);
/* bbg: note: can be optimized. */
if (sb > fmax) {
fmax = sb;
thresholdValue = k;
}
}
// at this point we have our thresholding value
// debug code to display thresholding values
if ( vvv & 1 )
fprintf(stderr,# OTSU: thresholdValue = %d gmin=%d gmax=%d/n,
thresholdValue, gmin, gmax);
return(thresholdValue);
}
- OTSU方法计算图像二值化的自适应阈值
- OTSU方法计算图像二值化的自适应阈值
- OTSU方法计算图像二值化的自适应阈值
- 基于Otsu算法的图像自适应阈值分割
- OPenCV:采用otsu自适应门限的图像二值化方法
- 图像分割--使用Otsu方法的全局阈值处理
- 带有 mask 的 OTSU 自适应阈值
- Otsu自适应阈值算法
- Emgu 学习笔记(二)---图像二值化,自适应阈值化,Otsu二值化
- otsu自动阈值对图像二值化程序
- 图像处理之基于Otsu阈值二值化
- 图像处理------基于Otsu阈值二值化
- 图像处理之基于OTSU阈值二值化
- 图像otsu阈值化
- 图像的二值化模块(固定阈值法和Otsu阈值法)
- 图像的阈值化-OTSU、固定阈值法、基于局部的阈值化
- OTSU算法提取图像阈值的C语言实现[转载】
- OTSU算法提取图像阈值的C语言实现
- C++的CLI版本起步
- 新年感悟
- How to Correctly and Uniformly Use Progress Monitors
- 中国人说汉语--使用汉语编程的.Net实践之路
- 一个完整的TableViewer实例
- OTSU方法计算图像二值化的自适应阈值
- Worthwhile spending for legitimation
- zt:高效程序员应该养成的七个习惯
- UNIX&&LINUX开篇
- 从getpid()开始
- VB分布式应用程序开发随记---使用实现继承的问题
- 成为Eclipse热键高手[Sam Brodkin][gamvan.cn]
- 关于sm的极限!
- 常用HTML的说明