Measure width of rectangular object

5 views (last 30 days)
Jon Mestal
Jon Mestal on 13 May 2020
Answered: Image Analyst on 13 May 2020
I am trying to automatically measure the width of a rectangular object in an image. This is an MR image of a true rectangular object, but the image can show some imperfections; the example image has some bowing. Ideally, I would measure the width near the center of the object, parallel to the object edges rather than along the image axes. The object should be rougly in the same position and orientaton every time, but may move slightly (a few cm) based on the actual positioning when the object is imaged.
I was able to successfuly measure width using BoundingBox from regionprops, but that does not work well when the image is rotated slightly, and it is measuring form the longest width instead of near the center.
I think I need to fill in the binary image, then rotate and count the pixels, but not sure how to do so or if that is the best approach.

Answers (2)

darova
darova on 13 May 2020
Here is an idea:
  • binarize image
  • imclose image to get solid region
  • use for loop and imline to create several lines. Use createMask property to create mask of the line
  • calculate length of each line
I = I1 & mask;
[ii,jj] = find(I);
d(i) = hypot(ii(end)-ii(1),jj(end)-jj(1));
  • choose shortest line

Image Analyst
Image Analyst on 13 May 2020
I'd probably threshold, then scan the top and bottom to get the top line and bottom line, which may be slightly tilted. Then call fitPolynomialRansac() or polyfit() to get the angle. Then rotate with imrotate to level/straighten the image and then take a profile right through the center. Then look for full width, half max of the profile. Let us know if you can't figure it out.

Community Treasure Hunt

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

Start Hunting!