MATLAB: I would like to know what is the best way to detect a trapezoid shape or a local narrowing in a binary image

computer visionsurf

I am looking to find a way to detect a trapezezoid shape or a local narrowing in an binary image.
I show you two examples of what I would like to do in the drawing below
thank you in advance.
Laurent

Best Answer

I see several possible places that could be a trapezoid shape, not only the ones you outlined in red. What I would try to do it this, assuming it's fairly vertical
  1. Find the orientation and centroid of the whole shape with regionprops()
  2. Divide the shape in two and find the orientation of only the lower part below the centroid.
  3. Rotate the image by that orientation so that now the bottom part is pretty vertical.
  4. Scan down from top to bottom using find() to get the left and right boundary of each line.
  5. Find the place where the width goes from decreasing to fairly stable. This is the bottom of the trapezoid.
  6. Define some width that will define how wide the "top" of the trapezoid should be.
  7. Go up from the bottom until the width is the top width.
  8. Now you have all 4 points of the trapezoid.
Give it a try yourself, because we can't because you didn't attach your two initial binary images. Even if you did, it's good to try it yourself first. Post your code if you can't figure it out, and I'll try to help.