简体   繁体   中英

Setting every pixel in image to closest match from a list of Colors

How can I set the colour of every pixel in a image to it's closest colour match from a list of colours in RGB format (no alpha), that can be of any length, in C#?

It's basically creating a custom BitmapPalette, but since you can't do that (Trust me, I've tried everything possible for that), I need an alternative.

Does anyone know a way to do this?

Boy...I hope you loves your maths...

This is a tough question. To determine the "closeness of fit" between two colors, you first must understand the color space/color model in which your are working. The RGB color model (not counting the alpha channel) is essentially Euclidean in nature: each color maps to a point in 3D space. Ergo, the putative distance between two colors, C1 and C2 is

 Distance = SQRT( (C1 red - C2 red ) 2 + (C1 green - C2 green ) 2 + (C1 blue - C2 blue ) 2 )  

WRT "normal" human visual perception, this is not necessarily correct. To take that into account gets much more complicated.

Try these two papers as jumping-off points:

The Color FAQ also provide many links to other colorspace resource s.

Some more links at http://www.golden-gryphon.com/software/misc/color-links.html

Here's a paper on color differences that might help also: http://www.axiphos.com/Reports/ColorDifferences.pdf

Bruce Lindbloom's web site has lots of stuff as well, including a color difference calculator, that works in the CIE color space (which has provision for distance computations).

ColorMine is open source C# library that has methods for converting between color spaces and comparing via a couple delta-e methods

For example, this will give you a similarity score based on the most common delta-E method (Cie76)

var a = new Rgb { R = 23, G = 117, B = 114 }
var b = new Rgb { R = 113, G = 27, B = 11 }
var deltaE = a.Compare(b,new Cie1976Comparison());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM