<p>How do I convert the RGB values of a pixel to a single monochrome value?</p>

[{'answer_id': 14331, 'author': 'Ashwin Nanjappa', 'author_id': 1630, 'author_profile': 'https://Stackoverflow.com/users/1630', 'pm_score': 6, 'selected': True, 'text': '<p>I found one possible solution in the <a href="http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html" rel="noreferrer">Color FAQ</a>. The <em>luminance component</em> Y (from the <em>CIE XYZ system</em>) captures what is most perceived by humans as color in one channel. So, use those coefficients:</p>\n\n<pre><code>mono = (0.2125 * color.r) + (0.7154 * color.g) + (0.0721 * color.b);\n</code></pre>\n'}, {'answer_id': 14339, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://msdn.microsoft.com/en-us/library/bb332387.aspx#tbconimagecolorizer_grayscaleconversion" rel="nofollow noreferrer">This MSDN article</a> uses <code>(0.299 * color.R + 0.587 * color.G + 0.114 * color.B);</code></p>\n\n<p><a href="http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale" rel="nofollow noreferrer">This Wikipedia article</a> uses <code>(0.3* color.R + 0.59 * color.G + 0.11 * color.B);</code></p>\n'}, {'answer_id': 15119, 'author': 'Carl Russmann', 'author_id': 1347, 'author_profile': 'https://Stackoverflow.com/users/1347', 'pm_score': 3, 'selected': False, 'text': '<p>This depends on what your motivations are.  If you just want to turn an arbitrary image to grayscale and have it look pretty good, the conversions in other answers to this question will do.</p>\n\n<p>If you are converting color photographs to black and white, the process can be both very complicated and subjective, requiring specific tweaking for each image.  For an idea what might be involved, take a look at this <a href="http://www.adobe.com/designcenter/photoshop/articles/phscs2mrblkwht.html" rel="noreferrer">tutorial</a> from Adobe for Photoshop.</p>\n\n<p>Replicating this in code would be fairly involved, and would still require user intervention to get the resulting image aesthetically "perfect" (whatever that means!).</p>\n'}, {'answer_id': 25772, 'author': 'Henrik Paul', 'author_id': 2238, 'author_profile': 'https://Stackoverflow.com/users/2238', 'pm_score': 3, 'selected': False, 'text': "<p>As mentioned also, a grayscale translation (note that monochromatic images need not to be in grayscale) from an RGB-triplet is subject to taste. </p>\n\n<p>For example, you could cheat, extract only the blue component, by simply throwing the red and green components away, and copying the blue value in their stead. Another simple and generally ok solution would be to take the average of the pixel's RGB-triplet and use that value in all three components.</p>\n\n<p>The fact that there's a considerable market for professional and not-very-cheap-at-all-no-sirree grayscale/monochrome converter plugins for Photoshop alone, tells that the conversion is just as simple or complex as you wish.</p>\n"}, {'answer_id': 51262134, 'author': 'Sau001', 'author_id': 2989655, 'author_profile': 'https://Stackoverflow.com/users/2989655', 'pm_score': 1, 'selected': False, 'text': '<p>The logic behind converting any RGB based picture to monochrome can is not a trivial linear transformation.  In my opinion such a problem is better addressed by "Color Segmentation" techniques. You could achieve "Color segmentation" by k-means clustering.</p>\n\n<p>See reference example from MathWorks site.</p>\n\n<p><a href="https://www.mathworks.com/examples/image/mw/images-ex71219044-color-based-segmentation-using-k-means-clustering" rel="nofollow noreferrer">https://www.mathworks.com/examples/image/mw/images-ex71219044-color-based-segmentation-using-k-means-clustering</a></p>\n\n<p><strong>Original picture in colours.</strong></p>\n\n<p><a href="https://i.stack.imgur.com/EcHkr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EcHkr.png" alt="Picture with colours"></a></p>\n\n<p><strong>After converting to monochrome using k-means clustering</strong>\n<a href="https://i.stack.imgur.com/VaLea.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VaLea.png" alt="Picture after converting to monochrome using k-means clustering"></a></p>\n\n<p><strong>How does this work?</strong></p>\n\n<p>Collect all pixel values from entire image. From an image which is W pixels wide and H pixels high, you will get W *H color values. Now, using k-means algorithm create 2 clusters (or bins) and throw the colours into the appropriate "bins". The 2 clusters represent your black and white shades. </p>\n\n<p><strong>Youtube video demonstrating image segmentation using k-means?</strong>\n<a href="https://www.youtube.com/watch?v=yR7k19YBqiw" rel="nofollow noreferrer">https://www.youtube.com/watch?v=yR7k19YBqiw</a></p>\n\n<p><strong>Challenges with this method</strong></p>\n\n<p>The k-means clustering algorithm is susceptible to outliers. A few random pixels with a color whose RGB distance is far away from the rest of the crowd could easily skew the centroids to produce unexpected results.</p>\n'}, {'answer_id': 69980882, 'author': 'Myndex', 'author_id': 10315269, 'author_profile': 'https://Stackoverflow.com/users/10315269', 'pm_score': 1, 'selected': False, 'text': '<p>Just to point out in the self-selected answer, you have to <em>LINEARIZE</em> the sRGB values before you can apply the coefficients. This means removing the transfer curve.</p>\n<p>To remove the power curve, divide the 8 bit R G and B channels by 255.0, then either use the <a href="https://www.myndex.com/WEB/LuminanceContrast" rel="nofollow noreferrer">sRGB piecewise transform</a>, which is recommended for image procesing, OR you can cheat and raise each channel to the power of 2.2.</p>\n<p>Only after linearizing can you apply the coefficients shown, (which also are not exactly correct in the selected answer).</p>\n<p>The standard is <strong>0.2126 0.7152</strong> and <strong>0.0722</strong>. Multiply each channel by its coefficient and sum them together for Y, the luminance. Then re-apply the gamma to Y and multiply by 255, then copy to all three channels, and boom you have a greyscale (monochrome) image.</p>\n<p>Here it is all at once in one simple line:</p>\n<pre class="lang-JS prettyprint-override"><code>// Andy\'s Easy Greyscale in one line.\n// Send it sR sG sB channels as 8 bit ints, and\n// it returns three channels sRgrey sGgrey sBgrey\n// as 8 bit ints that display glorious grey.\n\n\n  sRgrey = sGgrey = sBgrey = Math.min(Math.pow((Math.pow(sR/255.0,2.2)*0.2126+Math.pow(sG/255.0,2.2)*0.7152+Math.pow(sB/255.0,2.2)*0.0722),0.454545)*255),255);\n</code></pre>\n<p>And that\'s it. Unless you have to parse hex strings....</p>\n'}]
['https://Stackoverflow.com/questions/14330', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1630/']