Calculating similarities in colors in RGB - colors

I'm writing a program that hides text in the image (Steganography).
I needed an algorithm that can calculate a pixel color to be similar to others in the vicinity. For example, this image.
The central pixel: the closest color to the neighborhood. But I need to get the same R G B value. For example: 200, 200, 200.

Related

Why are the colors in the 1931 CIE xyY chromaticity diagram white?

When we look at the 1931 CIE chromaticity diagram, represented within the x y plane of xyY space, it renders white colors (or close to white) at points of luminance like the D65 point highlighted here with E.
But why is this the case? The point for D65 is supposed to be represented at x = 0.33, y = 0.33. Given the formula Y = 1 - x - y, wouldn't that mean Y is 0.34?
The sRGB correlate or xyY at 0.33,0.33,0.34 is 158.4182, 155.5676, 176.8565 according to every converter I found. This is a light brown and not the near-white seen in every 1931 chromaticity diagram.
It seems like I need to scale the Y to get the proper luminance value for every channel.
Using the Y = 1 - x - y formula, my diagram looks like this, a muted diagram:
What don't I understand?
Edit
Setting Y = 1 and the diagram looks like the below, better.
Edit
Now looks like the below.
There is some imprecision on the interpretation of chromacity diagrams.
CIE xyY is a 3D figures. Often we see only a projections (often not a intersecting plane, just a projection).
One common projection is the "additive" xy chromacity diagram. You may notice it because it has yellow at border, and the white somewhere near the center. In such projection you show the maximum Y given a chromacity x,y.
Common is also the "subtractive" diagram, like your second one. No yellow, no white. This diagram has just the subtractive mix of the primaries, so the brighter colour are the primaries, and you get darken between them.
Note: usually the chromacity diagram are also extended also out of gamut, so the primaries are no more the real primaries, and white could not be white, and the yellow could be cut off, as your diagrams. You may try at first just the triangle between primaries, then expand. It is easier to debug.
The white will be just on top of 3D figure. In the first case, you take the outer surface of gamut, so you get the white. In the second case, you get a plane inside the figure, so you will never get white. But it is still a xy chromacity diagram.
On your case, I think you clipped the colour values (Note 1), which it is wrong: by clipping you will not get the correct chromacities (by clipping, one remove a certain value of a colour, so the ratio between channel is not maintained). One should use float or larger numbers for calculations, before to normalize (channel values in range 0 to 255). [Normalize (in this case): keep chromacity, but adapt Y so that final colour is in gamut]. In practice: you get the maximum value between R, G, B, and you multiply every channels by 255/max(R,G,B).
Note: this is not fully correct/precise. The above normalization should be done in linear space (light mix linearly), and only after normalization, the gamma funtion should be applied. On the other hand, on above figures, we do not have the correct colour for every point x,y. We can do it correctly only on a triangle (of gamut). By expanding the available colour on screen to full xz chromacity, we create errors/imprecisions. So normalization before or after gamma correction is not more so relevant (and it just change slightly the colours).
Note 1: From comment: this (clipping) it is not true, OTOH the very tiny part of blue (dark blue), and too much magenta and cyan, make me thinking about some numerical prolem)
The white point of CIE 1931 is not in x=1/3, y=1/3, and white color is not x=1/3, y=1/3, Y = 1/3.
According to Wikipedia:
The CIE 1931 color space chromaticity coordinates of D65 are
x=0.31271
y=0.32902
Since D65 represents white light, its co-ordinates are also a white point, corresponding to a correlated color temperature of 6504 K. Rec. 709, used in HDTV systems, truncates the CIE 1931 coordinates to x=0.3127, y=0.329.
The meaning of x=1/3, y=1/3 is different:
Light with a flat power spectrum in terms of wavelength (equal power in every 1 nm interval) corresponds to the point (x, y) = (1/3, 1/3).
Important: D65 is not a "flat power spectrum".
Computer systems (PCs) uses sRGB color format.
In sRGB the color components are after gamma (in contrast to CIE 1931 which applies linear curve).
In xyY color space, x,y are the chromaticity and Y is the luminance.
x=0.31271, y=0.32902 is the chromaticity without luminance and applies gray chromaticity.
For white color use Y = 1
Rec. 709, used in HDTV systems, truncates the CIE 1931 coordinates to x=0.3127, y=0.329
Lets compute sRGB of x=0.3127, y=0.329, Y = 1:
X = (Y/y)*x = 0.95046
Y = 1
Z = Y/y*(1-x-y) = 1.0891
Rlinear 3.240600 -1.537200 -0.498600 X 0.99984
Glinear = -0.968900 1.875800 0.041500 * Y = 1.00010
Blinear 0.055700 -0.204000 1.057000 Z 1.00007
Assume result is 1, 1, 1.
Last stage is applying gamma for converting "Linear sRGB" to sRGB.
Since all values are 1, the result is sRGB = 1, 1, 1.
We can repeat the computation for Y = 0.2, and the result is Linear sRGB = 0.2, 0.2, 0.2.
Apply gamma:
gamma(u) = 1.055*u^(1/2.4) - 0.055 for u > 0.0031308
1.055*0.2^(1/2.4) - 0.055 = 0.48453
So sRGB = 0.48453, 0.48453, 0.48453.
For converting to the standard range of [0, 255] (one byte per color channel), we need to scale by 255 and round the result: RGB888 = 124, 124, 124.

Color gradient based on point's Z value

I am trying to decide on the color of various points in a 3D coordinate system. The way I want to do it is to decide the RGB Values for a particular 3D Point based on the Z Cordinate value of that point, For the point having maximum Z value to have only Red color and for a point having minimum Z value to have only blue color. But I am not sure how to transition the values for R, G and B in between for all the points.
Thanks
RGB color scheme is not very useful for that. Throw a look at HSV.
You have to scale your Z to (0, 360) and set Saturation and Value to 1 and then convert this into RGB
Or if you have something like the matlab jet color map in mind:
Grayscale to Red-Green-Blue (MATLAB Jet) color scale

Create a false color palette and associate pixel values with it

I have raw pixel data (640x480 pixels) from an infrared camera which stand for a specific measured temperature. These pixel values have a 16 bit range from 0 to 65535.
I can display the pixel values as 8 bit greyscale, which works very well.
But now I want to display those pixels by using a false color palette.
I noticed 2 challenges here:
1.) Creating a false color palette. This means not just a simple RGB or HSV palette...I am thinking of a transition from black to yellow, to orange, to red and finally to purple
2.) Associating the pixel values to a color on my palette (e.g. 0 = black, 65535 = purple, but 31521 = ???)
Do you have an idea how I should approach this problem? I use Qt4 and Python (PyQt) but also I would be very happy if you just share the way for a solution.
One simple way would be to define colors at certain points in your range - as in your example, 0 is black, 65535 is purple, maybe 10000 is red, whatever you want to do. Set up a table with those key rgb values, and then simply interpolate between the rgb values of the key values above and below your input value to find the rgb color for any given value.
eg. if you're looking up the color for the value 1000, and your table has
value=0, color=(0,0,0)
value=5000, color=(255, 0, 255)
Then you would interpolate between these values to get the color (51, 0, 51)
The easiest method is as follows:
Cast your unsigned short to a QRgb type, and use that in the QColor constructor.
unsigned short my_temp=...;
QColor my_clr((QRgb)my_temp);
This will make your values the colors between black and cyan.

Generate next color in spectrum

everyone. How would I generate the next color in the color spectrum? Like, a function that takes a red value, a green value, and a blue value for input and output. I could input solid red (RGB 255, 0, 0) and it would output an orangish-red.
EDIT: Some more background info: I'm assuming the H, S, and V values have numeric ranges from 0-255. The C program I'm writing increments the hue value if it is less than 256, resets it to 0 if it's not, converts the HSV to RGB, displays the color on the screen, and loops. I've tried a couple HSV-to-RGB functions, but they're not working.
Instead of the RGB domain for colors, you should work with HSV values. This way, you can modify the H value to move around the spectrum.
Do you have to work with RGB values? If you don't, then use HSL as #sukru suggested, otherwise, try to convert it into HSL by following the instructions here, then increment the H value by 1/12, and convert to RGB.

How do I calculate a four colour gradient?

If I have four colours (A, B, C & D) on four corners of a square and I want to fill that square with a gradient that blends nicely between the four colours how would I calculate the colour of the point E?
The closer E is to any of the other points, the strong that colour should affect the result.
Any idea how to do that? Speed and simplicity is preferred to accuracy.
colours http://rabien.com/image/colours.png
The best solution when a gradient is required between two colors, is to use the HSV representation (Hue Saturation Value).
If you have the HSV values for your two colors, you just make linear interpolation for H, S and V, and you have nice colors (interpolation in RGB space always lead to "bad" results).
You also find here the formulae to go from RGB to HSV and from HSV to RGB, respectively.
Now, for your problem with the four corner, you can make a linear combination of the four H/S/V values, weighted by the distance from E to that four points A,B,C and D.
EDIT: same method than tekBlues, but in HSV space (it is quite easy to test it in RGB and in HSV spaces. And you will see the differences. In HSV, you just turn around the chromatic cylinder, and this is why it gives nice result)
EDIT2: if you prefer "speed and simplicity", you may use a L1-norm, instead of a L2-norm (euclidian norm)
So, if a is the size of your square and the coordinate of your points are A(0,0), B(0,a), C(a,0), D(a,a), then the Hue of a point E(x,y) can be computed with:
Hue(E) = ( Hue(B)*y/a + Hue(A)*(1-y/a) ) * (x/a) + ( Hue(D)*y/a + Hue(C)*(1-y/a) ) * (1-x/a)
where Hue(A) is the Hue of point A, Hue(B) the Hue of B, etc...
You apply the same formulae for the Saturation and Value.
Once you have the Hue/Saturation/Value for your point E, you can transform it in RGB space.
Check out this site, which gives a visual demo of #ThibThib's comment that "gradients in HSV will be more satifying":
http://www.perbang.dk/rgbgradient/
It is a gradient creator that will create and show BOTH an RGB gradient and an HSV gradient.
If you try 9 steps from FFAAAA to AAFFAA (light red to green), you’ll get a nice transition through light yellow, and the HSV and RGB ones look similar.
But try 9 steps from FF0000 to 00FF00 (bold red to green), and you’ll see the RGB one transition through a yucky greenish brown. The HSV gradient, however, transitions through bold yellow.
Determine the distance of point E to each point A,B,C,D
The color for point E will be the combination of Red / Green / Blue. Calculate each color axis as the average of the same color axis for A,B,C,D, ponderating by distance.
distance_a = sqrt((xa-xe)^2+(ya-ye)^2)
distance_b = ....
sum_distances = distance_a + distance_b ...
red = (red_adistance_a + red_bdistance_b ... ) / sum_distances
color_E = ColorFromARgb(red,green,blue)

Resources