大津法otsu的自动阈值分割源代码
来源:互联网 发布:mac安装apache php 编辑:程序博客网 时间:2024/05/16 08:56
/*======================================================================*/
/* 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;
}
}
}
- 大津法otsu的自动阈值分割源代码
- 大津法otsu的自动阈值分割源代码
- 灰度图像的自动阈值分割(Otsu 法)
- 一种otsu阈值分割方法
- 基于Otsu算法的图像自适应阈值分割
- 图像分割--使用Otsu方法的全局阈值处理
- 自适应阈值分割—大津法(OTSU算法)C++实现
- 自适应阈值分割—大津法(OTSU算法)C++实现
- ostu大津法 自动阈值分割
- 自适应阈值分割之otsu算法
- 自适应阈值分割之otsu算法
- OTSU算法 -- 三通道阈值分割
- 灰度图像--图像分割 阈值处理之OTSU阈值
- 阈值分割中全局阈值自动确定的一种方法
- OpenCV的otsu自适应阈值分割的算法在肤色检测中的应用
- opencv实现c++的otsu自适应阈值分割的算法描述
- otsu自动阈值对图像二值化程序
- 自动阈值法:Otsu法 用MATLAB实现Otsu算法:
- HCI History
- automake搭建项目工程例子讲解
- 一个工程中如何包含多个web.config?
- nginx fpm性能调优
- 分享知识,收获成功
- 大津法otsu的自动阈值分割源代码
- 最近项目中应该记住的一些经验
- 使用navicat创建MySQL外键关联
- 音视频普及知识
- 总账交流
- flex中的buffer
- myeclipse gen——MyEclipse密钥破解
- php对csv文件的读取,写入,输出下载操作
- 云计算:程序员重回个人英雄时代