-1

我正在尝试进行动态阈值处理,但出现了一些错误。我正在尝试适应此代码: http: //www.inf.ed.ac.uk/teaching/courses/ivr/lectures/ivr5hand.pdf

function [Output ] = dinamicthresh()

[filename,pathname] = uigetfile('*.bmp; *.jpg', 'Please select an image file');     


I=fullfile (pathname,filename); 


I=imread(I);


I=rgb2gray(I);


I=im2double(I);

[H,W]= size(I);
Output= zeros(H,W);

halfH=round(H/2);
halfW=round(W/2);

for i= H:H
for j = W:W
    C = I(i-halfH:i+halfH,j-halfW:j+halfW);
    adaptative_thresh = mean(mean(C)) - 12;
          if I(i,j) < adaptative_thresh 
        Output(i,j)= 1;
            else 
        Output(i,j)= 0;
    end
end
end

subplot(1,2,1);imshow(I);title('Original Image');
subplot(1,2,2);imshow(Output);title('Adaptive Thresholding');

end
4

1 回答 1

0

您的阈值算法将每个像素与局部平均值之间的差异与给定阈值进行比较。

这个任务可以在 Matlab 中以更直接的方式执行,使用filter2.

例如这张图片:

在此处输入图像描述

% --- Parameters
w = 20;
h = 20;
th = 12;

% --- Load image
Img = double(rgb2gray(imread('Img.png')));

% --- Get the locally-averaged image
Mean = filter2(fspecial('average', [h w]), Img);

% --- Get thresholded image
BW = (Img-Mean)>th;

% --- Disply result
imshow(BW)

我得到以下结果:

在此处输入图像描述

当然,您可以使用参数来调整此代码以适应您的图像:

  • w是平均框的宽度
  • h是平均框的高度
  • th是与均值差的阈值。

最好的,

于 2015-03-03T20:02:52.600 回答