如何丢弃 Hough 检测到的一些线?
How can I discard some lines detected by Hough?
下面是我的原图,
这是我的霍夫代码,
function [hough_lines, H, T, R, P] = get_hough_lines( input_image , peaks)
binary_image = edge(input_image,'canny');
[H,T,R] = hough(binary_image);
P = houghpeaks(H, peaks, 'threshold', ceil(0.3*max(H(:))));
hough_lines = houghlines(binary_image,T,R,P,'FillGap',500,'MinLength',7);
end
用法
input_image = imread('7.png');
max_peaks = 3;
discard_pixels = 10;
[hough_lines, H, T, R, P] = get_hough_lines(input_image, max_peaks);
draw_hough_lines(input_image, hough_lines);
下面是我的线检测图,
如您所见,我的 Hough 源代码检测到 3 条最突出的行。
我要弃底线
所以,我写了下面的代码来丢弃图像中的一些区域,
function [output_image] = discard_image_area( input_image, pixel_count)
output_image = input_image;
[height, width] = size(input_image);
% discard top
output_image(1:pixel_count, :) = 125;
% discard bottom
output_image((height-pixel_count):height, :) = 125;
% discard left
output_image(:,1:pixel_count) = 125;
% discard right
output_image(:,(width-pixel_count):width) = 125;
end
用法
input_image = imread('7.png');
max_peaks = 3;
discard_pixels = 10;
[hough_lines, H, T, R, P] = get_hough_lines( discard_image_area(input_image,
discard_pixels), max_peaks);
draw_hough_lines(input_image, hough_lines);
但是,结果变得更糟,
相关源代码,
function draw_hough_lines(rotI, lines )
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
end
我解决了这个问题。
我用 0
代替了 125
。
function [output_image] = discard_image_area( input_image, pixel_count)
output_image = input_image;
[height, width] = size(input_image);
% discard top
output_image(1:pixel_count, :) = 0;
% discard bottom
h = height - pixel_count;
output_image(h:height, :) = 0;
% discard left
output_image(:,1:pixel_count) = 0;
% discard right
output_image(:,(width-pixel_count):width) = 0;
end
下面是我的原图,
这是我的霍夫代码,
function [hough_lines, H, T, R, P] = get_hough_lines( input_image , peaks)
binary_image = edge(input_image,'canny');
[H,T,R] = hough(binary_image);
P = houghpeaks(H, peaks, 'threshold', ceil(0.3*max(H(:))));
hough_lines = houghlines(binary_image,T,R,P,'FillGap',500,'MinLength',7);
end
用法
input_image = imread('7.png');
max_peaks = 3;
discard_pixels = 10;
[hough_lines, H, T, R, P] = get_hough_lines(input_image, max_peaks);
draw_hough_lines(input_image, hough_lines);
下面是我的线检测图,
如您所见,我的 Hough 源代码检测到 3 条最突出的行。
我要弃底线
所以,我写了下面的代码来丢弃图像中的一些区域,
function [output_image] = discard_image_area( input_image, pixel_count)
output_image = input_image;
[height, width] = size(input_image);
% discard top
output_image(1:pixel_count, :) = 125;
% discard bottom
output_image((height-pixel_count):height, :) = 125;
% discard left
output_image(:,1:pixel_count) = 125;
% discard right
output_image(:,(width-pixel_count):width) = 125;
end
用法
input_image = imread('7.png');
max_peaks = 3;
discard_pixels = 10;
[hough_lines, H, T, R, P] = get_hough_lines( discard_image_area(input_image,
discard_pixels), max_peaks);
draw_hough_lines(input_image, hough_lines);
但是,结果变得更糟,
相关源代码,
function draw_hough_lines(rotI, lines )
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
end
我解决了这个问题。
我用 0
代替了 125
。
function [output_image] = discard_image_area( input_image, pixel_count)
output_image = input_image;
[height, width] = size(input_image);
% discard top
output_image(1:pixel_count, :) = 0;
% discard bottom
h = height - pixel_count;
output_image(h:height, :) = 0;
% discard left
output_image(:,1:pixel_count) = 0;
% discard right
output_image(:,(width-pixel_count):width) = 0;
end