代码之家  ›  专栏  ›  技术社区  ›  Royi

自适应阈值

  •  2
  • Royi  · 技术社区  · 16 年前

    我尝试在Matlab中实现以下方法: 最小误差阈值-作者J.Kittler和J.Illingworth

    您可以查看pdf:

    我的代码是:

    function [ Level ] = MET( IMG )
    %Maximum Error Thresholding By Kittler
    %   Finding the Min of a cost function J in any possible thresholding. The
    %   function output is the Optimal Thresholding.
    
    for t = 0:255 % Assuming 8 bit image
        I1 = IMG;
        I1 = I1(I1 <= t);
        q1 = sum(hist(I1, 256));
    
        I2 = IMG;
        I2 = I2(I2 > t);
        q2 = sum(hist(I2, 256));
    
        % J is proportional to the Overlapping Area of the 2 assumed Gaussians
        J(t + 1) = 1 + 2 * (q1 * log(std(I1, 1)) + q2 * log(std(I2, 1)))...
            -2 * (q1 * log(q1) + q2 * log(q2));
    end
    
    [~, Level] = min(J);
    
    %Level = (IMG <= Level);
    
    end
    

    我在下面的图片上尝试过: Letters http://i45.tinypic.com/xmvr52.jpg

    Original size image .

    目标是提取字母的二进制图像(希伯来字母)。 我将代码应用于图像的子块(40 x 40)。 但是我得到的结果比 K-Means Clusters method .

    我错过什么了吗? 有人有更好的主意吗?

    谢谢。

    附笔。 是否有人会在主题标签中添加“自适应阈值”(我不能,因为我是新的)。

    2 回复  |  直到 11 年前
        1
  •  2
  •   Kocki    15 年前

    我认为你的代码不完全正确。使用图像的绝对柱状图代替本文中使用的相对柱状图。此外,您的代码效率相当低,因为它为每个可能的阈值计算两个柱状图。我自己实现了算法。也许,有人可以利用它:

    function [ optimalThreshold, J ] = kittlerMinimimErrorThresholding( img )
    %KITTLERMINIMIMERRORTHRESHOLDING Compute an optimal image threshold.
    %   Computes the Minimum Error Threshold as described in
    %   
    %   'J. Kittler and J. Illingworth, "Minimum Error Thresholding," Pattern
    %   Recognition 19, 41-47 (1986)'.
    %   
    %   The image 'img' is expected to have integer values from 0 to 255.
    %   'optimalThreshold' holds the found threshold. 'J' holds the values of
    %   the criterion function.
    
    %Initialize the criterion function
    J = Inf * ones(255, 1);
    
    %Compute the relative histogram
    histogram = double(histc(img(:), 0:255)) / size(img(:), 1);
    
    %Walk through every possible threshold. However, T is interpreted
    %differently than in the paper. It is interpreted as the lower boundary of
    %the second class of pixels rather than the upper boundary of the first
    %class. That is, an intensity of value T is treated as being in the same
    %class as higher intensities rather than lower intensities.
    for T = 1:255
    
        %Split the hostogram at the threshold T.
        histogram1 = histogram(1:T);
        histogram2 = histogram((T+1):end);
    
        %Compute the number of pixels in the two classes.
        P1 = sum(histogram1);
        P2 = sum(histogram2);
    
        %Only continue if both classes contain at least one pixel.
        if (P1 > 0) && (P2 > 0)
    
            %Compute the standard deviations of the classes.
            mean1 = sum(histogram1 .* (1:T)') / P1;
            mean2 = sum(histogram2 .* (1:(256-T))') / P2;
            sigma1 = sqrt(sum(histogram1 .* (((1:T)' - mean1) .^2) ) / P1);
            sigma2 = sqrt(sum(histogram2 .* (((1:(256-T))' - mean2) .^2) ) / P2);
    
            %Only compute the criterion function if both classes contain at
            %least two intensity values.
            if (sigma1 > 0) && (sigma2 > 0)
    
                %Compute the criterion function.
                J(T) = 1 + 2 * (P1 * log(sigma1) + P2 * log(sigma2)) ...
                         - 2 * (P1 * log(P1) + P2 * log(P2));
    
            end
        end
    
    end
    
    %Find the minimum of J.
    [~, optimalThreshold] = min(J);
    optimalThreshold = optimalThreshold - 0.5;
    
        2
  •  3
  •   Jonas    16 年前

    打谷是一项相当棘手的工作。多年来,我一直在对图像进行阈值处理,但我没有找到一种始终表现良好的技术,我开始怀疑在CS期刊上普遍表现出色的说法。

    最大误差阈值法只能很好地处理双峰直方图(但在双峰直方图上效果很好)。您的图像看起来像信号和背景可能没有足够的清晰分离,这一阈值方法工作。

    如果你想确保代码运行良好,你可以创建一个这样的测试程序,检查你是否得到了良好的初始分割,以及在什么程度上的“双模性”代码崩溃。