Matlab - 霍夫变换检测粗糙的线条(二值化后不完全笔直)
Matlab - Hough Transform detect harsh lines (not perfectly stright after binarize)
我正在尝试使用霍夫变换检测图像中的线条。我几乎明白了,但是二值化后的线条太粗糙了,不能被认为是直的(看图片,可能你需要看到它们的全尺寸)。有没有什么方法(也许是一些 "bwmorph" 操作)来软化二值化的线,并使它们更直以便霍夫变换更容易将它们检测为单线?
我现在的代码是:
F=getframe;
I = rgb2gray(frame2im(F));
BW = imbinarize(I, 'adaptive', 'Sensitivity', 0.35);
BW = bwmorph(BW,'thin', inf);
您不一定需要先骨架化,但您确实需要调整霍夫变换的参数,特别是您希望它如何检测峰值和填充间隙。这是我对您的图形 (https://www.mathworks.com/help/images/ref/houghlines.html) 进行的转换示例:
bw = (imbinarize(I, graythresh(I)));
dilatedImage = imdilate(bw,strel('disk',10));
thinnedImage = bwmorph(dilatedImage,'thin',inf);
[H,theta,rho] = hough(thinnedImage);
P = houghpeaks(H,20,'threshold',0);
lines = houghlines(thinnedImage,theta,rho,P,'FillGap',400,'MinLength',300);
figure, imshow(I), 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
输出如下所示:
我正在尝试使用霍夫变换检测图像中的线条。我几乎明白了,但是二值化后的线条太粗糙了,不能被认为是直的(看图片,可能你需要看到它们的全尺寸)。有没有什么方法(也许是一些 "bwmorph" 操作)来软化二值化的线,并使它们更直以便霍夫变换更容易将它们检测为单线?
我现在的代码是:
F=getframe;
I = rgb2gray(frame2im(F));
BW = imbinarize(I, 'adaptive', 'Sensitivity', 0.35);
BW = bwmorph(BW,'thin', inf);
您不一定需要先骨架化,但您确实需要调整霍夫变换的参数,特别是您希望它如何检测峰值和填充间隙。这是我对您的图形 (https://www.mathworks.com/help/images/ref/houghlines.html) 进行的转换示例:
bw = (imbinarize(I, graythresh(I)));
dilatedImage = imdilate(bw,strel('disk',10));
thinnedImage = bwmorph(dilatedImage,'thin',inf);
[H,theta,rho] = hough(thinnedImage);
P = houghpeaks(H,20,'threshold',0);
lines = houghlines(thinnedImage,theta,rho,P,'FillGap',400,'MinLength',300);
figure, imshow(I), 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
输出如下所示: