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

Normalize math precision in RGB/YIQ conversion #58531

Closed
packetslave mannequin opened this issue Mar 15, 2012 · 15 comments
Closed

Normalize math precision in RGB/YIQ conversion #58531

packetslave mannequin opened this issue Mar 15, 2012 · 15 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@packetslave
Copy link
Mannequin

packetslave mannequin commented Mar 15, 2012

BPO 14323
Nosy @terryjreedy, @mdickinson, @ezio-melotti, @serhiy-storchaka
Files
  • colorlib.patch: Old
  • acks.patch
  • colorlib.patch
  • colorsys_yiq_fcc.patch
  • colorsys_yiq_fcc_2.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2013-08-06.08:53:04.530>
    created_at = <Date 2012-03-15.17:19:36.927>
    labels = ['type-feature', 'library']
    title = 'Normalize math precision in RGB/YIQ conversion'
    updated_at = <Date 2013-08-06.08:53:04.529>
    user = 'https://bugs.python.org/packetslave'

    bugs.python.org fields:

    activity = <Date 2013-08-06.08:53:04.529>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2013-08-06.08:53:04.530>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2012-03-15.17:19:36.927>
    creator = 'packetslave'
    dependencies = []
    files = ['24866', '24867', '24895', '31161', '31163']
    hgrepos = []
    issue_num = 14323
    keywords = ['patch']
    message_count = 15.0
    messages = ['155915', '156090', '156098', '156131', '156132', '156133', '158893', '179323', '194433', '194445', '194460', '194481', '194504', '194507', '194531']
    nosy_count = 6.0
    nosy_names = ['terry.reedy', 'mark.dickinson', 'ezio.melotti', 'python-dev', 'serhiy.storchaka', 'packetslave']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue14323'
    versions = ['Python 3.4']

    @packetslave
    Copy link
    Mannequin Author

    packetslave mannequin commented Mar 15, 2012

    There doesn't seem to be a standard definition for the constants to use when doing the matrix calculations to convert RGB to YIQ or vise versa.

    Also, the current colorsys library uses two digits of precision for RGB-YIQ but six digits for YIQ-RGB.

    The attached patch standardizes both functions to use the same constants as Matlab, using the same 3 digits of precision. This makes a roundtrip of RGB->YIQ->RGB return the original values (for 3 digits of precision).

    Also added tests, which brings colorsys.py to 100% coverage.

    @packetslave packetslave mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Mar 15, 2012
    @terryjreedy
    Copy link
    Member

    The idea seems reasonable. Do you have a link or reference to a Matlab doc with the coefficients? Enhancements only go in new versions.

    @packetslave
    Copy link
    Mannequin Author

    packetslave mannequin commented Mar 16, 2012

    Matlab docs are here:

    Should these be referenced in the source itself?

    re new versions: sure, I'll create a separate patch for adding the tests to the maintenance releases so there's coverage there, too. Should that have a new bug as well?

    @terryjreedy
    Copy link
    Member

    Yes, put reference in source. I checked that you copied correctly.
    I think there is some controversy, which I do not understand, about adding tests to maintenance releases without a bug fix. I will ask, so do not do it yet.

    @terryjreedy
    Copy link
    Member

    Mark, I know you have worked on numerical algorithms.

    1. Do you agree that this is a reasonable change?
    2. Should new tests go in maintenance release?

    @packetslave
    Copy link
    Mannequin Author

    packetslave mannequin commented Mar 17, 2012

    Updated to add Matlab refs, also added a roundtrip RGB-YIQ-RGB test to match the other conversions.

    @mdickinson
    Copy link
    Member

    Terry: sorry, I missed this before.

    Re 1. Sure, this seems reasonable, if there's a real sense in which the new numbers are better than the old. Besides MATLAB, there's also a set of numbers given on Wikipedia that might be considered. I don't have the domain knowledge to know what's sensible here.

    I *would* rather see the inverse transformation keep the full 6 digits of precision, though. Or perhaps even give the inverse to full float precision. Without that, the result of roundtripping RGB -> YIQ -> RGB could be significantly (perhaps even visibly) different from the original. I don't think it's acceptable for the roundtrip error to increase significantly w.r.t. Python 3.2.

    Re 2. For this issue, I don't see a real benefit to adding the tests to the maintenance releases. No strong opinions, though.

    @serhiy-storchaka
    Copy link
    Member

    According to Wikipedia FCC conversion is defined as:

        y = 0.30*r + 0.59*g + 0.11*b
        i = 0.5990*r - 0.2773*g - 0.3217*b
        q = 0.2130*r - 0.5251*g + 0.3121*b

    and non-FCC conversion is defined as:

        y = 0.299*r + 0.587*g + 0.114*b
        i = 0.595716*r - 0.274453*g - 0.321263*b
        q = 0.211456*r - 0.522591*g + 0.311135*b

    Our current code

        y = 0.30*r + 0.59*g + 0.11*b
        i = 0.60*r - 0.28*g - 0.32*b
        q = 0.21*r - 0.52*g + 0.31*b

    looks like FCC conversion with the precision of two decimal places. Actually with this precision the difference between the different conversions are almost absent.

    @serhiy-storchaka
    Copy link
    Member

    Here is a patch which implements the FCC version of RGB/YIQ conversion.

    @terryjreedy
    Copy link
    Member

    Can you add a reference for the coefficients?

    I believe

    def test_main():
     test.support.run_unittest(ColorsysTest)
    
    if __name__ == "__main__":
     test_main

    has been and is being replaced in other test files with

    if __name__ == "__main__":
     unittest.main

    and should be here.

    This should get a short What's New entry in the library section, something like

    colorsys:
    "The number of digits in the coefficients for the RGB -- YIQ conversions have been expanded so that they match the FCC NTSC versions. The change in results should be less than 1% and may better match results found elsewhere."

    (You claim about the current rounding is not exactly correct. While .28*g rounds .277 rather than .274, the current .52*g rounds the non-FCC .523 rather than the FCC .5251. So I avoided making the claim in the suggested entry. It is not important.)

    @serhiy-storchaka
    Copy link
    Member

    Can you add a reference for the coefficients?

    I have only link to Wikipedia which refers to Code of Federal Regulations §73.682. This link (http://en.wikipedia.org/wiki/YIQ) already mentioned at the top of the file.

    (You claim about the current rounding is not exactly correct. While .28*g rounds .277 rather than .274, the current .52*g rounds the non-FCC .523 rather than the FCC .5251. So I avoided making the claim in the suggested entry. It is not important.)

    A sum of coefficients in this line should be 0 (Q=0 for R=G=B).

    Patch updated. I added a What's New entry and update to use of unittest.main(), rewrite rgb_to_yiq() in the form as in Wikipedia (it uses less multiplications) and write coefficients in yiq_to_rgb() with maximal precision (as calculated with Python).

    @ezio-melotti
    Copy link
    Member

    LGTM.

    @terryjreedy
    Copy link
    Member

    I agree. Go ahead and push.

    @mdickinson
    Copy link
    Member

    LGTM too.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Aug 6, 2013

    New changeset 80e9cb6163b4 by Serhiy Storchaka in branch 'default':
    Issue bpo-14323: Expanded the number of digits in the coefficients for the
    http://hg.python.org/cpython/rev/80e9cb6163b4

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants