Finding center of a line

22 ビュー (過去 30 日間)
samir
samir 2012 年 1 月 30 日
Hi i need to find the center of a line.A robot needs to foolow this line in real time but my problem is how to find the center of a line.I used bwmorph(image,'thin',inf); and i get the that my thick line thins to a line with 1 pixel size.
so now the only thing that i need is to calculate the distance between the center of my image (blue +) and the line so i could say to my robot move to left so that the center of the line is equal to the image center. .

採用された回答

Kevin Moerman
Kevin Moerman 2012 年 2 月 2 日
If your edge/line is the only segmented object (only the line consists of ones) in your image then if M is your binary image (containing ones for white) then: [I,J]=find(M); %equivalent to find(M==1) gives you the I and J indices or image coordinates of the whole line. If your line is straight then the mean of these coordinates is the same as the mean of the first and last coordinate and thus gives you the middle.
If you have multiple objects in the image with ones, not just your line. Then you could filter those out based on size/shape criteria and do the above. Alternatively could create an adjacency matrix whereby you check whether pixels are one and also have neighbours in the line direction that are one. For the end points of the line the latter will be false which allows you to find them. But this depends on the quality and thickness of your line depictions. So not sure if this would work. Also you could develop a type of mask (filter) that will highlight the ends of lines. But I would go for the simple mean of the object approach.
Kevin
  1 件のコメント
samir
samir 2012 年 2 月 2 日
well i used find for finding all x and y and then i used mean x and mean y so i got the mean values of x and y where every pixel is 1.

サインインしてコメントする。

その他の回答 (2 件)

Image Analyst
Image Analyst 2012 年 1 月 30 日
How about using the Pythagorean Theorem? Look up MATLAB's hypot() function.
  3 件のコメント
samir
samir 2012 年 1 月 31 日
i will try it with bwboundaries and see what i get

サインインしてコメントする。


Kevin Moerman
Kevin Moerman 2012 年 1 月 31 日
If its a straight line then you could simply take the mean of your line coordinates (or even centre of gravity of the image object).
If the line is curved then sum up line segments to get total curve length (calculate point-to-point distances and use cumsum for instance), and then solve for (e.g. interpolate) what coordinate describes half of the curve length.
  1 件のコメント
samir
samir 2012 年 1 月 31 日
ok thank you for your help.
I did some modifications i zoomed with the camera to the line so the line is now more or less straight.I used edge() function and i get the edges .So now i have to find the coordinates of first white and the last white pixel pixel in middle row
[x,y]=size(image);
middle_x=x/2;
With the funciton find() i get every white pixel.So my question is how to get only the twho white pixels where x=middle_x

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by