Finding colors in images: Can Nearest do it?

I am trying to find a way to search for colors in images. Here's a simplified example:

tree = ExampleData[{"TestImage", "Tree"}] 

tree image from Mathematica's example data set

I see there blue there, so I want xy place somewhere in this sea of โ€‹โ€‹pixels. Suppose I am looking for a specific shade of blue that I can provide approximately approximate RGB values โ€‹โ€‹for:

 Manipulate[Graphics[{RGBColor[r, g, b], Disk[]}], {r, 0, 1}, {g, 0, 1}, {b, 0, 1}] 

simple color mixer

and now I want to find the coordinates of some pixels that have this value, or close enough. Nearest could do this:

 Nearest[ImageData[tree], {0.32, 0.65, .8}] 

but not ... it generates a very big conclusion ...

This is done in reverse:

 ImageValue[tree, {90, 90}] 

itโ€™s normal if I already have numbers or you can click on the image. As soon as I find out the location of the colors, I can pass this on to functions that require โ€œmarkers,โ€ such as RegionBinarize .

I believe that there must be a Mathematica function for this, but she cannot find it yet ...

+7
source share
3 answers

it

 Position[#, First@Nearest [Flatten[#, 1], {0.32, 0.65, .8}]] &@ ImageData[tree] (* {{162, 74}} *) 

do what you want?

OK, try the following:

 tree = ExampleData[{"TestImage", "Tree"}]; dat = Reverse[ImageData[tree]\[Transpose], {2}]; dim = Dimensions[dat][[{1, 2}]]; nearfunc = Nearest[Join @@ dat -> Tuples @ Range @ dim]; Manipulate[ rgb = Extract[dat, Ceiling[p]]; posns = nearfunc[rgb, num]; Graphics[{ Raster[dat\[Transpose]], Red, Point[posns] }], {{p, {10, 10}}, Locator}, {{num, 20}, 1, 100, 1} ] 

this allows you to click somewhere on the image, determines the number of points that are closest (in accordance with the default rate) to the color of that point, and displays them. num - the number of points displayed.

It looks like this:

Mathematica graphics

+6
source

There are several problems with what you are trying to do.

  • You need the position coordinate, not the closest value

  • Nearest can return many values, not just one (use the third argument to specify)

  • Nearest asks for a list of values โ€‹โ€‹to search, not a table

You probably want something like this:

 Nearest[Join @@ ImageData@tree , {0.32, 0.65, .8}, 1] Position[ ImageData@tree , #] & /@ % 
  {{0.321569, 0.65098, 0.8}} 
  {{{162, 74}}} 

Do not miss the chance to build NearestFunction for efficiency if you are going to use it dynamically. Here is a more complete example:

 tree = ExampleData[{"TestImage", "Tree"}] findcolor[img_Image] := DynamicModule[ {dat, nearfunc}, dat = ImageData@img ; nearfunc = Nearest[Join @@ dat]; Manipulate[ Column[{ Graphics[{RGBColor[r, g, b], Disk[]}], Position[dat, nearfunc[{r, g, b}, 1][[1]]] }], {{r, 0.5}, 0, 1}, {{g, 0.5}, 0, 1}, {{b, 0.5}, 0, 1} ] ] findcolor[tree] 

Mathematica graphics

+4
source

Not quite the answer to the question, but you can find ideas in this:

 image = ExampleData[{"TestImage", "Tree"}]; red = Image@ConstantArray [List @@ Red, ImageDimensions[image]]; threshold = 0.15; p = ImageDimensions[image]/2; Row[ {VerticalSlider[Dynamic[threshold]], LocatorPane[ Dynamic[p], Dynamic[ colour = Extract[ImageData[image], Ceiling[p] /. {x_, y_} :> {ImageDimensions[image][[2]] - y, x} /. 0 -> 1]; mask = Binarize[ImageApply[Abs[# - colour] &, image], threshold]; Image[ ImageCompose[image, {SetAlphaChannel[red, mask], 0.5}], Magnification -> 1 ] ] ], Dynamic@ Graphics[{}, Background -> RGBColor @@ colour, ImageSize -> ImageDimensions[image]] } ] 

Mathematica graphics

+3
source

All Articles