© 2013 All rights reserved.
2

Image Search by color (PHP) #part1

Perhaps you’ve ever come across a problem that you need to search images by their color (like it has google at images.google.com).

hue

For this search it is necessary first to identify colors in the image. The predominant colors with the color ratio can be stored in a database and then search in this database by colors..

The problem is that in the classical picture are millions of colors that are represented in it. For image processing it is necessary to reduce the scale and work only with the basic colors.

We can reduce size the image to avoid redundant colors, which the image contains.

 

Image processing

 

If we have prepared the file, we have to go pixel by pixel and find its color.

We will create function with name getColorsFromImage that returns the array of colors and their frequency in the image.

The function assumes private variable with name image_size that contains the image dimensions and function getImage, which returns the modified image.

The whole class for image processing will look as follows:

 

Use in practice

 

We can create a simple demo application. Pictures can be stored in the array:

 

These images gradually go through and find their colors using Colorise class.

A simple example of image processing with a list of colors and frequency (code is only for demonstration):

The output of the class will look like this:


output-colorise-images

Comments (2)

hello

Urs

Great article! Thanks for sharing, it helps me a lot!

Just wondering, how it can be, that some RGB values are greater than 255
(e.g. sample in the middle, the color with 3,11207% has a value of 275 for red).
I have something similar for #ffffff with output as RGB of 275,275,275 by public function stringToColor($id).
Do you have a hint for me?
Thanks & best regards, Urs

Add comment

About
Hi, i am programmer from the Czech Republic. I love web development (Ruby, Ruby on Rails, PHP, Nette) and iOS development (Objective-C, Cocoa).
To cooperate, here is my phone:
+420 608 836