给定包含斜线的二值图像,我如何自动识别尽可能多的线?使用 Matlab 中的 bwtraceboundary
函数,我已经能够识别其中之一,手动提供识别线的起始坐标。
谁能指出一种循环 1 和 0 矩阵以自动识别尽可能多的方法?
这是一个示例图片:
% Read the image
I = imread('./synthetic.jpg');
figure(1)
BW = im2bw(I, 0.7);
imshow(BW2,[]);
c = 255; % X coordinate of a manually identified line
r = 490; % Y coordinate of a manually identified line
contour = bwtraceboundary(BW,[c r],'NE',8, 1000,'clockwise');
imshow(BW,[]);
hold on;
plot(contour(:,2),contour(:,1),'g','LineWidth',2);
从上面的代码我们得到:
最佳答案
这是一个小示例,说明如何在 MATLAB 中对线条使用霍夫变换,并对图像进行一些先验降噪。
此代码不会检测所有行,您可能需要对其进行调整/稍微更改,这将需要了解正在发生的事情,这超出了 StackOverflow 的范围。也许有更多知识的人可以找到更好的方法:
I=rgb2gray(imread('/image/fTWHh.jpg'));
I = imgaussfilt(I,1);
I=I([90:370],:);
BW = edge(I,'canny');
[H,T,R] = hough(BW);
P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',3);
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
关于image - 如何自动识别图像中的多行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49492530/