MATLAB: Is it possible to obtain the length of lines from graph object only

2d plotline length

I need to calculate the total length of lines from a graph like this: http://prntscr.com/bdoz3e
It's difficult for me to calculate the length during the drawing process because there I plot parts of some lines over the others and I cannot simply sum them all up.
Is it possible to calculate it using some properties and data from figure handler or something similar?

Best Answer

Stefan
find attached script that measures length circuit tracks.
Explanation
1.- basic image processing: improve contrast, clean-up, translate to binary map
A=imread('circuit1.jpg');
A1=imadjust(A(:,:,1));A2=imadjust(A(:,:,2));A3=imadjust(A(:,:,3)); % improve image contrast
B=zeros(size(A));
B(:,:,1)=A1;B(:,:,2)=A2;B(:,:,3)=A3;
% figure(1);imshow(B);
threshold1=125;threshold2=125; % turn image into binary map, it misses some pixels
B(find(B>threshold1))=255;
B(find(B<=threshold2))=0; % basic image clean-up, converting to binary map
B(:,:,2)=and(B(:,:,2),B(:,:,3));
B(:,:,3)=[];
B=and(B(:,:,1),B(:,:,2));
B=logical(~B);
figure(1);imshow(B);
2.- remove background grid
[sz1B sz2B]=size(B);
B2=B; % remove horizontal background grid dashed lines
for k=1:1:sz2B
L=B2(:,k);
for s=2:1:(sz1B-1)
D=L([s-1 s s+1]);
if D(1)==0 && D(2)==1 && D(3)==0
L(s)=0;
end
end
B2(:,k)=L;
end
figure(2);imshow(B2);
B3=B2; % remove vertical background grid dashed lines
for k=1:1:sz1B
L=B3(k,:);
for s=2:1:(sz2B-1)
D=L([s-1 s s+1]);
if D(1)==0 && D(2)==1 && D(3)==0
L(s)=0;
end
end
B3(k,:)=L;
end
figure(3);imshow(B3);
3.- removing thin white lines along image frame, making sure that only circuit tracks are white
for k=1:1:sz2B % remove thin horizontal white line left behind
L=B3(:,k);
D=L([end-2 end-1 end]);
if D(1)==0 && D(2)==0 && D(3)==1
L(end)=0;
end
B3(:,k)=L;
end
figure(4);imshow(B3);
4.- measuring horizontal sections, in pixels
Ln_horz=zeros(1,sz1B);
for k=1:1:sz1B
L=B3(k,:);
for s=1:1:sz2B
if L(s)==1;
Ln_horz(k)=Ln_horz(k)+1;
end;
end;
end
% figure(5);stem(Ln_horz);
5.- removing measured pixels belonging to dots on circuit track crossing points
Ln_horz(Ln_horz<250)=0; % measurements <10 means just measuring cross dots, not paths

figure(6);stem(Ln_horz);
6.- measuring vertical sections
Ln_vert=zeros(1,sz2B);
for k=1:1:sz2B
L=B3(:,k);
for s=1:1:sz1B
if L(s)==1;
Ln_vert(k)=Ln_vert(k)+1;
end;
end;
end
figure(6);stem(Ln_vert);
7.- removing measured pixels belonging to circuit crossing points
Ln_vert(Ln_vert<80)=0; % measurements <10 means just measuring cross dots, not paths
figure(7);stem(Ln_vert);
%%
8.- counting peaks:
figure(1);plot(Ln_vert);
[vert_pks loc_vert_pks]=findpeaks(Ln_vert);
hold on;plot(loc_vert_pks,vert_pks,'rd');
figure(2);plot(Ln_horz);
[horz_pks loc_horz_pks]=findpeaks(Ln_horz);
hold on;plot(loc_horz_pks,horz_pks,'rd');
9.- the length measurement you are after:
vert_unit = 38 % amount of pixels per vertical unit
round(vert_pks/vert_unit)
% =

% 14.00 2.00 9.00 10.00 11.00
% 8.00 5.00 11.00 13.00 13.00
% 13.00 13.00 7.00 12.00 13.00
% 13.00 13.00 14.00 12.00 13.00
horz_unit=50
round(horz_pks/horz_unit)
% =
% 13.00 15.00 13.00 9.00 13.00
% 11.00 10.00 6.00 9.00 11.00
% 12.00 17.00 16.00
10.- circuit track length measured in circuit units:
length_tracks_circuit_units=sum(round(horz_pks/horz_unit))+sum(round(vert_pks/vert_unit))
=
374.00
circuit track length measured in pixels:
length_tracks_px=horz_unit*sum(round(horz_pks/horz_unit))+vert_unit*sum(round(vert_pks/vert_unit))
=
16072.00
If you find this answer of any help solving your question,
please mark this answer as accepted
thanks in advance
John