<h5 dir="auto">System information (version)</h5>

<ul dir="auto">
<li>OpenCV =&gt; 4.1.1</li>
<li>Operating System / Platform =&gt; Windows 64 Bit / C++ &amp; Python</li>
<li>Compiler =&gt; Visual Studio 2017, Python 3.7.6</li>
</ul>
<h5 dir="auto">Detailed description</h5>
<p dir="auto">I just updated OpenCV 4.2.0 and found a big problem.<br>
If I open the lenna BMP image with IMREAD_GRAYSCALE flag, the image is little bit dark. (the mean value was 110)<br>
I felt something weird because I knew the mean value of lenna gray image is 124.<br>
So I tried to open the lenna image as a color image and convert it to a grayscale image using cvtColor(), then the mean value was 124.<br>
So there must be something wrong on the color conversion code in imread().</p>
<h5 dir="auto">Steps to reproduce</h5>
<p dir="auto">Mat src1 = imread("lenna.bmp", IMREAD_GRAYSCALE);<br>
Mat src2 = imread("lenna.bmp", IMREAD_COLOR);<br>
cvtColor(src2, src2, COLOR_BGR2GRAY);</p>
<p dir="auto">cout &lt;&lt; mean(src1)[0] &lt;&lt; endl;  // 110.207<br>
cout &lt;&lt; mean(src2)[0] &lt;&lt; endl;  // 124.049</p>
<h5 dir="auto">My opinion</h5>
<p dir="auto">I checked the recent versions, and found that OpenCV 4.1.0 was ok.<br>
So I searched commits and found it:<br>
<a class="commit-link" href="https://github.com/opencv/opencv/commit/058367f34c60d6a843579adce84895f4bc5d31dd#diff-c868f69925dd124c54190546d4843841"><tt>058367f</tt>#diff-c868f69925dd124c54190546d4843841</a></p>
<p dir="auto">In the updated code, I think 68th line should be changed like this:</p>
<p dir="auto">(-) if (_swap_rb) std::swap(cRGB0, cRGB2);<br>
(+) if (!_swap_rb) std::swap(cRGB0, cRGB2);</p>
<p dir="auto">Please check the code.</p>
<p dir="auto"><a class="commit-link" href="https://github.com/opencv/opencv/commit/058367f34c60d6a843579adce84895f4bc5d31dd#diff-c868f69925dd124c54190546d4843841"><tt>058367f</tt>#diff-c868f69925dd124c54190546d4843841</a> break BGR color conversion utilities in opencv/modules/imgcodecs/src/utils.cpp.<br>
Before this commit when swap channels is set to 0 we have a following formula:<br>
rgba[0]*cB + rgba[1]*cG + rgba[2]*cR</p>
<p dir="auto">As <code class="notranslate">rgba</code>, in this case, is really BGRA data it works fine.</p>
<p dir="auto">After this change:<br>
rgba[0]*cRGB0 + rgba[1]*cG + rgba[2]*cRGB2<br>
where<br>
cRGB0 = cR<br>
cRGB0 = cB</p>
<p dir="auto">It is used by ../modules/imgcodecs/src/grfmt_tiff.cpp which reads RBG data, but uses icvCvt_BGRA2Gray_8u_C4C1R for conversion but with channel swapped.</p>
<p dir="auto">relates <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="453605866" data-permission-text="Title is private" data-url="https://github.com/opencv/opencv/issues/14760" data-hovercard-type="pull_request" data-hovercard-url="/opencv/opencv/pull/14760/hovercard" href="https://github.com/opencv/opencv/pull/14760">#14760</a></p>