Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for Y'CbCr matrices generation. #486

Closed
KelSolaar opened this issue Jul 2, 2019 · 5 comments
Closed

Implement support for Y'CbCr matrices generation. #486

KelSolaar opened this issue Jul 2, 2019 · 5 comments

Comments

@KelSolaar
Copy link
Member

@nick-shaw and I talked about implementing a new colour.RGB_to_YCbCr_matrix definition that would generate the matrices used by the various Y'CbCr flavours, it would eventually be used by the current colour.RGB_to_YCbCr and colour.YCbCr_to_RGB definitions.

It would be beneficial for generating the matrices for usage in other applications and also probably faster.

@KelSolaar
Copy link
Member Author

Adding some related code for future reference, it misses the offset though:

def compute_YCbCr_matrix(weights, is_legal=False, bit_depth=8):
    Kr, Kb = weights
    Y_min, Y_max, C_min, C_max = colour.models.rgb.ycbcr.YCbCr_ranges(bit_depth, is_legal, False)
    Y = np.array([Kr, (1 - Kr - Kb), Kb])
    Cb = 0.5 * (np.array([0, 0, 1]) - Y) / (1 - Kb)
    Cr = 0.5 * (np.array([1, 0, 0]) - Y) / (1 - Kr)
    Y *= Y_max - Y_min
    Cb *= C_max - C_min
    Cr *= C_max - C_min
    return np.linalg.inv(np.vstack([Y, Cb, Cr]))

@nick-shaw
Copy link
Contributor

nick-shaw commented Aug 20, 2019

Should that code not also include the is_int parameter?

Would it be useful also to create a corresponding compute_YCbCr_offsets() function, even if that was relatively trivial? It would presumably be something like:

def compute_YCbCr_offsets(weights, is_legal=False, bit_depth=8, is_int=False):
    Y_min, Y_max, C_min, C_max = colour.models.rgb.ycbcr.YCbCr_ranges(bit_depth, is_legal, is_int)
    Y_offset = Y_min
    C_offset = (C_min + C_max) / 2
    return np.array([Y_min, C_offset, C_offset])

Then you could calculate the matrix and offsets and do:

 YCbCr = colour.utilities.dot_vector(matrix, RGB) + offsets

@KelSolaar
Copy link
Member Author

Absolutely! It is not complete at all, I just pasted it so that I don't have to scroll for 10 hours in Slack :)

@remia
Copy link

remia commented Oct 3, 2019

 YCbCr = colour.utilities.dot_vector(matrix, RGB) + offsets

Maybe propose an option to have the offset right into a 3x4 or 4x4 matrix as well ?

@KelSolaar
Copy link
Member Author

This raises the question as to where the offsets would go in the 4x4 matrix.

@KelSolaar KelSolaar modified the milestones: v0.3.14, v0.3.15 Oct 26, 2019
@KelSolaar KelSolaar modified the milestones: v0.3.15, v0.3.16 Jan 25, 2020
@KelSolaar KelSolaar modified the milestones: v0.3.16, v0.4.0 Nov 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants