How can I detect the line in a binary image?

조회 수: 3 (최근 30일)
Hao Yu Wang
Hao Yu Wang 2020년 6월 10일
댓글: Hao Yu Wang 2020년 6월 12일
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.

채택된 답변

KSSV
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 CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by