Skip to content

YUV implemented incorrectly #4946

@opencv-pushbot

Description

@opencv-pushbot

Transferred from http://code.opencv.org/issues/4227

|| William Mantzel on 2015-03-05 00:56
|| Priority: Normal
|| Affected: branch '2.4' (2.4-dev)
|| Category: imgproc, video
|| Tracker: Bug
|| Difficulty: Medium
|| PR: 
|| Platform: x64 / Linux

YUV implemented incorrectly

The 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. 

At present, what is implemented is:
Y = .114R + .587G + .299R
U = .492(R-Y)
V = .877(B-Y)

When it should be:
Y = .299R + .587G + .114B
U = .492(B-Y)
V = .877(R-Y)

The inverse transform mirrors this bug, which is why it has stayed hidden for a while.

History

Steven Puttemans on 2015-03-05 08:12
Wikipedia (http://nl.wikipedia.org/wiki/YUV) says

<pre>
Y  = + 0,299R + 0,587G + 0,114B
U  = 0,492(B - Y)
   = - 0,147R - 0,289G + 0,436B
V  = 0,877(R - Y)
   = + 0,615R - 0,515G - 0,100B
</pre>

So indeed some stuff got swapped. Will supply a PR in a moment!
-   Assignee changed from William Mantzel to Steven Puttemans
-   Status changed from New to Open
Steven Puttemans on 2015-03-05 09:45
Fixing this seems more difficult than expected -_- I cannot find my way around the tegra optimizations.
Could you supply the fix maybe?
-   Assignee changed from Steven Puttemans to William Mantzel
Max Khardin on 2015-03-13 08:30
Steven Puttemans wrote:
> Fixing this seems more difficult than expected -_- I cannot find my way around the tegra optimizations.
> Could you supply the fix maybe?

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. 

Or am I missing something?
Ilya Lavrenov on 2015-03-16 10:19
I don't understand where have you found a mistaken implementation?
https://github.com/Itseez/opencv/blob/2.4/modules/imgproc/src/color.cpp#L786 uses the same as in WIKI.
Steven Puttemans on 2015-03-16 13:46
Max Khardin wrote:
> Steven Puttemans wrote:
> > Fixing this seems more difficult than expected -_- I cannot find my way around the tegra optimizations.
> > Could you supply the fix maybe?
> 
> 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. 
> 
> Or am I missing something?

For example that was not clear to me at all :D
Max Khardin on 2015-03-19 18:05
Ilya Lavrenov wrote:
> I don't understand where have you found a mistaken implementation?
> https://github.com/Itseez/opencv/blob/2.4/modules/imgproc/src/color.cpp#L786 uses the same as in WIKI.

I think ticket is about this line: https://github.com/Itseez/opencv/blob/2.4/modules/imgproc/src/color.cpp#L3835
Nisarg Thakkar on 2015-04-03 03:37
I think this has been implemented correctly. OpenCV uses YCrCb whereas Matlab uses YCbCr. I guess this might be causing the confusion.
Artem Sanakoev on 2015-04-27 08:06
Possible workaround while bug is not fixed to convert from BGR to YUV:

<pre>
  cv::Mat source = cv::imread(filename, CV_LOAD_IMAGE_COLOR);
  cv::Mat yuvSource;    
  cvtColor(source, yuvSource, cv::COLOR_BGR2RGB); // rearranges B and R in appropriate order
  cvtColor(yuvSource, yuvSource, cv::COLOR_BGR2YUV);
</pre>
Vadim Pisarevsky on 2015-04-27 11:38
-   Category set to imgproc, video
Tim van Oosterhout on 2015-06-05 13:22
This issue is still present in version 3.0.0
An alternative workaround is to simply convert from RGB instead of BGR:

<pre>
cvtColor(yuvSource, yuvSource, cv::COLOR_RGB2YUV);
</pre>

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions