I cannot run this code and I am unable to determine where I went wrong. I am getting an error message: Index in position 1 exceeds array bounds (must not exceed 1).
close all
I=imread('Line Image.jpg');
imshow (I);
rotI=rgb2gray(I);
BW = edge(rotI, 'canny');
imshow (BW)
theta_sample_frequency= 0.01;
[x,y] = size(BW);
rho_limit= norm([x y]);
rho = (-rho_limit:1:rho_limit);
theta = (0:theta_sample_frequency:pi);
num_thetas = numel(theta);
num_rhos = numel(rho);
acc = zeros(num_rhos, num_thetas);
for xi = 1:x
for yj = 1:y
if BW (xi, (xi (yj) == 1 ) == 2)
for theta_id = 1:num_thetas
th = theta (theta_id);
r = xi * cos ( th ) + yj * sin ( th );
rho_id = round (r + num_rohs/2 );
acc (rho_id, theta_id) = acc (rho_id, theta_id) + 1;
end
end
end
end
%% show hough transform%
figure;
imagesc (theta, rho, acc);
title ('Hough Transform')
xlabel ('Theta (radians)');
ylabel ('Rho (pixels)');
colormap ('gray'); hold on;
%%extract the parameters
[M,I]= max(acc(:));
[rho_id, theta_id]=ind2sub (size(acc), I);
plot (thera(theta_id),rho(rho_id), 'o','LineWidth', 3);
hold off
%% compute the line coordinate
m = -(cos(600 )/sin(800)); %slope
b = rho (600)/sin(800); % the intercept
x = 1:x; % x coordinates
y = m*x+b; % y coordiantes
% - Note: remember the variation between Matlab image and plot coordinate
% system
% plot (x, m*x+b;) % it will be roated one
%% plot the detected line superimposed on original
figure;
subplot (1,2,1);
imagesc (BW);
colormap (gray);
hold on ;
plot (y, x, 'g', 'LineWidth', 2);
subplot (1,2,2);
imagesc (imputimage);
colormap (gray);
hold on;
plot (y, x, 'g', 'LineWidth', 2);