OTSU常用的二值化方法
来源:互联网 发布:淘宝京东比价插件 编辑:程序博客网 时间:2024/05/22 05:18
OTSU算法称为大律法或最大类间方差法
原理:
Mat Otsu(Mat src)
{
int height=src.rows ;//rows ;
int width=src.cols ;
int threshold;
float maxVariance=0;
float variance[256];
//histogram
float histogram[256] = {0};
for(int i=0; i < height; i++)
{
//unsigned char* p=(unsigned char*)src.data + src.step * i;
unsigned char* p=src.ptr<unsigned char>(i);
for(int j = 0; j < width; j++)
{//histogram[int(*p++)]++;
histogram[p[j]]++;
}}
//normalize histogram
int size = height * width;
for(int i = 0; i < 256; i++)
{
histogram[i] = histogram[i] / size;
}for(threshold=0;threshold<256;threshold++)
{
float w0 = 0, u0 = 0,w1= 0,u1= 0;
for(int i = 0; i < threshold; i++)
{
w0 += histogram[i]; //前景像素 所占整幅图像的比例
u0 += i * histogram[i];// 前景像素的平均灰度值
}
for(int i = threshold; i < 256; i++)
{
w1 += histogram[i]; //背景像素)所占整幅图像的比例
u1 += i * histogram[i];// 背景像素的平均灰度值
}
variance[threshold] = w0*w1*(u0-u1)*(u0-u1);
}
threshold=1;
for(int i=1;i<256;i++)
{
if(variance[i]>variance[threshold])
{
threshold=i;
}
}
Mat dst=src.clone();
for(int i=0; i < height; i++)
{
unsigned char* data=src.ptr<unsigned char>(i);
unsigned char* dst_data=dst.ptr<unsigned char>(i);
for(int j = 0; j < width; j++)
{
if(data[j]<threshold)
{
dst_data[j]=255;
}
else
{
dst_data[j]=0;
}
}
}return dst;
}
- OTSU常用的二值化方法
- OTSU方法计算图像二值化的自适应阈值
- OTSU方法计算图像二值化的自适应阈值
- OTSU方法计算图像二值化的自适应阈值
- OPenCV:采用otsu自适应门限的图像二值化方法
- Otsu方法
- 基于otsu的图像二值化
- otsu二值化
- otsu(大津法)灰度图像二值化方法的原理和C实现
- 图像二值化:基于otsu的图像二值化
- 图像分割--使用Otsu方法的全局阈值处理
- 图像二值化方法--OTSU(最大类间方差法)
- 大津法 OTSU的算法
- 一种otsu阈值分割方法
- 【二值化】Otsu算法
- 双窗OTSU 二值化
- Otsu 大律法 图像二值化
- Otsu’s 二值化
- 配置keystone使用redis
- [Leetcode] 50 - Pow(x, n)
- Android必备:使用Eclipse編譯Android源碼
- Hive SQL使用和数据加载的一点总结
- 初学LINUX 计划安排
- OTSU常用的二值化方法
- 【BZOJ3158】千钧一发 最小割
- workerman和nodejs自测性能对比
- 我的新起点
- JQuery中如何给html的元素赋值
- 幻方解法之Strachey法生成双偶幻方
- Gora官方范例
- 在Eclipse中开发Android系统的内置应用(Mms, Contacts ect)
- 希尔排序