-1

I am new to image processing and python but interested to measure the distance in pixels for both axes (major and minor). Acknowledgment of this link for initial procedures on how to create boundary and axes (major and minor).

I have used the snippet in the link above to create the boundary by changing the parameter in the cv2.drawContours(orig, box.astype('int'),-1, (0, 255, 0), 3) to cv2.drawContours(orig, ctns,-1, (0, 255, 0), 3)to obtain the axes but not able to measure the distance as required. Kindly help me on how to achieve the expected results as per the attachments bearing in mind that the distance between 1 line and another in the bounded object is 1 pixel. The following are the attachments.

Kimwaga Makono
  • 121
  • 1
  • 8

1 Answers1

0

What I think you should do is this.Your cnts is an array of points, it is important that you find a way to define which points you want to measure the distances from.

For example, maybe the extremes? The largest and smallest x values and the largest and smallest y values. Or just points that have x1=x2 and y1=y2. After you decide which points you want to use, it is simple distance = √ x^2 + y^2 for the selected points.

For 1, you have to first find the center of the object. I would suggest this thread: OpenCV Centroid of Irregular Shape, and then you can apply same logic explained below.

for 2:

#say your contours array looks something like this.
cnts = [(12,12),(13,14),(14,16)...]

Lets say you select point 1:

selected_point = cnts[0] #(x,y) = (12,12)

Logically, the counterpart of this will have either the same x, or same y value, depending on if you are drawing the line vertically or horizontally. For example, if you are going horizontally, it will have the same y value. Therefore search the cnts array for the other point that has value of y=12, and measure the distances between them with x1-x2.

Ahmet
  • 434
  • 4
  • 13
  • 1. Suppose that I want to measure from the center of mass of the object to either directions until the **green** boundary how can I go through? 2. From the left side of the object to the right side without exceeding the green boundary. choosing any point as a starting point on a boundary – Kimwaga Makono Feb 06 '20 at 09:31
  • ...….. Thank you and sorry for a delay. This seems to be what I exactly want and I hope you are good at it. why can't you give me some snippets that I can be able so save the coordinates with the respective distance in an excel format eg: `(12, 23), (12, 32), d = 9` in three columns `p1` `p2` `d` – Kimwaga Makono Feb 12 '20 at 10:18