العتبة التكيفية - تنفيذ طريقة الحد الأدنى من عتبة الخطأ

StackOverflow https://stackoverflow.com/questions/2055774

سؤال

أحاول تنفيذ ما يلي الحد الأدنى لعتبة الخطأ (بواسطة ج.كيتلر و ج.طريقة Illingworth) في 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-يعني طريقة التجميع.

هل فاتني شيء؟هل لدى أي شخص فكرة أفضل؟

شكرًا.

ملاحظة.هل يمكن لأي شخص إضافة "عتبة التكيف" إلى علامات الموضوع (لا أستطيع لأنني جديد).

هل كانت مفيدة؟

المحلول

أعتقد أن الكود الخاص بك ليس صحيحًا تمامًا.يمكنك استخدام الرسم البياني المطلق للصورة بدلاً من الرسم البياني النسبي المستخدم في الورقة.بالإضافة إلى ذلك، فإن التعليمات البرمجية الخاصة بك غير فعالة إلى حد ما لأنها تحسب رسمين بيانيين لكل حد ممكن.لقد قمت بتنفيذ الخوارزمية بنفسي.ربما يمكن لأي شخص الاستفادة منه:

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;

نصائح أخرى

العتبة هي عمل صعب إلى حد ما.في السنوات العديدة التي كنت أتعامل فيها مع الصور، لم أجد تقنية واحدة تؤدي دائمًا أداءً جيدًا، وأصبحت لا أثق في ادعاءات الأداء الممتاز عالميًا في مجلات علوم الكمبيوتر.

تعمل طريقة الحد الأقصى للخطأ فقط على الرسم البياني ثنائي النسق بشكل جيد (لكنها تعمل بشكل جيد مع تلك).تبدو صورتك وكأنها إشارة وقد لا يتم فصل الخلفية بشكل واضح بما يكفي لكي تعمل طريقة العتبة هذه.

إذا كنت تريد التأكد من أن الكود يعمل بشكل جيد، فيمكنك إنشاء برنامج اختبار مثل هذا والتحقق مما إذا كنت تحصل على تجزئة أولية جيدة، وكذلك على مستوى "ثنائية النمط" الذي ينهار فيه الكود.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top