How can I detect the line in a binary image?
2 次查看(过去 30 天)
显示 更早的评论
Hi all,
I have a image as follow:
How can I use the built-in function to find the line within the white area?
In fact, I would like to obtain the vector of the line.
The shown below is an example of a possible output image.
0 个评论
采纳的回答
KSSV
2020-6-10
I = imread('image.png') ;
I1 = rgb2gray(I) ;
%% Remove the hwite background around the image
I1(1:5,:) = [] ; I1(:,1:5) = [] ;
I1(end-5:end,:) = [] ; I1(:,end-5:end) = [] ;
%
[y,x] = find(I1~=0) ; % get the two regions which are white
% USe kmeans to seperate into two groups
idx = kmeans([x y],2) ;
% get bounding box for each set
x11 = min(x(idx==1)) ; x21 = max(x(idx==1)) ;
y11 = min(y(idx==1)) ; y21 = max(y(idx==1)) ;
BB1 = [x11 y11 ; x21 y11 ; x21 y21 ; x11 y21] ;
%
x12 = min(x(idx==2)) ; x22 = max(x(idx==2)) ;
y12 = min(y(idx==2)) ; y22 = max(y(idx==2)) ;
BB2 = [x12 y12 ; x22 y12 ; x22 y22 ; x12 y22] ;
% get line for each
L1 = [mean(BB1(1:2,:)) ; mean(BB1(3:4,:))] ;
L2 = [mean(BB2(1:2,:)) ; mean(BB2(3:4,:))] ;
imshow(I) ;
hold on
% plot(x(idx==1),y(idx==1),'.r')
% plot(x(idx==2),y(idx==2),'.b')
plot(L1(:,1),L1(:,2),'r','LineWidth',3)
plot(L2(:,1),L2(:,2),'r','LineWidth',3)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Lighting, Transparency, and Shading 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!