Нашел на просторах интернета вполне себе подход к определению цвета и отчасти наличия фона на изображеии. Судя по всему его же можно применять для определения реального размера объекта на монотонном фоне.
A good way to solve this problem is to treat the image as a
graph, where there is one node ('component' in this answer) for each color filled area.
Here is one way to implement this approach:
- Mark all pixels as unvisited.
For each pixel, if the pixel is unvisited, perform the flood fill algorithm on it. During the flood fill mark each connected pixel as visited.
Now you should have a list of solid color areas in your image (or 'components'), so you just have to figure out how they are connected to each other:
Find the component that has pixels adjacent to the background color component - this is your figure border. Note that you can find the background color component by finding the component with the 0,0 pixel.
Now find the components with pixels adjacent to the newly found 'figure border' component. There will be two such components - pick the one that isn't the background (ie that doesn't have the 0,0 pixel). This is your figure background.
To find the pixel groups, simply count the number of components with pixels adjacent to the figure background component (ignoring of course the figure border component)
Advantages of this approach:
- runs in O(# pixels) time.
- easy to understand and implement.
- doesn't assume the background color and figure background color are different.
Жаль что, похоже, столько времени в институте было потрачено зря.