Hello, there is no error message regarding my code but the program is busy forever. what id the problem?

1 view (last 30 days)
im = imread('retina.png');
im=rgb2gray(im);
S=size(im);
numR=S(1);
numC=S(2); %now without edges
%Voting
Accumulator = zeros(S); % initialize the accumulator
[yIndex ,xIndex] = find(im); % find x,y of edge pixels
r=30; r2 = r^2; % square of radius, to prevent its calculation in the loop
for cnt = 1:numel(xIndex)
low=xIndex(cnt)-r;
high=xIndex(cnt)+r;
if (low<1)
low=1;
end
if (high>numC)
high=numC;
end
for x0 = low:high
yOffset = sqrt(r2-(xIndex(cnt)-x0)^2);
y01 = round(yIndex(cnt)-yOffset);
y02 = round(yIndex(cnt)+yOffset);
if y01 < numR && y01 >= 1
Accumulator(y01,x0) = Accumulator(y01,x0)+1;
end
if y02 < numR && y02 >= 1
Accumulator(y02,x0) = Accumulator(y02,x0)+1;
% rectangle('position',[x y 1 1],'EdgeColor','r');
end
end
end
% Finding local maxima in Accumulator
thresh=4;
y0detect = []; x0detect = [];
AccumulatorbinaryMax = imregionalmax(Accumulator);
[Potential_y0 ,Potential_x0] = find(AccumulatorbinaryMax == 1);
Accumulatortemp = Accumulator - thresh;
for cnt = 1:numel(Potential_y0)
if Accumulatortemp(Potential_y0(cnt),Potential_x0(cnt)) >= 0
y0detect = [y0detect;Potential_y0(cnt)];
x0detect = [x0detect;Potential_x0(cnt)];
end
end
figure,imshow(im);
for i=1:length(x0detect)
x0=x0detect(i);
y0=x0detect(i);
for x=1:numC
y=round(y0-sqrt(r2-(x-x0)^2));
if y<=numR && y>=1
rectangle('position',[x y 1 1],'EdgeColor','r');
end
end
end

Answers (2)

Cris LaPierre
Cris LaPierre on 22 Dec 2018
You are looping through every pixel in the image. Your comment (% find x,y of edge pixels) makes me think this may be unintentional. That is going to take a while.
There are also several nested for loops, which add to the run time.
You may try seeing if you can take advantage of matrix math to eliminate some of them.

Image Analyst
Image Analyst on 22 Dec 2018
Not really sure what you're doing due to lack of comments but I see where you're trying to dynamically enlarge an array:
y0detect = [y0detect;Potential_y0(cnt)];
That takes a lot of time. You might try to preallocate y0detect and x0detect at their maximum expected size, then crop afterwards to the actual size.

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!