🌐
Good Calculators
goodcalculators.com › rgb-to-grayscale-conversion-calculator
RGB to Grayscale Conversion Calculator | Good Calculators
You can use this RGB to Grayscale conversion calculator to transform RGB values to their corresponding grayscale values
🌐
RapidTables
rapidtables.com › convert › image › rgb-to-grayscale.html
Color image to black and white converter
Home›Conversion›Image conversion› RGB to Grayscale photo · RGB to grayscale image conversion online: Gray RGB color code has equal red,green and blue values: R = G = B · For each image pixel with red, green and blue values of (R,G,B): R' = G' = B' = (R+G+B) / 3 = 0.333R + 0.333G + 0.333B ·
🌐
MathWorks
mathworks.com › matlab › graphics › images
rgb2gray - Convert RGB image or colormap to grayscale - MATLAB
I = rgb2gray(RGB) converts the truecolor image RGB to the grayscale image I. The rgb2gray function converts RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance.
🌐
Mustafa Murat ARAT
mmuratarat.github.io › 2020-05-13 › rgb_to_grayscale_formulas
RGB to Grayscale Conversion | Mustafa Murat ARAT
May 13, 2020 - 105 105 106]] plt.imshow(img_gray, cmap='gray') plt.savefig('image_weighted_average_OPENCV.png') Average method is the most simple one. You just have to take the average of three colors. Since its an RGB image, so it means that you have add R with G with B and then divide it by 3 to get your ...
🌐
Brandonrohrer
brandonrohrer.com › convert_rgb_to_grayscale.html
How to Convert an RGB Image to Grayscale
import lodgepole.image_tools as lit gray_img = lit.rgb2gray(color_img) If after reading this far you insist on straight up averaging the three channels together, I will judge you. Now go make beautiful grayscale images!
🌐
Dynamsoft
dynamsoft.com › blog › insights › image-processing › image-processing-101-color-space-conversion
Color Space Conversion & Binarization for Image Processing | Dynamsoft Blog
May 24, 2019 - The barcode scanner first converts the color image to grayscale and then binary. Using the average method, we get a grayscale image like this one: To further split the foreground (the DataMatrix code) from the white background with the red lines, let’s give a larger weight to the red channel. ... Here is the result. The red lines are lighter compared with the result from the average method, i.e., closer to white. ... import cv2 RGB_image = cv2.imread("thai-government-lottery.png") blue = RGB_image[:,:,0] green = RGB_image[:,:,1] red = RGB_image[:,:,2] Average_Gray = blue/3+green/3+red/3 Weighted_Gray = (0 * blue) + (0* green) + (1 * red)
🌐
Grayscale Images
grayscaleimage.com › home
Grayscale Image - Convert Photo to Grayscale for Free
March 27, 2023 - Grayscale Image is a online free tool to Convert Images into Greyscalefor free. We provide a fast & quick processing free toolkit to Convert JPGE, PNG, WEBP, RGB Image to Black and White or Greyscale. Enjoy the beauty in few clicks.
🌐
scikit-image
scikit-image.org › docs › stable › auto_examples › color_exposure › plot_rgb_to_gray.html
RGB to grayscale — skimage 0.26.0 documentation
import matplotlib.pyplot as plt from skimage import data from skimage.color import rgb2gray original = data.astronaut() grayscale = rgb2gray(original) fig, axes = plt.subplots(1, 2, figsize=(8, 4)) ax = axes.ravel() ax[0].imshow(original) ax[0].set_title("Original") ax[1].imshow(grayscale, cmap=plt.cm.gray) ax[1].set_title("Grayscale") fig.tight_layout() plt.show() Total running time of the script: (0 minutes 0.624 seconds)
Find elsewhere
🌐
Online JPG Tools
onlinejpgtools.com › convert-jpg-to-grayscale
Convert a JPG to Grayscale – Online JPG Tools
Incredibly simple, free, and fast browser-based utility for converting JPEG pictures to grayscale. Just paste your JPEG here and it'll be instantly grayscaled.
Top answer
1 of 8
103

The specific numbers in the question are from CCIR 601 (see Wikipedia article).

If you convert RGB -> grayscale with slightly different numbers / different methods, you won't see much difference at all on a normal computer screen under normal lighting conditions -- try it.

Here are some more links on color in general:

Wikipedia Luma

Bruce Lindbloom 's outstanding web site

chapter 4 on Color in the book by Colin Ware, "Information Visualization", isbn 1-55860-819-2; this long link to Ware in books.google.com may or may not work

cambridgeincolor : excellent, well-written "tutorials on how to acquire, interpret and process digital photographs using a visually-oriented approach that emphasizes concept over procedure"

Should you run into "linear" vs "nonlinear" RGB, here's part of an old note to myself on this. Repeat, in practice you won't see much difference.


### RGB -> ^gamma -> Y -> L*

In color science, the common RGB values, as in html rgb( 10%, 20%, 30% ), are called "nonlinear" or Gamma corrected. "Linear" values are defined as

Rlin = R^gamma,  Glin = G^gamma,  Blin = B^gamma

where gamma is 2.2 for many PCs. The usual R G B are sometimes written as R' G' B' (R' = Rlin ^ (1/gamma)) (purists tongue-click) but here I'll drop the '.

Brightness on a CRT display is proportional to RGBlin = RGB ^ gamma, so 50% gray on a CRT is quite dark: .5 ^ 2.2 = 22% of maximum brightness. (LCD displays are more complex; furthermore, some graphics cards compensate for gamma.)

To get the measure of lightness called L* from RGB, first divide R G B by 255, and compute

Y = .2126 * R^gamma + .7152 * G^gamma + .0722 * B^gamma

This is Y in XYZ color space; it is a measure of color "luminance". (The real formulas are not exactly x^gamma, but close; stick with x^gamma for a first pass.)

Finally,

L* = 116 * Y ^ 1/3 - 16

"... aspires to perceptual uniformity [and] closely matches human perception of lightness." -- Wikipedia Lab color space

2 of 8
17

I found this publication referenced in an answer to a previous similar question. It is very helpful, and the page has several sample images:

Perceptual Evaluation of Color-to-Grayscale Image Conversions by Martin Čadík, Computer Graphics Forum, Vol 27, 2008

The publication explores several other methods to generate grayscale images with different outcomes:

  • CIE Y
  • Color2Gray
  • Decolorize
  • Smith08
  • Rasche05
  • Bala04
  • Neumann07

Interestingly, it concludes that there is no universally best conversion method, as each performed better or worse than others depending on input.

🌐
Had2know
had2know.org › technology › rgb-to-gray-scale-converter.html
RGB to Grayscale Conversion Calculator
Convert rgb color values to grayscale values, grayscale conversion calculator, adapting colored designs to black and white display
Top answer
1 of 6
38

The problem is that I can't understand how the "official" JPG->PGM convertitors work in terms of what value to assign to the final pixel (i guess, 0->255) starting from the classic RGB format.

There is likely a gamma adjustment in the conversion those "official" tools are using.
That is, it is not just a linear transform.

See this Wikipedia section for the details: Converting color to grayscale

I believe you want to use the formula for Csrgb.
Try it out and see if it matches the results you're expecting.

Basically, you'll do this:

  1. Take R, G, B color (each in [0,1] range)
    • If they're in the range 0..255 instead, simply divide by 255.0
  2. Compute Clinear = 0.2126 R + 0.7152 G + 0.0722 B
    • This is likely the linear transform you were applying before
  3. Compute Csrgb according to it's formula, based on Clinear
    • This is the nonlinear gamma correction piece you were missing
    • Check out this WolframAlpha plot
    • Csrgb = 12.92 Clinear when Clinear <= 0.0031308
    • Csrgb = 1.055 Clinear1/2.4 - 0.055 when Clinear > 0.0031308
2 of 6
4

To harold's point about the "Y plane": standard color JPEGs are encoded using the YCbCr colorspace, where Y is the luminance component (i.e. the brightness) and Cb and Cr are the blue-difference and red-difference chroma components. So one way of turning a color JPEG into a grayscale one is to simply drop the Cb and Cr components.

There is a utility called jpegtran than can do this losslessly, using the -grayscale option. (The lossless part would really only matter if you wanted to end up with a JPEG and not PGM, to avoid generation loss.) In any case, this would probably be the fastest way to do this transformation, because it doesn't even decode the image into pixels, much less do math on each one.

🌐
scikit-image
scikit-image.org › docs › 0.25.x › auto_examples › color_exposure › plot_rgb_to_gray.html
RGB to grayscale — skimage 0.25.2 documentation
import matplotlib.pyplot as plt from skimage import data from skimage.color import rgb2gray original = data.astronaut() grayscale = rgb2gray(original) fig, axes = plt.subplots(1, 2, figsize=(8, 4)) ax = axes.ravel() ax[0].imshow(original) ax[0].set_title("Original") ax[1].imshow(grayscale, cmap=plt.cm.gray) ax[1].set_title("Grayscale") fig.tight_layout() plt.show() Total running time of the script: (0 minutes 0.624 seconds)
🌐
TutorialsPoint
tutorialspoint.com › dip › grayscale_to_rgb_conversion.htm
Grayscale to RGB Conversion
New grayscale image = ( (0.3 * R) + (0.59 * G) + (0.11 * B) ). According to this equation, Red has contribute 30%, Green has contributed 59% which is greater in all three colors and Blue has contributed 11%. ... As you can see here, that the ...
🌐
KDnuggets
kdnuggets.com › 2019 › 12 › convert-rgb-image-grayscale.html
How to Convert an RGB Image to Grayscale - KDnuggets
December 18, 2019 - import lodgepole.image_tools as lit gray_img = lit.rgb2gray(color_img) If after reading this far you insist on straight up averaging the three channels together, I will judge you. Now go make beautiful grayscale images! Original. Reposted with permission. Brandon Rohrer is a Staff Machine Learning Engineer at LinkedIn. Brandon's specialty is creating algorithms and computational methods. You can find examples of his work here. How to Use ChatGPT to Convert Text into a PowerPoint Presentation
🌐
MathWorks
mathworks.com › matlab › graphics › images
im2gray - Convert RGB image to grayscale - MATLAB
I = im2gray(RGB) converts the specified truecolor image RGB to a grayscale intensity image I.