matlab中的霍夫变换以显示线条

Hough Transform in matlab to display lines

我想对我的图像使用霍夫变换。

I=imread('myimage.png');
I=im2bw(I);
BW = bwmorph(I,'skel',1);
[H, T, R] = hough(BW)
P = houghpeaks(H, 100);
lines = houghlines(BW, T, R, P, 'FillGap',5, 'MinLength', 20);

现在我想在单独的图像文件中打印所有行(每行在单独的图像上)。我该怎么做?

编辑- 我不想在背景中使用原始图像并在原始图像上画线 image.I 想要一个新图像,每张图像只有一条线.

您可以使用 findobj to fetch lines on the current axes and delete them one at a time right before plotting the next line. Right after that capture the content of the axes using getframe 完成并使用 imwrite 保存。

对于以下示例,我使用了 circuit.tif 演示图像并应用了 Canny 边缘来突出线条,但这应该适用于您的应用程序。

clear
clc
close all

I=imread('circuit.tif');
%BW = bwmorph(I,'skel',1);
BW = edge(I,'canny');

[H, T, R] = hough(BW);
P = houghpeaks(H, 100);
lines = houghlines(BW, T, R, P, 'FillGap',5, 'MinLength', 20);

%// NEW \
imshow(true(size(BW)));

hold on
for k = 1:numel(lines)

    %// Delete current line displayed
   CurrLine = findobj('Type','line');
   delete(CurrLine);

   %// Plot current line
   xy = [lines(k).point1; lines(k).point2];
   plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');

   drawnow

   %// Create file name and capture axes content
   CurrentFrame = getframe(gca);

   ImageName = sprintf('Image_%i',k);

   %// Save image
   imwrite(CurrentFrame.cdata,[ImageName '.jpg'],'jpg');

end

希望对您有所帮助!

您可以使用 Computer Vision System Toolbox 中的 insertShape 函数将线条绘制到图像中。