Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emulate PhotoShop's "Color Range" Algorithm

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.

like image 703
k.parnell Avatar asked Nov 14 '13 19:11

k.parnell


People also ask

How do I use the color range command in Photoshop?

To use the Color Range command, choose the Select > Color Range menu option. You’ll see the Color Range dialog appear: The most flexible way to use Color Range is in the “Sampled Colors” mode (choose from the drop-down box). Click with the mouse in the document window on an area of your image that contains the colour you want to select.

What is the use of the color range tool?

Color Range is a great choice of selection tool when the area you wish to select is distinctively different from the other colors within the image.

How do you select the same color in Photoshop?

The Select Option. We can select pixels that share the same or similar color just by clicking on an area of that color in the image. Photoshop "samples" the color we clicked on and selects all of the pixels that are the same as, or within a certain range of, that color (hence the name "Color Range").

How to use color range in AutoCAD?

To use the Color Range command, choose the Select > Color Range menu option. You’ll see the Color Range dialog appear: The most flexible way to use Color Range is in the “Sampled Colors” mode (choose Sampled Colors from the Select drop-down box).


1 Answers

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.

like image 162
Igor Ševo Avatar answered Oct 18 '22 14:10

Igor Ševo