-5

I would like to transform my RGB image to grayscale image by not using converting function but with the red green blue values . For example, if my image is totally blue, it will be converted to white if I get blue components of it and it will be black if I get red components of my RGB image. It will be done in Python via OpenCV.

Thanks in advance.

Miki
  • 37,220
  • 12
  • 98
  • 183
HilmiK
  • 53
  • 9
  • 2
    Great! Let us know how it goes. Start here: http://stackoverflow.com/help/how-to-ask – Jeff Loughlin Mar 13 '17 at 19:11
  • I could not understand what the problem is. Can you clearly read my question and explain please ? – HilmiK Mar 13 '17 at 19:17
  • 1
    In pretty much all languages and libraries the image is imported as an array, so you can just slice colours out for grayscale. For example an image might be a 300x500x3 array (300 pixels vertically, 500 pixels horizontally, and 3 colour channels). So you can get a colour channel by slicing it out e.g. `red_channel = image[:, :, 0]` or `blue_channel = image[:,:,2]`. – Ari Cooper-Davis Mar 13 '17 at 19:26

1 Answers1

0

The converting function that you are referring to does the same - it weights the R,G and B channel values of each pixel, and takes the sum. Since OpenCV uses the BGR colorspace on reading images, your conversion function will be something like this-

def rgbToGray(img):
    grayImg = 0.0722*img(:,:,1) + 0.7152*img(:,:,2) + 0.2126*img(:,:,3)
    return grayImg

The specific weights mentioned here are taken from the ITU-R BT.709 standard used for HDTV, developed by the ATSC (https://en.wikipedia.org/wiki/Grayscale)

Nisu
  • 26