custom colour space transformation in ffmpeg? - python-3.x

I was wondering if anyone knows if there is a custom colour space transformation command in ffmpeg? So that I can specify where I want the corners of the colour space to move to, which might be different in different situations.
The problem i'm facing at the moment is that the colour always seems to come out slightly different. If i use gamma correction in FFMPEG, then it translates the space, but doesn't transform or stretch the colour space triangle to fit the new colour space triangle. Which means that for example if I match a colour by way of empirical binary search, it won't match to the other colours.
Also, i can't seem to find a pre-defined colour space transformation in FFMPEG that matches the colour swatches I have.
I'd like to try and automate transformation of colour space in video by way of optimisation, so that it finds the corners of the colour space for me based on how it renders out, and what colour is expected, and was wondering if something like this is possible... ?
Thanks in advance for any insight or suggestions regarding this.

Related

Finding nearest color in RGB?

I have to find the nearest color. For example, I have two colors colorA1, colorA2 which are nearly same color. And also I have other color colorB1.
And I need such a method:
Color getNearestColor(colorA1, colorA2, colorB1). This method should give me the colorB2 which is calculated by using the difference of colorA1 and colorA2, then using their distance it should give me colorB2 which has the same distance as in colorA1 and colorA2.
Can you give some ideas how to implement it?
To find the nearest colour, you need a definition of "near", so a metric.
In Wikipedia you will find different metrics of color differences.
Personally I would use the 2*R*R + 4*G*G + 3*B*B. (no need for square roots, you will just compare same metrics). Easy to calculate, you can use just integers (if you use 32 bit integers, you will have no overflow).
Then find which colour has the smallest differences between your target colour.
The other methods are more precise, but in that case "RGB" is not enough. You need to know which colour space are using (probably you are in sRGB).

What is the relationship between color space RGB, XYZ and the color matching function?

What is the relationship between color spaces (RGB, XYZ) and the color matching function? Let's say we have some color matching function in the color space XYZ (3 row matrix). We also have the transformation matrix which translates from XYZ coordinates to RGB coordinates.
My understanding is that there is some visual input, which is made up of the color spectrum S(y). The human eye does not see the world - it only sees its interpretation of the world. The human eye has 3 cone types LMS, each of which is responsible for processing RED, GREEN, or BLUE. The human eye sees the spectral color only because it's eye sums over RED, GREEN, BLUE vector, and this sum matches the color of the input. In order to match the color, there is a color matching function, which takes the input spectrum and produces the weights by which to multiply the primary RED, GREEN, BLUE color vector. These then get added and their output visually matches the spectral input, even though the spectrum had many many frequencies added, while the human eye was only adding 3. So we went from HUGE space to space where we can describe all with 3 vectors, summed as dictated by the color matching function.
The spectral input, color primaries, and color matching functions behave as described above and can be summarized in this formula:
where pi is the 3d vector of primary colors, c - color matching function is also a vector of 3 components, and finally s is the spectral input.
We have XYZ color space, and a corresponding color matching function which does what is described above. We are then given matrix T, which transforms XYZ coordinates to RGB coordinates. We already know T, and we need to use it to produce a new color matching function for the RGB color space.
I do not understand how the color space relates to choice of primaries pi(λ) and the choice of color functions ci(λ1).
I have been trying to understand about colours from months and after some research, i believe I have some insights which probably can help me answer your question.
I do not understand how the color space relates to choice of primaries
pi(λ)
Primaries are nothing but the wavelength of the colors that we choose to use for making all the other colors in space and that also defines the gamut of the colour space. So if you play with the applet provided in the link that is given below you can see that the whole gamut in the colour space changes when you change your primary.
Have a look at Alternative primaries and gamuts section.
Now I do not know how much you understand the RGB and XYZ or what do you mean when you say RGB here (assuming you are referring to sRGB gamut values); XYZ are actually Tristimulus values which are called rho, beta and gamma as shown in the image above and just for simplicity XYZ are converted to xy space from where you get your standard sRGB gamut.
Please go through this if you are interested in understanding how colour sensors work and converting sensor values to XYZ matrix
Please comment if I have missed any information or answer needs editing.
I think lots of issues with color selection are due technical problems people had to solve. Usually you are not trying to reproduce colors as accurately as possible, but to make them pleasant looking, cheap, fast to calculate on cpu.... If someone watches plains of New Zealand on TV he is very unlikely to know they really look like, but almost certainly wants to enjoy the picture and pay little for it.
Several reasons why you might want to use different color matching functions might include:
You are taking pictures under non-white light and you want your picture to look natural.
You are taking underwater pictures and want to compensate for the fact that water attenuates different frequencies at different speeds.
Your sensor is not perfect and you want to compensate for that.
On the other hand you might want to change your primaries due to some reason. For example your images might be taking a picture of a scene with limited amount of colors. By nudging your primaries a little you might get a "fuller" picture.
Finally sometimes you just have to compensate for some of the limitations you have with your devices. Your phosphorus on CRT TV will impose some restrictions. So will the noise in air when transmitting using PAL. On the other hand if you go digital you might be forced to have less than 36 bits per pixel. In that case you will have to make compromises and this will give you opportunity to lose as little as possible.
If you want a short tutorial visit Cambridge in colour.
Here is a Szeliski's textbook on photography, look at chapters 1 2 and 10.
Poyton has list of common transformations.

Mapping RGB/hex color codes to general color categories

Is there a dataset that maps each of the ~16M RGB or hex color values to a general color family/category - e.g. red, purple, orange, beige, brown, etc. - that I could access programmatically or load into a database or JSON document to cross-refence the color codes against? The use case is to classify the results of PIL color detection of swatch files into a small set of color pickers for a shopping site. It would also work if the mapping is a bit more granular, say 100-200 categories, since it would be easy enough to map those to my target 10-15 myself. I have some knowledge of kNN classification and will work with that if I have to, but it would be so much easier to use a static mapping if one already exists.
You can use a table such as the one in X11
http://www.astrouw.edu.pl/~jskowron/colors-x11/rgb.html
In order to find color proximity, it's best to transform the colors to Lab color space first, so that euclidean distances have more meaning, and then nearest neighbor would give good results.
You could convert from RGB to CIE Lab color space wherein Euclidian distance between two color selections is perceptually more meaningful. Here is the link to all relevant color space transformation formulae used in OpenCV's color conversion method (cvtColor): http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html
Since your use case is to compare two swatches, I would advise you to use texture descriptors (http://www.robots.ox.ac.uk/~vgg/research/texclass/with.html) in addition to color information for better results.

Represent the colour of a bit field

Can anyone show me a (language agnostic) way to assign a colour value to a bit field so that comparatively similar bit fields have a similar colour to each other. So for example
01100111
And
01110111
Are close in colour relatively. But
11011001
Is further away
By "further away" I mean distant in hue, saturation, brightness, etc...
If we have an array of all the bit fields then it would be possible to compare them all then produce a set of colours. But what if we don't know and we want one bit field to always be represented by one colour?
Or else we could pre compute all possible colour values for a given number if bits. How would I go about doing that?
You cannot do this, because (essentially) there are only 2 dimensions to your perceived color space, while treating the bits independently as you suggest makes a separate dimension for each bit.

How to adjust gradient color by RGB or HSB

I'm trying to make a bar with gradient color updownward, I set 3 points as stated in the bar. Now the picture seems good, but I don't know how to automatically generate these color mathematically, by RGB or HSB? I'm having trouble with the rule of this kind of art thing.
I was intending to do it with RGB but I found it hard to do. But with HSB, I changed "S" and it makes a little sense as shown in picture.
My question is: How to calculate these three colors based on ONE given color, makes the gradient natural?
Thanks in advance, this has nothing to do with code but I think it definitely has a mathematical solution(formula).
I think there's no general rule for how to do this and different possibilities to get to a (subjectively pleasing) result.
I copied your colors here for analysis but didn't find a pattern in your choice. My solution would be to find a pleasing distance of (relative) luminance. To adabt to your example I chose one arbitrary color, then increased the Lum value by 18% for the second color and for the third one I subtracted 10% Lum again.
Do you like this solution?

Resources