MATLAB自适应阈值超慢
MATLAB adaptive thresholding super slow
所以我有一堆代码 运行 花了大约一分钟,追溯到自适应阈值,特别是一行。关于如何加快速度的任何建议或关于为什么这是不可避免的任何解释?.."mIM=medfilt2(IM,[ws ws]);" 是一切都变慢的地方。
function bw=adaptivethreshold(IM,ws,C,tm)
%ADAPTIVETHRESHOLD An adaptive thresholding algorithm that seperates the
%foreground from the background with nonuniform illumination.
% bw=adaptivethreshold(IM,ws,C) outputs a binary image bw with the local
% threshold mean-C or median-C to the image IM.
% ws is the local window size.
% tm is 0 or 1, a switch between mean and median. tm=0 mean(default); tm=1 median.
%
% Contributed by ...
% at Tsinghua University, Beijing, China.
%
% For more information, please see
% http://homepages.inf.ed.ac.uk/rbf/HIPR2/adpthrsh.htm
if (nargin<3)
error('You must provide the image IM, the window size ws, and C.');
elseif (nargin==3)
tm=0;
elseif (tm~=0 && tm~=1)
error('tm must be 0 or 1.');
end
IM=mat2gray(IM);
disp(strcat('100: ',datestr(now)))
if tm==0
mIM=imfilter(IM,fspecial('average',ws),'replicate');
else
mIM=medfilt2(IM,[ws ws]);
end
sIM=mIM-IM-C;
bw=im2bw(sIM,0);
bw=imcomplement(bw);
中值滤波是一种非线性操作,因此可能需要很长时间才能执行。对于较大的 ws
值,您应该更喜欢 ordfilt2
而不是 medfilt2
:它既更快又更灵活!
这是一个示例代码,它对两个函数执行相同的中值滤波:
Img = imread('elephant.jpg');
ws = 100;
tic
Res = medfilt2(Img, [ws ws]);
toc
tic
Res = ordfilt2(Img, round(ws^2/2), true(ws));
toc
我机器上的时间:
Elapsed time is 0.190697 seconds.
Elapsed time is 0.095528 seconds.
最佳,
所以我有一堆代码 运行 花了大约一分钟,追溯到自适应阈值,特别是一行。关于如何加快速度的任何建议或关于为什么这是不可避免的任何解释?.."mIM=medfilt2(IM,[ws ws]);" 是一切都变慢的地方。
function bw=adaptivethreshold(IM,ws,C,tm)
%ADAPTIVETHRESHOLD An adaptive thresholding algorithm that seperates the
%foreground from the background with nonuniform illumination.
% bw=adaptivethreshold(IM,ws,C) outputs a binary image bw with the local
% threshold mean-C or median-C to the image IM.
% ws is the local window size.
% tm is 0 or 1, a switch between mean and median. tm=0 mean(default); tm=1 median.
%
% Contributed by ...
% at Tsinghua University, Beijing, China.
%
% For more information, please see
% http://homepages.inf.ed.ac.uk/rbf/HIPR2/adpthrsh.htm
if (nargin<3)
error('You must provide the image IM, the window size ws, and C.');
elseif (nargin==3)
tm=0;
elseif (tm~=0 && tm~=1)
error('tm must be 0 or 1.');
end
IM=mat2gray(IM);
disp(strcat('100: ',datestr(now)))
if tm==0
mIM=imfilter(IM,fspecial('average',ws),'replicate');
else
mIM=medfilt2(IM,[ws ws]);
end
sIM=mIM-IM-C;
bw=im2bw(sIM,0);
bw=imcomplement(bw);
中值滤波是一种非线性操作,因此可能需要很长时间才能执行。对于较大的 ws
值,您应该更喜欢 ordfilt2
而不是 medfilt2
:它既更快又更灵活!
这是一个示例代码,它对两个函数执行相同的中值滤波:
Img = imread('elephant.jpg');
ws = 100;
tic
Res = medfilt2(Img, [ws ws]);
toc
tic
Res = ordfilt2(Img, round(ws^2/2), true(ws));
toc
我机器上的时间:
Elapsed time is 0.190697 seconds.
Elapsed time is 0.095528 seconds.
最佳,