我试图实施以下 误差最小阈值 (J.Kittler和J.伊林沃思)的方法在MATLAB。

你可以看看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

原来大小的图片.

目标是提取的二进制形象的信(希伯来语字母)。我应用的代码分块的图像(40×40).但我结果这都不如 K-意味着聚类方法.

我错过了什么?任何人有一个更好的主意吗?

谢谢。

P.S.会有人添加"适应-阈值"的主题标记(我不能为我的新).

有帮助吗?

解决方案

我认为你的代码不是完全正确的。你用绝对的直方图象而不是相对直方图,它是用纸张。此外,你代码是相当低效率的,因为它计算的两个柱状图每可能的阈值。我实施的算法我自己。也许有人可以使用它:

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;

其他提示

阈值是一个相当棘手的事务。在多年来,我一直阈值的图像我还没有找到一种单一的技术,始终执行行好,我来不信任的权利要求的普遍优越性能在CS期刊。

最大的错误阈值方法仅适用于很好的双峰的直方图(但它的工作原理以及在那些)。你像看起来像信号和背景可能不能清楚地分离不够,用于该阈值方法的工作。

如果你想确认代码工作得很好,您可以创建一个测试这样的程序,并检查是否得到良好的初始分割,以及在什么水平的'双峰'代码划分。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top