Good day,
quite rarely, but still the question arises of the need to automatically divide the image into logical fragments. If you are limited only by the means of PHP, then the task becomes a little more difficult, but still solvable.
In this article, I will consider a special case of pattern recognition, focused on a not very sophisticated audience.
The article uses examples from one of the sites with an explicit reference, the site is not mine, initially there were no thoughts to write an article.
So, we will consider the initial data

Rectangular pictures of various sizes and proportions, with a white background, on which are depicted the figures of the surprise kinder and the water mark, which sometimes touches the figures. The task is to get pictures of figures without a water mark.
')
The images are quite simple, so from the whole range of possible options (links are listed at the end of the article) it is reasonable to apply the
method of growing areas . Its essence is that we initially poke at an arbitrary point and compare the color of neighboring pixels, in our case the similarity criterion - the color should not be white. If the color is not white, we remember the coordinates of the pixel and compare its neighbors, so our area crawls until it reaches the edge of the segment. Then we poke at the next point and so on. It is clear to everyone that poking is completely random, this is not the most optimal way since it is possible to sort out empty pixels for a long time. This method is also called
the scorched earth method .
Integration 1. Selection of non-white areas
At this step, we get the height and width of the original image and read the color pixel-by-pixel in a double cycle pixel-by-pixel. All data is entered into a double array, if the pixel is white, we put 0, if not white, we put one. We display data on the screen for clarity and to check if we were wrong.

Php code$path = '../images/series/9/1.jpg'; $size = getimagesize($path); $img = imagecreatefromjpeg($path); $x = 1; $y = 1; $color = array(); for ($i = 1; $i < $size[0] * $size[1]; $i++) { $color_index = imagecolorat($img, $x, $y);
In the above code, the adjacent pixel is discarded if it is close to white, since at the jpg input we don’t have the best quality.
Paint over the resulting areas on the original image.

As you can see now our algorithm clings to pieces of garbage (therefore, in the above code there is filtering by color)
The erroneous approach, an attempt to select the boundary regions of the segments
Each of the segments can define its boundary points, for the border of the segment on the left side there will be points that do not have a colored pixel on the left, in a similar way for the upper, lower, and right borders.
We try to implement

Grind

We also grind, in the following picture, a more or less acceptable result, although many pixels are still captured by mistake.

Now, if we paint all the colored dots inside these constraints, we will end up with a picture in which you can already understand that these are smurfs)

From the human point of view, these segments can already be unambiguously divided, but from our algorithm it is still an array of ones and zeros, as well as an array of zeros and ones - only the borders of the segments (but now it’s still 1 array and the main question is how divide it according to segments).
On the original image, you can see clear white spaces between the segments, try to divide the segments according to the principle: “If we have large gaps (more than N) between the filled segments, then we divide the pixels in the main array on the left and right sides of the gap.”


Further, in the same way, we can make horizontal breaks, but they are harder to make because of the inscription in the center of the picture.
But at this moment, I realized that I was going in the wrong direction, so I finished these experiments.
So, back to the outcomes - we have a large array of units - colored pixels, how to divide them into segments?
Sprawl method
A brief description is given above (at the beginning of the article), so let's get down to business right away.

We set fire to the smurf, it is clear that the whole process goes through recursion, we color the interactions blocks into our colors.

Php code function nburn($ret, $color, $img, $col = false) { $ret_arr = array(); foreach ($ret as $k => $v) { foreach ($v as $k2 => $v2) { $ret_arr['numb'] = get_nearleast($color, $k, $k2); $stop = false; if (count($ret_arr['numb']) > 0) { if (count($ret_arr['numb']) == 1)
With this function, an array with pixels is divided into many sub-arrays (multi-dimensional array). Each sub array corresponds to one segmented image. Further, analyzing the resulting subarrays, we can remove the widest array that corresponds to the label.
A well-known example, I think everyone guessed the character.

However, sometimes the algorithm does not work correctly. For example, in the following picture, the first figure is divided into two because of a too white object in the hands of the character. In such cases, it is necessary to either fine tune the screened colors, or manual adjustment.

The placed code was written for itself, therefore, most likely, there are more elegant solutions.
Related Links:
UPDThanks to everyone who is a plus.