I am trying to implement Hough transform algorithm. Algorithm works, but it's slow. Currently i calculate rho, by this equation in two for loops:
for i = 1 : length(x)
j=1; for theta = -pi/2:nBinsTheta:pi/2-nBinsTheta ro =round(x(i).*cos(theta) + y(i).*sin(theta)); .... j = j + 1; end
end
How can i simplify this, to work without for loops? I need to calculate ro without loops, but how can i do this, to cover all possible theta's?
EDIT: Now i need to know how to add 1, to designated cell's in accumulator matrix given x and y coordinate vector. For example let's say that i have vectors like:
x: [1 2 1 3]
y: [1 3 1 4]
I'd like to solve this problem without loops. I know that i need to convert to linear indices using sub2ind, but the problem is that there'll be a lot of same linear indices for example that i gave, there will be 2x1 (1,1 coordinate is repeated twice). If you try to add 1 like so:
A([1 1]) = A([1 1]) + 1;
it'll add 1 only once, that's my problem.
Assuming x
and y
to be row vectors, you can use the following code to pre-calculate all ro
values in a 2D matrix which hopefully should speed things up for you inside the nested loops for the rest of the work you might be doing involving the ro
values -
theta_vec = [-pi/2:nBinsTheta:pi/2-nBinsTheta].'; %//'
ro_vals = round( cos(theta_vec)*x + sin(theta_vec)*y );