How to find the distance between lines in a image in matlab

Hi All, I am trying to learn how to process images in Matlab. I am trying to find the vertical distance from the upper line to the lower one. I have a few questions as to how to start the process. Do I need to convert the image to binary in order to find the edges? How can I get the count of the pixels between these two lines?
Any guidance would be greatly appreciated to get me started in the right direction.
Thanks!
Added a JPG file.

 Risposta accettata

They don't look quite perfectly straight to me, so do you want to assume they're perfectly straight, or do you want to fit a curve to them, like a quadratic or something? Also, do you need the average separation, or the separation on a column-by-column basis? And, can we assume that the garbage along the edges is something we can either zero out or crop away? And are the lines roughly horizontal in every single image, or can they be at any arbitrary angle, like 45 degrees or whatever?

9 Commenti

Hi,
Yeah you're right. They're not perfectly straight lines. In face in certain images the lines do not stretch from one end to another, rather I can only see line segments. They're are basically top and bottom layers of a tablet coating. So I want to fit a curve and find the average distance between the two curves. And yes the garbage can be ignored.
In some cases the layers are cured and are not always horizontal.
First I'd crop off the easy, obvious edge garbage. Then I'd try to get rid of the noise using an ordinal filter, ordfilt2(). Then I'd sum horizontally and identify the line where you can split the image into the upper half and lower half images. Then for each image, threshold and use find() and put the coordinates of the thin line into polyfit(x,y,2) to fit a quadratic. Then calculate the distance between top and bottom curve for each column. Anyway, that's what I'd try first.
Thanks. I'll follow your steps and try to get the output.
I'll keep you posted and if its not a problem I'll get back to you if I'm stuck somewhere.
Bildori = imread ('output2.jpg'); Bildgray = rgb2gray(Bildori); level = graythresh(Bildgray); Bildbina = im2bw(Bildgray,level); Bild = Bildbina; [bi_z bi_s] = size(Bild); figure,imshow(Bild); q = 0;
% Sum all the rows horizontally and find the rows with 'more 1's' in it. % Basically to find the rows contailing the white lines
for i = 1:1:bi_z
A = Bild(i,:);
[zeile, spalte, v] = find( A == 1);
[m,n] = size(zeile);
if n > 50
q = q +1;
I(q,1) = i;
end
end
%Splits the matrix I which contains the row numbers of the lines into groups.
s = []; b = []; sb = []; [a1 b1 c1] = find (I< (I(1,1)+ 30)); [z1 s1] = size(a1); lasta1 = a1(z1,1);
s(1,1) = z1; b = a1; [zb1 sb1] = size (a1); sb(1,1) = 0; sb(2,1)= zb1;
if (z1+1) < q
[a2 b2 c2] = find (I< (I(lasta1 +1,1 )+ 30)& I > I(lasta1,1));
[z2 s2 ] = size(a2);
lasta2 = a2(z2,1);
s(2,1) = z2;
b = [a1 ; a2];
[zb2 sb2] = size (a2);
sb(3,1)= zb2 + zb1 ;
if (lasta2+1) < q
[a3 b3 c3] = find (I< (I((lasta2+1),1)+ 30)& I > I(lasta2,1));
[z3 s3 ] = size(a3);
lasta3 = a3(z3,1);
[zb3 sb3] = size (a3);
s(3,1) = z3;
b = [a1; a2 ;a3];
sb(4,1)= zb3 + zb2 + zb1 ;
if (lasta3+1) < q
[a4 b4 c4] = find (I< (I((lasta3+1),1)+ 30)& I > I(lasta3,1));
[z4 s4 ] = size(a4);
s(4,1) = z4;
b = [a1; a2 ;a3 ;a4];
lasta4 = a4(z4,1);
[zb4 sb4] = size (a4);
sb(5,1)= zb4 + zb3 + zb2 + zb1;
if (lasta4+1) < q
[a5 b5 c5] = find (I< (I((lasta4+1),1)+ 30)& I > I(lasta4,1));
[z5 s5 ] = size(a5);
lasta5 = a5(z5,1);
s(5,1) = z5;
b = [a1; a2 ;a3 ;a4 ;a5];
[zb5 sb5] = size (a5);
sb(1,6)= zb5 + zb4 + zb3 + zb2 + zb1;
if (lasta5+1) < q
[a6 b6 c6] = find (I< (I((lasta5+1),1)+ 30)& I > I(lasta5,1));
[z6 s6 ] = size(a6);
s(6,1) = z6;
b = [a1 ;a2 ;a3 ;a4 ;a5; a6];
[zb6 sb6] = size (a6);
sb(1,7)= zb6 + zb5 + zb4 + zb3 + zb2 + zb1;
end
end
end
end
end
[s_zei s_spalt] = size (s);
%To do polyfit for each group and find the distance
for h = 1:1: s_zei
R = [];
Rmax = [];
l = 0;
f = 0;
d = b(sb(h,1)+ 1 :1:sb(h+1,1),1);
for i = 1:1: bi_s
for j = 1:1:s(h,1)
R(j,i) = Bild(I(d(j,1),1),i);
end
end
end
I'm not sure how to do polyfit after this. This is how far I've got to.
Which one of those variables contains the x,y coordinates for the upper line and which is the coordinates for the bottom line. Attached is my polyfit demo.
for h = 1:1: s_zei
R = [];
Rmax = [];
l = 0;
f = 0;
d = b(sb(h,1)+ 1 :1:sb(h+1,1),1);
for i = 1:1: bi_s
for j = 1:1:s(h,1)
R(j,i) = Bild(I(d(j,1),1),i);
end
end
This part of the code separates the image and returns the image which only contains the lines. So for h = 2,3 I can see the part of the image which contains the lines. I have to write a program which automatically determines the line locations so I cannot possibly crop out the garbage on top and bottom of the image. Hence I get h to be from 1 to 4. Containing four parts i.e. the two lines which I want and the top and bottom garbage parts.
Hi, I actually have a very similar question as this. If you ever got the code to work, could you show it to me?

Accedi per commentare.

Più risposte (1)

With this function you can find the lines:
You get the coordinates from both lines and substract the top from the bottom one, that's the distance.

1 Commento

Hi,
Thanks. I think the solution is only of horizontal lines. But I need a general approach to measure the distance for all type of lines say even cured ones. I don't think the lines are always horizontal.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by