Houghlines output does not match rho theta?

조회 수: 6 (최근 30일)
Jeremy Young
Jeremy Young 2017년 7월 18일
댓글: Jeremy Young 2017년 7월 20일
How can I can I plot a rho and theta output from hough on my image?
I would like to extract the line information directly from the Hough transform (i.e. the function hough) rather than relying on houghlines to extract the information for me. The reason being I would like to be able to extract all points on a line. I have been trying to plot with rho and theta so I could better understand what houghlines is doing and why it excludes some points.
For example, going with the example given by Mathworks: https://www.mathworks.com/help/images/ref/houghlines.html
I = imread('circuit.tif');
rotI = imrotate(I,33,'crop');
BW = edge(rotI,'canny');
[H,T,R] = hough(BW);
imshow(H,[],'XData', T, 'YData', R, 'InitialMagnification','fit');
xlabel('\theta'),ylabel('\rho');
axis on, axis normal, hold on;
% P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
% x = T(P(:,2)); y = R(P(:,1));
% plot(x,y,'s','color','white');
P = houghpeaks(H,5,'threshold',ceil(.3*max(H(:))));
x = T(P(:,2));
y = R(P(:,1));
plot(x,y,'s','color','white');
%Find lines and plot them
lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
figure, imshow(rotI), hold on
max_len = 0;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% Plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
% Determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
I will get the following output:
The lines here correspond to:
rho = 127 170 138 155 32
theta = 56 56 56 56 -32
But if I try to plot rho and theta by converting back to (x,y) pairs solving the parametric function
rho = x*cos(theta) + y*sin(theta) --> y = -x*cos(theta)/sin(theta) + r/sin(theta)
I will get the resulting plot
First of all, all 5 lines appear to be parallel. There should be one that is close to perpendicular to the others. Secondly, these lines don't match any of the segments shown in the output from houghlines. They are somewhat perpendicular to what we would expect (rotate 90 degrees counter clockwise?). Do I have some sort of misunderstanding of how to use rho and theta?
This is the code I used to generate the plot from the rho and theta given:
rho = y;
theta = x;
xx = 1:size(BW,2);
yy=zeros(length(rho),length(xx));
for i=1:length(rho)
yy(i,:) = (rho(i) - xx* cos(theta(i)))/ sin(theta(i));
end
figure;
imshow(BW); hold on;
for i=1:length(rho)
plot(xx,yy(i,:),'g'); hold on;
end
hold off

채택된 답변

Don Zheng
Don Zheng 2017년 7월 20일
Use 'cosd' and 'sind' instead of 'cos' and 'sin', respectively, in your code.
  댓글 수: 1
Jeremy Young
Jeremy Young 2017년 7월 20일
Thank you so much. I cannot believe I didn't catch that the output of theta was in degrees instead of radians. I suppose I should of realized it when the values were much greater than 6!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품

Community Treasure Hunt

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

Start Hunting!

Translated by