Message Boards Message Boards

0
|
2334 Views
|
5 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Using nearest function on a list of sample colors represented by jpg files

Posted 8 years ago

Hi - I am new to Wolfram programming and am trying to figure out a way to construct a list of colors supplied by a number of jpgs, and then have it match an unknown color to the nearest match in the list. I can do this, and it happily outputs the image that is closest, but I'd rather know the index number (5th one in the list, etc) or have each known color paired with a label that I could retrieve on the nearest match. Can anyone point me towards some hints on how to do this. My example below uses simple colors, but the colors used in actual use will not be anything standard, so i need to know that item[x] in the array is closest to the sampled color.

Thanks! Scott

Eg:

Example

POSTED BY: scott shekels
5 Replies
Posted 8 years ago

Thank you both for the hints! I had some time today to play with this a bit more and its working quite well!

Cheers! Scott

POSTED BY: scott shekels
Posted 8 years ago

Marco - Thank you so much for the insight on how to do this! Wolfram is not like any of the other languages I have used, so its taking me a bit to wrap my head around it all!

I knew there had to be some way that I was missing!

Thanks! S.

POSTED BY: scott shekels

Hi,

suppose you have a reference list of colours:

list = RandomColor[30]

enter image description here

Then you have a random colour to identify:

inputcolor = RandomColor[]

enter image description here

This gives you the index of the closest colour in the reference list:

indexnearst = Flatten[Position[list, Nearest[list, inputcolor][[1]]]][[1]]
(*24*)

If you wan to identify the RGB values of that reference colour you can use:

List @@ list[[indexnearst]]
(*{0.687419, 0.556168, 0.166387}*)

Cheers,

Marco

POSTED BY: Marco Thiel

I'd recommend using just Nearest with a rule in the first argument:

indexnearst = Nearest[list -> Range[Length[list]]];

indexnearst[inputcolor]

POSTED BY: Daniel Lichtblau

Dear Daniel,

yes, that's much better. I did not like my "[[1]]" everywhere anyway. When that shows up in Wolfram Language Code something is usually much less elegant than it should be.

Yours is much cleaner!

Thanks, Marco

POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract