Width of contour for all y-values


I am trying to find the width of a contour, and the y-coordinate of this width. I am aware I can use boundingRectangle to find the max width, but from my understanding that only calculates the distance between extreme left and extreme right, regardless of y-value. I need to find the y-value where the contour is at its widest and narrowest.

Here is a representation of my problem, where green is the contour, blue is the already drawn bounding box and purple is an approximation of the width and y-value I want to find.

I have tried iterating through the list of points but I’m not getting the correct results.

Thank you for any help!

contours are the wrong data structure for some operations.

got the mask of this?

then find the leftmost and rightmost pixel for every scanline.

np.argmin and np.argmax can help with that.

1 Like

Hey and thanks for the reply.

I will try using scanlines instead, however I now encounter a different problem . I am using a trackbar threshold demo to find optimal thresholding values, however I am having difficulties separating the object from the background.

The object is very similar to the gray background, which I am assuming makes it difficult to create a good mask.
When toying with the treshhold values I get the following mask:

Here the value is 192, and this is the minimum value I can have for the object to be properly detected, however here there is some encroaching white pixels interfering with the image. The lower the value the less encroaching white, but then I don’t properly see the object.

I am applying Gaussian Blur and erosion on the image.

Is there anything I can do in the code to improve the mask or is this simply an issue with the original object being too similar to the background?

Any pointers are appreciated.

I am including the original grayscale image here since I could only post one image per reply:

why not place that thing on a background against which it contrasts better?

I have already recommended it but I am not sure it’s possible as there are some industrial restraints.

I have found a solution where I use adaptive thresholding and I do manage to get the contour of the object alright, with some white noise on the edges, so I will simply use this as a workaround to then use bitwise_and over a black image to only draw white pixels inside the object contour. Then I scan each individual line to find the width for each row.

Maybe not an ideal solution but it works fine for me and efficiency is not really and issue.

Thank you for the pointer to scanLine, it was exactly what I was looking for :slight_smile: