How can I detect the maximum-sized rectangle that I can draw onto the mask?

前端 未结 3 1732
温柔的废话
温柔的废话 2020-12-06 03:17

I\'m making an image processing project and I have stuck in one the project\'s steps. Here is the situation;

This is my mask:

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-06 03:40

    The black boundaries in your image are curved and not closed. For example, in the top right corner, the black boundaries won't meet and form a closed contour. Therefore, a simple strategy in one of my comments will not work.

    I am now providing you with a skeleton of a code which you can play with and add conditions as per your need. My idea is as follows:

    To find left-side x-coordinate of the rectangle, first count the white pixels each column of the image contains:

    %I assume that the image has already been converted to binary.
    
    whitePixels=sum(img,1);
    

    Then find the rate of change:

    diffWhitePixels=diff(whitePixels);
    

    If you see the bar plot of diffWhitePixels then you will observe various large entries (which indicate that the white region is still not in a straight line, and it is not a proper place to put the rectangles left vertical edge). Small entries (in your image, less than 5) indicate you can put the rectangle edge there.

    You can do similar things to determine right, top and bottom edge positions of the rectangles.

    Discussion:

    First of all, the problem is ill-posed in my opinion. What do you mean by maximum-sized rectangle? Is it maximum area or length of side? In all possible cases, I don't think above method can get the correct answer. I can think of two or three cases right now where above method would fail, but it will at least give you the right answer on images similar to the given image, provided you adjust the values.

    You can put some constraints once you know how your images are going to look. For example, if the black boundary curves inside, you can say that you don't want a column such as [0;0;...0;1;1;...0;0;...;0;1;1;...;1] i.e. zeros surrounded by ones. Another constraint could be how many black pixels do you want to allow? You can also crop the image till to remove extra black pixels. In your image, you can crop the image (programmatically) from the left and the bottom edge. Cropping an image is probably necessary, and definitely the better thing to do.

提交回复
热议问题