Line detection using Hough Transfer

3 次查看(过去 30 天)
Ivy
Ivy 2021-6-23
编辑: DGM 2021-6-23
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);
  1 个评论
DGM
DGM 2021-6-23
编辑:DGM 2021-6-23
The error occurs here:
if BW (xi, (xi (yj) == 1 ) == 2)
I have no idea what the second index is supposed to mean, but it's nonsense in multiple ways.
(xi (yj) == 1 ) == 2
Both xi and yj are scalars that range from 1 to the respective size of BW.
xi(yj)
is always going to be an error if yj is anything other than 1. What is the conceptual purpose of indexing into a scalar index?
Even if it somehow worked, the result of
(xi (yj) == 1 )
is a logical value. It can never be equal to 2. The result of such an indexing operation would be an empty vector.
You'll have to figure out how to fix it or explain it, because I don't know what's being attempted.

请先登录,再进行评论。

回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by