# Get length in pixels between edges of two contours

Hi.

I'm using OpenCV 3.3.1 on Windows 7.

I want to get length (in pixels) between two contours. Here are thresholded and original images (figure may rotated by different angles):

This is what I need to get:

I can find centers of this figures:

but I dont know how to find distance from center to edge...

UPDATE

Thanks @StevenPuttemans for idea, I tried to implement it, but during this time found one more important note that figure's position is changing and it may be rotated by random angle (sorry, I should note about it earlier):

edit retag close merge delete

( 2018-01-11 05:51:13 -0500 )edit

@supra56 please man, start by reading the question ... he needs a way of defining the outer pixel border, not the actual distance between two points ...

( 2018-01-11 06:39:50 -0500 )edit

@StevenPuttemans.I will stop making comment from now on.

( 2018-01-11 07:46:10 -0500 )edit
2

@supra56 It's all good. Everyone makes mistakes, especially me.

( 2018-01-11 20:11:10 -0500 )edit

Oh come on ... I did not say you can no longer post comments. Just lately you have been replying without any consideration of what is actually being asked for and thats what I wanted to get you to understand. That is all ... let us not overdramatize this.

( 2018-01-12 03:11:02 -0500 )edit

Sort by ยป oldest newest most voted

If your objects appear in a fixed position I would follow these steps

1. Given both contours, check which one is right and which one is left, by looking at the x coordinate of center
2. Now for the left contour, look at all the points of your contour (loop over them) and pic the x coordinate which is smallest on the x axis compared to center.
3. Do the opposite for the second contour, looking for the largest x value, and thus the point most to the right
more

Thanks for idea, this may works in case contours are not rotaing. Unfortunatelly, my figure can have random angle of rotation, so in this case "lowest X coordinate" is not the most left edge of contour... (added this not in update)

( 2018-01-11 08:18:14 -0500 )edit

Will your object still be rectangular. Then you simply need to do edge detection, find the dominant edge direction and rotate your image to a horizontal position.

( 2018-01-11 08:35:41 -0500 )edit

May be you can use minArea for each shape

( 2018-01-11 09:37:54 -0500 )edit

hi everyone. have you find a solution for this ? can you share the code please ?

( 2020-11-01 15:04:55 -0500 )edit

Official site

GitHub

Wiki

Documentation