fbpx
维基百科

大津算法

计算机视觉图像处理中,大津二值化法用来自动对基于聚类的图像进行二值化[1] 或者说,将一个灰度图像退化为二值图像。该算法以大津展之英语Nobuyuki Otsu命名。算法假定该图像根据双模直方图(前景像素和背景像素)把包含两类像素,于是它要计算能将两类分开的最佳阈值,使得它们的类内方差最小;由于两两平方距离恒定,所以即它们的类间方差最大。[2] 因此,大津二值化法粗略的来说就是一维費舍爾判别分析的离散化模拟。

使用大津算法二值化的图像
原图

原始方法的多级阈值扩展称为多大津算法。[3]

方法

在大津算法中,我们穷举搜索能使类内方差最小的阈值,定义为两个类的方差的加权和:

 

权重   是被阈值   分开的两个类的概率,而   是这两个类的方差。

大津证明了最小化类内方差和最大化类间方差是相同的:[2]

 

用类概率   和类均值   来表示。

类概率   用阈值为   的直方图计算:

 

而类均值   为:

 

其中   为第   个直方图面元中心的值。 同样的,你可以对大于   的面元求出右侧直方图的   

类概率和类均值可以迭代计算。这个想法会产生一个有效的算法。

大津算法得出了0:1范围上的一个阈值。这个阈值用于图像中出现的像素强度的动态范围。例如,若图像只包含155到255之间的像素强度,大津阈值0.75会映射到灰度阈值230(而不是192,因为图像包含的像素不是0–255全范围的)。常见的摄影图像往往包含全范围的像素强度,就会让这一点有争论,但其他应用会对这点区别很敏感。[4]

算法

  1. 计算每个强度级的直方图和概率
  2. 设置    的初始值
  3. 遍历所有可能的阈值   最大强度
    1. 更新   
    2. 计算  
  4. 所需的阈值对应于最大的  
  5. 你可以计算两个最大值(和两个对应的)。  是最大值而   是更大的或相等的最大值
  6. 所需的阈值 =  

JavaScript实现

注:输入参数total是给定图像中的像素数。输入参数histogram是灰度图像不同灰度级(典型的8位图像)的256元直方图。此函数输出图像的阈值。

function otsu(histogram, total) { var sum = 0; for (var i = 1; i < 256; ++i) sum += i * histogram[i]; var sumB = 0; var wB = 0; var wF = 0; var mB; var mF; var max = 0.0; var between = 0.0; var threshold1 = 0.0; var threshold2 = 0.0; for (var i = 0; i < 256; ++i) { wB += histogram[i]; if (wB == 0) continue; wF = total - wB; if (wF == 0) break; sumB += i * histogram[i]; mB = sumB / wB; mF = (sum - sumB) / wF; between = wB * wF * (mB - mF) * (mB - mF); if ( between >= max ) { threshold1 = i; if ( between > max ) { threshold2 = i; } max = between; } } return ( threshold1 + threshold2 ) / 2.0; } 

C实现

unsigned char otsu_threshold( int* histogram, int pixel_total ) {  unsigned int sumB = 0;  unsigned int sum1 = 0;  float wB = 0.0f;  float wF = 0.0f;  float mF = 0.0f;  float max_var = 0.0f;  float inter_var = 0.0f;  unsigned char threshold = 0;  unsigned short index_histo = 0;   for ( index_histo = 1; index_histo < 256; ++index_histo )  {  sum1 += index_histo * histogram[ index_histo ];  }   for (index_histo = 1; index_histo < 256; ++index_histo)  {  wB = wB + histogram[ index_histo ];  wF = pixel_total - wB;  if ( wB == 0 || wF == 0 )  {  continue;  }  sumB = sumB + index_histo * histogram[ index_histo ];  mF = ( sum1 - sumB ) / wF;  inter_var = wB * wF * ( ( sumB / wB ) - mF ) * ( ( sumB / wB ) - mF );  if ( inter_var >= max_var )  {  threshold = index_histo;  max_var = inter_var;  }  }   return threshold; } 

MATLAB实现

total为给定图像中的像素数。 histogramCounts为灰度图像不同灰度级(典型的8位图像)的256元直方图。 level为图像的阈值(double)。

function level = otsu(histogramCounts, total) %% OTSU automatic thresholding method sumB = 0; wB = 0; maximum = 0.0; threshold1 = 0.0; threshold2 = 0.0; sum1 = sum((1:256).*histogramCounts.'); % the above code is replace with this single line for ii=1:256  wB = wB + histogramCounts(ii);  if (wB == 0)  continue;  end  wF = total - wB;  if (wF == 0)  break;  end  sumB = sumB + ii * histogramCounts(ii);  mB = sumB / wB;  mF = (sum1 - sumB) / wF;  between = wB * wF * (mB - mF) * (mB - mF);  if ( between >= maximum )  threshold1 = ii;  if ( between > maximum )  threshold2 = ii;  end  maximum = between;  end end level = (threshold1 + threshold2 )/(2); end 

另一种方法是用向量化方法(可以很容易地转换为便于GPU处理Python矩阵数组版本)

function [threshold_otsu] = Thredsholding_Otsu( Image) %Intuition: %(1)pixels are divided into two groups %(2)pixels within each group are very similar to each other  % Parameters: % t : threshold  % r : pixel value ranging from 1 to 255 % q_L, q_H : the number of lower and higher group respectively % sigma : group variance % miu : group mean % Author: Lei Wang % Date  : 22/09/2013 % References : Wikepedia,  % for multi children Otsu method, please visit : https://drive.google.com/file/d/0BxbR2jt9XyxteF9fZ0NDQ0dKQkU/view?usp=sharing % This is my original work nbins = 256; counts = imhist(Image,nbins); p = counts / sum(counts); for t = 1 : nbins  q_L = sum(p(1 : t));  q_H = sum(p(t + 1 : end));  miu_L = sum(p(1 : t) .* (1 : t)') / q_L;  miu_H = sum(p(t + 1 : end) .* (t + 1 : nbins)') / q_H;  sigma_b(t) = q_L * q_H * (miu_L - miu_H)^2; end [~,threshold_otsu] = max(sigma_b(:)); end 

该实现有一点点冗余的计算。但由于大津算法快速,这个实现版本是可以接受,并且易于理解的。因为在一些环境中,如果使用向量化的形式,可以更快地运算循环。大津二值化法使用架构最小的堆-孩子标签(heap-children label)可以很容易地转变成多线程的方法。

参考文献

  1. ^ M. Sezgin and B. Sankur. Survey over image thresholding techniques and quantitative performance evaluation. Journal of Electronic Imaging. 2004, 13 (1): 146–165. doi:10.1117/1.1631315. 
  2. ^ 2.0 2.1 Nobuyuki Otsu. A threshold selection method from gray-level histograms. IEEE Trans. Sys., Man., Cyber. 1979, 9 (1): 62–66. doi:10.1109/TSMC.1979.4310076. 
  3. ^ Ping-Sung Liao and Tse-Sheng Chen and Pau-Choo Chung. A Fast Algorithm for Multilevel Thresholding. J. Inf. Sci. Eng. 2001, 17 (5): 713–727. 
  4. ^ Q&A Forum. Stack Overflow. [2015-10-20]. (原始内容于2015-11-14). 

外部链接

  • Lecture notes on thresholding(页面存档备份,存于互联网档案馆) – covers the Otsu method.
  • A plugin for ImageJ(页面存档备份,存于互联网档案馆) using Otsu's method to do the threshold.
  • A full explanation of Otsu's method(页面存档备份,存于互联网档案馆) with a working example and Java implementation.
  • Implementation of Otsu's method(页面存档备份,存于互联网档案馆) in ITK
  • Otsu Thresholding in C#(页面存档备份,存于互联网档案馆) A straightforward C# implementation with explanation.
  • Otsu's method using MATLAB(页面存档备份,存于互联网档案馆

大津算法, 在计算机视觉和图像处理中, 大津二值化法用来自动对基于聚类的图像进行二值化, 或者说, 将一个灰度图像退化为二值图像, 该算法以大津展之, 英语, nobuyuki, otsu, 命名, 算法假定该图像根据双模直方图, 前景像素和背景像素, 把包含两类像素, 于是它要计算能将两类分开的最佳阈值, 使得它们的类内方差最小, 由于两两平方距离恒定, 所以即它们的类间方差最大, 因此, 大津二值化法粗略的来说就是一维費舍爾判别分析的离散化模拟, 使用二值化的图像, 原图, 原始方法的多级阈值扩展称为多, 目录. 在计算机视觉和图像处理中 大津二值化法用来自动对基于聚类的图像进行二值化 1 或者说 将一个灰度图像退化为二值图像 该算法以大津展之 英语 Nobuyuki Otsu 命名 算法假定该图像根据双模直方图 前景像素和背景像素 把包含两类像素 于是它要计算能将两类分开的最佳阈值 使得它们的类内方差最小 由于两两平方距离恒定 所以即它们的类间方差最大 2 因此 大津二值化法粗略的来说就是一维費舍爾判别分析的离散化模拟 使用大津算法二值化的图像 原图 原始方法的多级阈值扩展称为多大津算法 3 目录 1 方法 1 1 算法 1 2 JavaScript实现 1 3 C实现 1 4 MATLAB实现 2 参考文献 3 外部链接方法 编辑在大津算法中 我们穷举搜索能使类内方差最小的阈值 定义为两个类的方差的加权和 s w 2 t w 1 t s 1 2 t w 2 t s 2 2 t displaystyle sigma w 2 t omega 1 t sigma 1 2 t omega 2 t sigma 2 2 t 权重 w i displaystyle omega i 是被阈值 t displaystyle t 分开的两个类的概率 而 s i 2 displaystyle sigma i 2 是这两个类的方差 大津证明了最小化类内方差和最大化类间方差是相同的 2 s b 2 t s 2 s w 2 t w 1 t w 2 t m 1 t m 2 t 2 displaystyle sigma b 2 t sigma 2 sigma w 2 t omega 1 t omega 2 t left mu 1 t mu 2 t right 2 用类概率 w i displaystyle omega i 和类均值 m i displaystyle mu i 来表示 类概率 w 1 t displaystyle omega 1 t 用阈值为 t displaystyle t 的直方图计算 w 1 t S 0 t p i displaystyle omega 1 t Sigma 0 t p i 而类均值 m 1 t displaystyle mu 1 t 为 m 1 t S 0 t p i x i w 1 displaystyle mu 1 t left Sigma 0 t p i x i right omega 1 其中 x i displaystyle x i 为第 i displaystyle i 个直方图面元中心的值 同样的 你可以对大于 t displaystyle t 的面元求出右侧直方图的 w 2 t displaystyle omega 2 t 与 m 2 displaystyle mu 2 类概率和类均值可以迭代计算 这个想法会产生一个有效的算法 大津算法得出了0 1范围上的一个阈值 这个阈值用于图像中出现的像素强度的动态范围 例如 若图像只包含155到255之间的像素强度 大津阈值0 75会映射到灰度阈值230 而不是192 因为图像包含的像素不是0 255全范围的 常见的摄影图像往往包含全范围的像素强度 就会让这一点有争论 但其他应用会对这点区别很敏感 4 算法 编辑 计算每个强度级的直方图和概率 设置 w i 0 displaystyle omega i 0 和 m i 0 displaystyle mu i 0 的初始值 遍历所有可能的阈值 t 1 displaystyle t 1 ldots 最大强度 更新 w i displaystyle omega i 和 m i displaystyle mu i 计算 s b 2 t displaystyle sigma b 2 t 所需的阈值对应于最大的 s b 2 t displaystyle sigma b 2 t 你可以计算两个最大值 和两个对应的 s b 1 2 t displaystyle sigma b1 2 t 是最大值而 s b 2 2 t displaystyle sigma b2 2 t 是更大的或相等的最大值 所需的阈值 threshold 1 threshold 2 2 displaystyle frac text threshold 1 text threshold 2 2 JavaScript实现 编辑 注 输入参数total是给定图像中的像素数 输入参数histogram是灰度图像不同灰度级 典型的8位图像 的256元直方图 此函数输出图像的阈值 function otsu histogram total var sum 0 for var i 1 i lt 256 i sum i histogram i var sumB 0 var wB 0 var wF 0 var mB var mF var max 0 0 var between 0 0 var threshold1 0 0 var threshold2 0 0 for var i 0 i lt 256 i wB histogram i if wB 0 continue wF total wB if wF 0 break sumB i histogram i mB sumB wB mF sum sumB wF between wB wF mB mF mB mF if between gt max threshold1 i if between gt max threshold2 i max between return threshold1 threshold2 2 0 C实现 编辑 unsigned char otsu threshold int histogram int pixel total unsigned int sumB 0 unsigned int sum1 0 float wB 0 0f float wF 0 0f float mF 0 0f float max var 0 0f float inter var 0 0f unsigned char threshold 0 unsigned short index histo 0 for index histo 1 index histo lt 256 index histo sum1 index histo histogram index histo for index histo 1 index histo lt 256 index histo wB wB histogram index histo wF pixel total wB if wB 0 wF 0 continue sumB sumB index histo histogram index histo mF sum1 sumB wF inter var wB wF sumB wB mF sumB wB mF if inter var gt max var threshold index histo max var inter var return threshold MATLAB实现 编辑 total为给定图像中的像素数 histogramCounts为灰度图像不同灰度级 典型的8位图像 的256元直方图 level为图像的阈值 double function level otsu histogramCounts total OTSU automatic thresholding method sumB 0 wB 0 maximum 0 0 threshold1 0 0 threshold2 0 0 sum1 sum 1 256 histogramCounts the above code is replace with this single line for ii 1 256 wB wB histogramCounts ii if wB 0 continue end wF total wB if wF 0 break end sumB sumB ii histogramCounts ii mB sumB wB mF sum1 sumB wF between wB wF mB mF mB mF if between gt maximum threshold1 ii if between gt maximum threshold2 ii end maximum between end end level threshold1 threshold2 2 end 另一种方法是用向量化方法 可以很容易地转换为便于GPU处理Python矩阵数组版本 function threshold otsu Thredsholding Otsu Image Intuition 1 pixels are divided into two groups 2 pixels within each group are very similar to each other Parameters t threshold r pixel value ranging from 1 to 255 q L q H the number of lower and higher group respectively sigma group variance miu group mean Author Lei Wang Date 22 09 2013 References Wikepedia for multi children Otsu method please visit https drive google com file d 0BxbR2jt9XyxteF9fZ0NDQ0dKQkU view usp sharing This is my original work nbins 256 counts imhist Image nbins p counts sum counts for t 1 nbins q L sum p 1 t q H sum p t 1 end miu L sum p 1 t 1 t q L miu H sum p t 1 end t 1 nbins q H sigma b t q L q H miu L miu H 2 end threshold otsu max sigma b end 该实现有一点点冗余的计算 但由于大津算法快速 这个实现版本是可以接受 并且易于理解的 因为在一些环境中 如果使用向量化的形式 可以更快地运算循环 大津二值化法使用架构最小的堆 孩子标签 heap children label 可以很容易地转变成多线程的方法 参考文献 编辑 M Sezgin and B Sankur Survey over image thresholding techniques and quantitative performance evaluation Journal of Electronic Imaging 2004 13 1 146 165 doi 10 1117 1 1631315 2 0 2 1 Nobuyuki Otsu A threshold selection method from gray level histograms IEEE Trans Sys Man Cyber 1979 9 1 62 66 doi 10 1109 TSMC 1979 4310076 Ping Sung Liao and Tse Sheng Chen and Pau Choo Chung A Fast Algorithm for Multilevel Thresholding J Inf Sci Eng 2001 17 5 713 727 Q amp A Forum Stack Overflow 2015 10 20 原始内容存档于2015 11 14 外部链接 编辑Lecture notes on thresholding 页面存档备份 存于互联网档案馆 covers the Otsu method A plugin for ImageJ 页面存档备份 存于互联网档案馆 using Otsu s method to do the threshold A full explanation of Otsu s method 页面存档备份 存于互联网档案馆 with a working example and Java implementation Implementation of Otsu s method 页面存档备份 存于互联网档案馆 in ITK Otsu Thresholding in C 页面存档备份 存于互联网档案馆 A straightforward C implementation with explanation Otsu s method using MATLAB 页面存档备份 存于互联网档案馆 取自 https zh wikipedia org w index php title 大津算法 amp oldid 74972633, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。