S
S
Sergey2012-01-08 22:20:54
Image processing
Sergey, 2012-01-08 22:20:54

Getting region color?

There is a small piece of the picture, approximately 50*50 pixels. It contains a certain gradient of one color (say, yellow, or rather its various shades. This region is the center of a certain three-dimensional object, hence the gradation of shades).
There is a task to get the color of the object as a string. Well, tobish for 255; 0; 0 should give out that it is red.
There are not so many shades, about 10 basic ones (red, blue, ..., pink, orange)
In view of the short time, the task was solved by me in haste. The average color value was taken from the image (for each of the channels) and then the closest value is selected for the given "references". Since the object is glossy, there are highlights (usually white, or the colors of the light source), which introduces a significant error in determining the average value. I thought to solve this problem by filtering out values ​​that are very far from the average. Here already the brain began to fail.
When determining the color by an array of standards, there is also a significant error, but here it can be seen only by the experimental method, it is necessary to select standards.
The function itself for determining the color looks like this:

private string GetColorName(Bgr color)
        {
            string[] names = {
                                "Red",
                                "Blue",
                                "Green",
                                "Black",
                                "Orange",
                                "Yelow",
                                "Pink",
                                "White Or Transparent",
                            };

            Bgr[] colors = {
                               new Bgr(0, 0, 255),
                               new Bgr(255, 0, 0),
                               new Bgr(0, 255, 0),
                               new Bgr(0, 0, 0),
                               new Bgr(55, 160, 245),
                               new Bgr(55, 235, 245),
                               new Bgr(170, 190, 255),
                               new Bgr(255, 205, 175),
                           };

            double minDistance = 0;
            int colorIndex = 0;

            for (int i = 0; i < 8; i++)
            {
                double distance = Math.Pow(color.Red - colors[i].Red, 2) +
                                  Math.Pow(color.Blue - colors[i].Blue, 2) +
                                  Math.Pow(color.Green - colors[i].Green, 2);
                if (i == 0)
                {
                    minDistance = distance;
                    colorIndex = i;
                }
                if (distance < minDistance)
                {
                    colorIndex = i;
                    minDistance = distance;
                }
            }

            return names[colorIndex];


        }

Actually interested in whether there are ready-made implementations for obtaining the average value of the prevailing group of shades. I'm also interested in a more efficient/accurate way of color grading, although here I don't hope anymore.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Akson87, 2012-01-08
@Akson87

Try to treat your task as a clustering task. Those. there is a bunch of points in a 3-dimensional space corresponding to RGB and you need to find the largest cluster, most likely it will turn out to be very elongated, you can fit it into an ellipse and the center of the ellipse will be the desired color.
Even if the properties of the 3D model are known, you can think that this is a surface, it has normals, screw some kind of lighting model and try to restore the true color of the object. But this is so ... what comes to mind first.

E
Eternalko, 2012-01-09
@Eternalko

Maybe so?
pl.php.net/manual/en/function.imagecolorclosest.php
or like this:
www.phpbuilder.com/board/showthread.php?t=10355107

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question