我正在尝试理解MATLAB用于Hough变换的代码。
这张图片中有一些我已经明白的内容:
binary_image
是input_image
的单色版本。hough_lines
是包含图像中检测到的直线的向量。我看到已检测到四条直线。T
包含图像在 (ϴ, ρ) 空间中的θ值。R
包含图像在 (ϴ, ρ) 空间中的ρ值。
我有以下问题:
- Why is the image rotated before applying Hough Transform?
- What do the entries in
H
represent? - Why is
H
(Hough Matrix) of size 45x180? Where does this size come from? - Why is
T
of size 1x180? Where does this size come from? - Why is
R
of size 1x45? Where does this size come from? - What do the entries in
P
represent? Are they(x, y)
or(ϴ, ρ)
?29 162 29 165 28 170 21 5 29 158
- Why is the value 5 passed into
houghpeaks()
? - What is the logic behind
ceil(0.3*max(H(:)))
?
相关源代码
% Read image into workspace.
input_image = imread('Untitled.bmp');
%Rotate the image.
rotated_image = imrotate(input_image,33,'crop');
% convert rgb to grascale
rotated_image = rgb2gray(rotated_image);
%Create a binary image.
binary_image = edge(rotated_image,'canny');
%Create the Hough transform using the binary image.
[H,T,R] = hough(binary_image);
%Find peaks in the Hough transform of the image.
P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
%Find lines
hough_lines = houghlines(binary_image,T,R,P,'FillGap',5,'MinLength',7);
% Plot the detected lines
figure, imshow(rotated_image), hold on
max_len = 0;
for k = 1:length(hough_lines)
xy = [hough_lines(k).point1; hough_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(hough_lines(k).point1 - hough_lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
% Highlight the longest line segment by coloring it cyan.
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan');
hough()
、houghpeaks()
和houghlines()
。 - user366312