How can I detect the line in a binary image?

3 Ansichten (letzte 30 Tage)
Hao Yu Wang
Hao Yu Wang am 10 Jun. 2020
Kommentiert: Hao Yu Wang am 12 Jun. 2020
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.

Akzeptierte Antwort

KSSV
KSSV am 10 Jun. 2020
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)

Weitere Antworten (0)

Kategorien

Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by