【问题标题】:Matlab - Dynamic ThresholdingMatlab - 动态阈值
【发布时间】:2015-05-04 11:38:08
【问题描述】:

我正在尝试进行动态阈值处理,但出现了一些错误。我正在尝试适应这段代码: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

【问题讨论】:

  • 您能否提供一些有关您收到的错误以及代码的哪一部分给您错误的信息?
  • 您的for 循环范围为H:HW:W...每个循环执行一次。这看起来很可疑。我希望像 1:H1:W 这样的东西。

标签: image algorithm matlab threshold adaptive-threshold


【解决方案1】:

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

在 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 是与均值差的阈值。

最好的,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多