{"action":"opened","issue":{"url":"https://api.github.com/repos/mshabunin/test2/issues/1543","labels_url":"https://api.github.com/repos/mshabunin/test2/issues/1543/labels{/name}","comments_url":"https://api.github.com/repos/mshabunin/test2/issues/1543/comments","events_url":"https://api.github.com/repos/mshabunin/test2/issues/1543/events","html_url":"https://github.com/mshabunin/test2/issues/1543","id":96127540,"number":1543,"title":"YUV implemented incorrectly","user":{"login":"opencv-pushbot","id":2051563,"avatar_url":"https://avatars.githubusercontent.com/u/2051563?v=3","gravatar_id":"","url":"https://api.github.com/users/opencv-pushbot","html_url":"https://github.com/opencv-pushbot","followers_url":"https://api.github.com/users/opencv-pushbot/followers","following_url":"https://api.github.com/users/opencv-pushbot/following{/other_user}","gists_url":"https://api.github.com/users/opencv-pushbot/gists{/gist_id}","starred_url":"https://api.github.com/users/opencv-pushbot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/opencv-pushbot/subscriptions","organizations_url":"https://api.github.com/users/opencv-pushbot/orgs","repos_url":"https://api.github.com/users/opencv-pushbot/repos","events_url":"https://api.github.com/users/opencv-pushbot/events{/privacy}","received_events_url":"https://api.github.com/users/opencv-pushbot/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/mshabunin/test2/labels/affected:%202.4","name":"affected: 2.4","color":"ededed"},{"url":"https://api.github.com/repos/mshabunin/test2/labels/auto-transferred","name":"auto-transferred","color":"ededed"},{"url":"https://api.github.com/repos/mshabunin/test2/labels/bug","name":"bug","color":"fc2929"},{"url":"https://api.github.com/repos/mshabunin/test2/labels/category:%20imgproc","name":"category: imgproc","color":"ededed"},{"url":"https://api.github.com/repos/mshabunin/test2/labels/category:%20video","name":"category: video","color":"ededed"},{"url":"https://api.github.com/repos/mshabunin/test2/labels/priority:%20normal","name":"priority: normal","color":"ededed"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-07-20T18:29:38Z","updated_at":"2015-07-20T18:29:38Z","closed_at":null,"body":"Transferred from http://code.opencv.org/issues/4227\r\n```\n|| William Mantzel on 2015-03-05 00:56\n|| Priority: Normal\n|| Affected: branch '2.4' (2.4-dev)\n|| Category: imgproc, video\n|| Tracker: Bug\n|| Difficulty: Medium\n|| PR: \n|| Platform: x64 / Linux\n```\n\nYUV implemented incorrectly\n-----------\n```\nThe BT.601 BGR to YUV conversion is incorrectly implemented. The U channel is proportional to (R-Y) instead of (B-Y) and vice versa for V, and the coefficients for the Y channel are accidentally swapped, likely as a result of trying to account for RGB/BGR variations. \r\n\r\nAt present, what is implemented is:\r\nY = .114R + .587G + .299R\r\nU = .492(R-Y)\r\nV = .877(B-Y)\r\n\r\nWhen it should be:\r\nY = .299R + .587G + .114B\r\nU = .492(B-Y)\r\nV = .877(R-Y)\r\n\r\nThe inverse transform mirrors this bug, which is why it has stayed hidden for a while.\n```\n\nHistory\n-------\n##### Steven Puttemans on 2015-03-05 08:12\n```\nWikipedia (http://nl.wikipedia.org/wiki/YUV) says\r\n\r\n<pre>\r\nY  = + 0,299R + 0,587G + 0,114B\r\nU  = 0,492(B - Y)\r\n   = - 0,147R - 0,289G + 0,436B\r\nV  = 0,877(R - Y)\r\n   = + 0,615R - 0,515G - 0,100B\r\n</pre>\r\n\r\nSo indeed some stuff got swapped. Will supply a PR in a moment!\n-   Assignee changed from William Mantzel to Steven Puttemans\n-   Status changed from New to Open\n```\n##### Steven Puttemans on 2015-03-05 09:45\n```\nFixing this seems more difficult than expected -_- I cannot find my way around the tegra optimizations.\r\nCould you supply the fix maybe?\n-   Assignee changed from Steven Puttemans to William Mantzel\n```\n##### Max Khardin on 2015-03-13 08:30\n```\nSteven Puttemans wrote:\r\n> Fixing this seems more difficult than expected -_- I cannot find my way around the tegra optimizations.\r\n> Could you supply the fix maybe?\r\n\r\nCould you explain more - what are your particular problems with tegra optimizations? As far as I can see, all that we have in cvtColor for TEGRA_OPTIMIZATION and bgr2yuv is a call to tegra::cvtRGB2YCrCb that does not use any info on coefficients supplied by us. \r\n\r\nOr am I missing something?\n```\n##### Ilya Lavrenov on 2015-03-16 10:19\n```\nI don't understand where have you found a mistaken implementation?\r\nhttps://github.com/Itseez/opencv/blob/2.4/modules/imgproc/src/color.cpp#L786 uses the same as in WIKI.\n```\n##### Steven Puttemans on 2015-03-16 13:46\n```\nMax Khardin wrote:\r\n> Steven Puttemans wrote:\r\n> > Fixing this seems more difficult than expected -_- I cannot find my way around the tegra optimizations.\r\n> > Could you supply the fix maybe?\r\n> \r\n> Could you explain more - what are your particular problems with tegra optimizations? As far as I can see, all that we have in cvtColor for TEGRA_OPTIMIZATION and bgr2yuv is a call to tegra::cvtRGB2YCrCb that does not use any info on coefficients supplied by us. \r\n> \r\n> Or am I missing something?\r\n\r\nFor example that was not clear to me at all :D\n```\n##### Max Khardin on 2015-03-19 18:05\n```\nIlya Lavrenov wrote:\r\n> I don't understand where have you found a mistaken implementation?\r\n> https://github.com/Itseez/opencv/blob/2.4/modules/imgproc/src/color.cpp#L786 uses the same as in WIKI.\r\n\r\nI think ticket is about this line: https://github.com/Itseez/opencv/blob/2.4/modules/imgproc/src/color.cpp#L3835\n```\n##### Nisarg Thakkar on 2015-04-03 03:37\n```\nI think this has been implemented correctly. OpenCV uses YCrCb whereas Matlab uses YCbCr. I guess this might be causing the confusion.\n```\n##### Artem Sanakoev on 2015-04-27 08:06\n```\nPossible workaround while bug is not fixed to convert from BGR to YUV:\r\n\r\n<pre>\r\n  cv::Mat source = cv::imread(filename, CV_LOAD_IMAGE_COLOR);\r\n  cv::Mat yuvSource;    \r\n  cvtColor(source, yuvSource, cv::COLOR_BGR2RGB); // rearranges B and R in appropriate order\r\n  cvtColor(yuvSource, yuvSource, cv::COLOR_BGR2YUV);\r\n</pre>\n```\n##### Vadim Pisarevsky on 2015-04-27 11:38\n```\n-   Category set to imgproc, video\n```\n##### Tim van Oosterhout on 2015-06-05 13:22\n```\nThis issue is still present in version 3.0.0\r\nAn alternative workaround is to simply convert from RGB instead of BGR:\r\n\r\n<pre>\r\ncvtColor(yuvSource, yuvSource, cv::COLOR_RGB2YUV);\r\n</pre>\n```"}}