How to calculate the maximum width of object in binary image?

Here is the binary image and I want to get the maximum width of the object(the gap between the right and left edges). Currently I used minboundrect function to get this max distance on a rectangular bound.
I am thinking is there any function that can tell you the direction vector anywhere along the right edge, and determine tangent and normal vector at each edge, then extend it in each direction until it hits the edge of the left edge.
Appreciate any thoughts and helps on this. Thanks!

 Respuesta aceptada

Use bwdist():
edtImage = bwdist(binaryImage);
maxDiam = 2 * max(edtImage(:)); % Max() will give the radius.

12 comentarios

Hi Image Analyst, I want to draw a red line as a marker on the maximum width of the object. For which I need to get the x,y coordinates of the maximum diameter, Could you please guide me how to get the points/coordinates(P1 and P2 in the image)to draw the line.
leading_black = sum( cumprod( ~YourArray, 2), 2 );
trailing_black = sum( cumprod( ~fliplr(YourArray), 2), 2);
width_at_row = trailing_black - leading_black;
[widest, which_row] = max(width_at_row);
Hi Walter, The code does not work for me. I just want to find maximum width of a binary OBJECT in pixels(as Image analysis answered) and draw a red line at maximum width as I shown in the Image, for which I need to find out P1 and P2. I need help to find out P1 and P2 coordinates to plot a line on the image. I would appreciate a Matlab code to illustrate it on the given image(The one without red line)
"width" can have different meanings. The bwdist() meaning is basically: what is the largest disk you can fit into the blob such that the disk it completely enclosed - i.e. no part of the disk would touch any black pixels. Walter computed width by looking at the leftmost and rightmost white pixels of the object. So for example if you had a row (let's say row 800) that was white from columns 100 to 200, but then also had a thin L-shaped tendril that went over in row 700 but dipped down to pass through row 800 at column 500, my answer would give a diameter of 100 while Walter's would give an answer of 500-100 = 400 pixels wide. It's more like a "caliper" dimension. They're different definitions so you need to define if you want the largest width of the disk that can fit inside the blob, or if you want the caliper dimension of the blob, which would be wider.
Hi,Thanks so much for explanation. I just need to find maximum width(in pixels)of a binary object. Also i need to get x,y coordinates of the maximum width to draw a line on it. For example maximum width starts from columns 100 to 700 and rows 40,40. I need to automatically compute that coordinates as well to draw a red line on maximum width.
See attached code where I show a caliper method (different than Walter's) and Walter's method.
Image Analyst Method 2:
image width of 84 occurs at row 547
between column 5 and 110.
Walter Roberson Method:
image width of 84 occurs at row 547
between column 5 and 93.
Note that this is not the same as my bwdist() method, which computes the largest internal width, not the caliper width, and which can handles cases where the widest width is not lying perfectly along rows of the image, unlike these two caliper methods.
Thanks for the implementation. The algorithm is not generic for any kind of objects and its orientations, it only works fine when the object is in vertical position. Here are the results:
the results:
My original code can be extended to give you the location of the max diameter for blobs that are not vertical:
[rowOfMaxWidth, colOfMaxWidth] = find(edtImage == max(edtImage(:)))
viscircles(rowOfMaxWidth, colOfMaxWidth, maxDiam/2);
"The algorithm is not generic for any kind of objects and its orientations, it only works fine when the object is in vertical position."
Image Analyst's algorithm works to "get the maximum width of the object" when that has been defined to be "the gap between the right and left edges". If you have a different definition of "width" to substitute then you need to specify the new meaning of "width".
The first algorithm, using bwdist(), is not between leftmost and rightmost edges, it uses a definition of width based on the interior width, not the exterior width. The line across that width can be at any angle.
Hey, Thanks everyone. I am still hoping to get automatically line(Not circle) coordinates(i.e P1,P2 in the image) of the maximum width of an object of any orientation in the image.
I don't have any code for that now, so you can write it. I'd just look at all the pixels within +/- radius from the center row and column, and send a line out from the center to any pixel that is the radius away from the center. There should only be a few pixels that are that exact distance away. It's a brute force method but it's intuitive, easy, and fast (because there aren't that many pixels to examine). It's not hard, trust me. I want you to try it and then share the final code once you've got it working.

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

hyz
el 13 de Abr. de 2017

Comentada:

el 11 de Jun. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by