My function is stuck in a loop
6 views (last 30 days)
Show older comments
I'm writing an function to detect corners in certain images, it runs forever. If I pause after 2 minutes, it ends up in the function INDEX listed at the bottom ([edit Rik: the give_index function was bold]). BAsed on my playing around with the ebugging tool, it never leaves the INDEX function. The value of INDEX is always zero. I'm not sure why, can anyone deliver some insight?
I apologize for the code dump, please ask any questions if you need clarification.
clc
clear
close all
IMAGE_1 = imread('Building1.jpg');
IMAGE_2 = imread('CheckerBoard.jpg');
SIGMA = 1;
X = -2:1:2; % Setting up gaussian filter
H = [];
for i = 1 : 5
H(i) = exp(-X(i)^2/(2*SIGMA^2))/(SIGMA * sqrt(2*3.14));
end
H = H / min(H);
H = H / sum(H);
I_s = uint8(conv2(H,H', IMAGE_1));
[row,col] = size(I_s);
grad = [-1 0 1]; % Setting up gradient
E_x = conv2(I_s,grad);
E_x = imresize(E_x,[row, col]);
E_y= conv2(I_s,grad');
E_y = imresize(E_y, [row,col]);
INPUT = 'Input N \n';
N = input(INPUT);
INPUT = 'Type desired Tau \n';
TAU = input(INPUT);
L = [];
for i = 2:row-1
for j = 2 : col -1
C = [E_x(i,j)^2,E_x(i,j)*E_y(i,j);E_x(i,j)*E_y(i,j),E_y(i,j)^2];
TEMP = eig(C);
if TEMP(2) > TAU
L(end+1,:) = [TEMP(2),[i,j]];
end
end
end
L = sortrows(L,1,'descend');
[row_L, col_L] = size(L);
for i = 1 : row_L
% Check N
TEMP = L(i,:);
row_temp = TEMP(2);
col_temp = TEMP(3);
% Determine appropiate matrix
if row_temp-N>1 && row_temp+N<row && col_temp-N>1 && col_temp+N<col
for m = row_temp-N : row_temp+N
for n = col_temp-N : col_temp+N
if m ~= row_temp && n ~= col_temp
L_row_ind = find(L(:,2) == m);
L_col_ind = find(L(:,3) == n);
INDEX = give_index(L_row_ind,L_col_ind);
if INDEX ~= 0
L(INDEX,:) = 0;
end
end
end
end
end
end
%% delete rows contain 0 i.e. garma2 = 0
L = L(any(L,2),:);
plot(L(:,2),L(:,3),'x')
function INDEX = give_index(x,y)
INDEX = 0;
for i = 1 : size(x)
for j = 1 : size(y)
if x(i) == y(j)
INDEX = x(i);
return
end
end
end
end
0 Comments
Accepted Answer
Rik
on 22 Apr 2020
Edited: Rik
on 23 Apr 2020
That give_index function looks like it is very slow and may return unexpected results for non-integer inputs. It will also only operate along the first dimension, because size returns a vector. You should consider creating such loops with numel instead of size.
clear
%generate random integers to avoid floating point rounding errors
x=randi(10,100,1);
y=randi(10,100,1);
isequal(give_index(x,y),give_index2(x,y))
%suggested function
function INDEX = give_index2(x,y)
L=ismembertol(x,y,2*eps,'DataScale',1);
if any(L)
INDEX=x(find(L,1));
else
INDEX = 0;
end
end
%your function for reference
function INDEX = give_index(x,y)
INDEX = 0;
for i = 1 : size(x) % should be replaced by numel(x)
for j = 1 : size(y) % should be replaced by numel(y)
if x(i) == y(j) % should be replaced by abs(x(i)-y(i))<=2*eps
INDEX = x(i);
return
end
end
end
end
0 Comments
More Answers (1)
Image Analyst
on 23 Apr 2020
You say
if x(i) == y(j)
Are x and y floating point numbers? If so, see the FAQ: https://matlab.fandom.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
You should use ismembertol().
0 Comments
See Also
Categories
Find more on Matrix Indexing 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!