After the publication of the last
article , I completely scored an attempt to perform an algorithm using the HSV or Lab coordinates. Scored on the use of color libraries and in general on the script itself scored.
But something became boring and again it was done to work with the images and at the same time I wanted to correct the existing algorithm.
Script:
link
Decision
The ill spot of the algorithm was the identification of similar tones. It was sick because of not taking into account the brightness of similar colors. At the time of writing, I perfectly represented the problem that I would have to face in determining the brightness and I decided not to take it into account. It turned out that black and white images were not processed. Also quite saturated with colors and contrasting images gave out strange tones, which, as it were, were not in the picture.
It was all this why. The unique color identifier was defined as
(rg) * 1,000,000 + (rb) * 1000 + (gb) . Accordingly, colors having the same identifiers were similar. During processing, an ever greater error was introduced into the r, g, and b components of the color. Made the simplest rounding:
round (round ((rg) / $ error) * $ error) and so on.
')
Actually, the solution turned out to be extremely simple, but not without a nuance. I first determined the brightness of the color. This is the simplest arithmetic average, only reduced to an integer scale from 0 to n, where n in this case is the number of brightness intervals. Actually there is a big problem, the colors will actually be similar, but they will be processed separately and each one will be displayed. But it will be noticeable only in the images, where the colors located on opposite sides of the border will be caught. Ideally, such boundaries should not be strictly broken, i.e. depending on the current palette are shifted to one side or the other.
Also, due to the division of tones into brightness intervals, we had to increase the number of processed colors. Now, instead of 60,000, the algorithm processes 500,000 colors and the verification step is reduced from 20px to 10px. So perhaps slowing down.
Result
As a result, the picture has changed a lot for the better! The script produces more or less adequate colors on most images. B / W images also work, although bugs are possible with them.
Below are a few examples of work:





Additionally
I did not publish to the blog "Algorithms", because This is just a small change in the work of the previous version.
Many asked the script to determine the colors in the image. Actually
here . Inside there is a description.
UPD_1 At the request of some users, added color output in hex format after image processing. RGB need?
UPD_2 Added support for PNG.
UPD_3 Author of the photo of the red-haired girl: Elena Serebriakova.
UPD_4 Brightness recalculation according to the formula 0.299 * R + 0.587 * G + 0.114 * B. Changed the error. Added the ability to transfer colors from HEX to RGB format.
UPD_5 Reduced threshold error. Added the ability to define colors in the loaded photo. Just click in the right place on the image.