使用(rho,theta)在图像上绘制白线

时间:2017-06-19 13:24:21

标签: matlab

我想使用Hough变换使用检测到的线的(rho,theta)在下图上画一条线。

enter image description here

预期产出

enter image description here

由于y值为负值,因此无法显示该行。

input_image = gray_imread('Scratch1.1.png');
maxPeaks = 1; 
fillgap = 500; 
minline = 7; 
binary_image = edge(input_image,'canny');
[H,T,R] = hough(binary_image);
threshPeaks = ceil(0.3*max(H(:)));    
P  = houghpeaks(H, maxPeaks, 'threshold', threshPeaks);    
hlines = houghlines(binary_image, T, R, P, 'FillGap', fillgap, 'MinLength', minline);

h_line = hlines(1);

rho = h_line.rho;
theta = h_line.theta;

imshow(input_image);
hold on;
x = h_line.point1(1):h_line.point2(1);
y = (rho - x* cos(theta) )/ sin(theta);
plot(x,y);

这段代码有什么问题?

1 个答案:

答案 0 :(得分:1)

You did not need to create the y variable from the rho/theta values.

Instead, you can use the h_line.point1(2) value using the below code.

x = linspace(h_line.point1(1),h_line.point2(1),512);
y = linspace(h_line.point1(2),h_line.point2(2),512);

Which gives you the output Desired Output

Also, if you look at your original equation y = (rho - x* cos(theta) )/ sin(theta);

The outputs of the hough function are in degrees not radians which MATLAB's sin functions use. If you wish to use those, you will have to use sind and cosd

y = (rho - x* cosd(theta) )/ sind(theta);
相关问题