Search code examples
matlabimage-processingcomputer-visionpattern-matchingdimensions

Extract vertical and horizontal dimensions of an irregular object


I have the following object and want to extract the maximum vertical (red line) and horizontal (blue line) sizes in MATLAB.

enter image description here

I used the following code, but I am not sure if I am using a right property:

L = bwlabel(myImage);
prop = regionprops(L,'BoundingBox');

Solution

  • If you want another answer that doesn't use regionprops, assuming that the black mass is the only object in the image, you can find the minimum spanning bounding box by finding the top left and bottom right corners of the object, then finding the width and height by subtracting the right-most column and left-most column and bottom most-row and top-most row of whatever is considered an object pixel respectively. Assuming your binary image is stored in L, do the following:

    [r,c] = find(~L);
    width = max(c) - min(c) + 1;
    height = max(r) - min(r) + 1;
    

    find finds all row and column locations that are non-zero. As such, I had to invert your image so that all zero locations become non-zero. r and c are column vectors that return the row and column locations of what is non-zero respectively then the logic I stated above is carried out.