11

I'm trying to replace a manual process done in PhotoShop with an automated process done on the server. Currently in PhotoShop the "Color Range" tool is used to select a range of colors using the "Fuzziness" factor and starting with either Black or White depending on the part of the process.

My initial approaches included both using thresholds for Luminescence in the L*a*b color space as well is DE94 between the candidate color and Black/White. In both cases I selected colors that shouldn't be selected and/or didn't select colors that should.

My hunch is that I should be using cones instead of spheres for my selection.

Can anyone give any insight into what PhotoShop is doing and if I'm heading the right direction? Also, if there is a library out there to do this that would be awesome I'm currently writing this in C.

3
  • Regarding language of choice, I strongly recommend matlab at least for prototyping. There are some STL libraries in C++ which can also help.
    – ElKamina
    Nov 14, 2013 at 19:35
  • 1
    I'd consider trying to treat the r,g,b component as a 3d vector. You could then find the distance between your target and source colours by calculating the length of the vector between these two points. The Gimp has a similar functionality to Photoshop. You could always have a sniff through its source for a tried-n-tested approach.
    – enhzflep
    Nov 14, 2013 at 19:36
  • FWIW check out this OpenFrameworks C++ answer on doing RGB <-> CIE XYZ conversion (via Lab). The example uses the euclidean distance in the perceptual colour space (CIE XYZ) to find images with the closest average colour. Hopefully as a proof of concept it can be adapted to for colour range (XYZ euclidean distance with an adjustable threshold). Jan 24, 2019 at 16:06

3 Answers 3

5
+50

From what I have seen in Photoshop, the algorithm could probably be similar to the following:

  1. Define a function that calculates the closeness of two colors: for example, use a Euclidian distance in the colorspace - that is, calculate the distance between the colors of the two pixels in the RGB space using the Euclidean distance formula.
  2. Next, adjust the intensity of each pixel by using a fallof function, such as the Gaussian function. You will probably need to tweak some parameters. To clarify: you calculate the distance of two pixels in RGB space (not the distance in 2D pixel coordinates), and then feed that into the falloff function which will provide a result between 0.0 and 1.0. Multiply all color components of the current pixel with the result of the falloff function for it. Do this for each pixel of the image.
  3. If you want to add the range parameter of the effect, simply use the same falloff function for each pixel again, but this time feed it the Euclidean distance between the selected pixel and the current pixel in the 2D space of pixels (the distance between pixel coordinates on the image).

If you only want to select certain pixels, then instead of applying the effect directly at the pixels in the image you could store the falloff values in a matrix of doubles in range from 0.0 to 1.0. Then, choose a threshold value above which you will select the given pixel.

For example, if the step 2. for pixel at the coordinate (x, y) yielded 0.8 and the step 3. yielded 0.5, then the value of the matrix element with coordinates x and y should be 0.8*0.5=0.4. If you picked a selection threshold below 0.4, you would select pixel (x, y), otherwise, you would not.

1
  • I'm not actually wanting to apply and effect I want to select pixels in the same way that you would with the Color Range tool under the selection menu.
    – k.parnell
    Nov 25, 2013 at 17:32
3

I don't know how photoshop does it under the hood, but this is a simple RGB as XYZ 3d vector approach:

rDelta = pixel.r - color.r
gDelta = pixel.g - color.g
bDelta = pixel.b - color.b
fuzziness = 0.1  // anything 0 to 1.0
maxDistance = fuzziness * 441 // max distance, black -> white

distance = Math.sqrt(rDelta * rDelta + gDelta * gDelta + bDelta * bDelta)

if (distance < maxDistance) includePixel()
else dontIncludePixel()

This is the pixel_difference function from the gimp source:

https://github.com/GNOME/gimp/blob/125cf2a2a3e1e85172af25871a2cda3638292fdb/app/core/gimpimage-contiguous-region.c#L290

static gfloat
pixel_difference (const gfloat        *col1,
                  const gfloat        *col2,
                  gboolean             antialias,
                  gfloat               threshold,
                  gint                 n_components,
                  gboolean             has_alpha,
                  gboolean             select_transparent,
                  GimpSelectCriterion  select_criterion)
{
  gfloat max = 0.0;

  /*  if there is an alpha channel, never select transparent regions  */
  if (! select_transparent && has_alpha && col2[n_components - 1] == 0.0)
    return 0.0;

  if (select_transparent && has_alpha)
    {
      max = fabs (col1[n_components - 1] - col2[n_components - 1]);
    }
  else
    {
      gfloat diff;
      gint   b;

      if (has_alpha)
        n_components--;

      switch (select_criterion)
        {
        case GIMP_SELECT_CRITERION_COMPOSITE:
          for (b = 0; b < n_components; b++)
            {
              diff = fabs (col1[b] - col2[b]);
              if (diff > max)
                max = diff;
            }
          break;

        case GIMP_SELECT_CRITERION_R:
          max = fabs (col1[0] - col2[0]);
          break;

        case GIMP_SELECT_CRITERION_G:
          max = fabs (col1[1] - col2[1]);
          break;

        case GIMP_SELECT_CRITERION_B:
          max = fabs (col1[2] - col2[2]);
          break;

        case GIMP_SELECT_CRITERION_H:
          {
            /* wrap around candidates for the actual distance */
            gfloat dist1 = fabs (col1[0] - col2[0]);
            gfloat dist2 = fabs (col1[0] - 1.0 - col2[0]);
            gfloat dist3 = fabs (col1[0] - col2[0] + 1.0);

            max = MIN (dist1, dist2);
            if (max > dist3)
              max = dist3;
          }
          break;

        case GIMP_SELECT_CRITERION_S:
          max = fabs (col1[1] - col2[1]);
          break;

        case GIMP_SELECT_CRITERION_V:
          max = fabs (col1[2] - col2[2]);
          break;
        }
    }

  if (antialias && threshold > 0.0)
    {
      gfloat aa = 1.5 - (max / threshold);

      if (aa <= 0.0)
        return 0.0;
      else if (aa < 0.5)
        return aa * 2.0;
      else
        return 1.0;
    }
  else
    {
      if (max > threshold)
        return 0.0;
      else
        return 1.0;
    }
}
2
  • This sounds like the algorithm stated in the question, using RGB instead of LAB color space - LAB is probably better for this application. I'll also suggest squaring maxDistance and dropping the sqrt call for speed. Nov 14, 2013 at 21:44
  • Agreed, using two squares would be faster. I'd like to keep the code as is though for clarity. Nov 14, 2013 at 22:55
2

My educated guess would be that it is using an HSL color space, and the fuzziness is a parameter that selects all colors with a specific hue and saturation in a window of lightness (based on this).

Now the selection may just be doing a threshold calculation finding everything which is within that window (which would be a very small region in the colorspace). However, it may also be doing a statistical distance calculation. If the color sample actually small samples a window around the color, you can calculated the color variance and use something like a Mahalanobis distance calculation and threshold from that.

Again, that is just all conjecture, but maybe it'll help your thinking process.

Lastly, while this library does not have something like this directly implemented, OpenCV has many image processing tools that would make implementation easier.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.