Question

How can I convert a colorful(RGB) jpeg image to gray scale?

The code I am working with only accepts the gray scale images but I want to feed colorful images as well.

rawimg=readJPEG("winter.jpeg")
rawimg=t(rawimg)
rawimg=rawimg[,ncol(rawimg):1]
image(rawimg,col = grey((0:12)/12))
Was it helpful?

Solution

I am not sure if any R-specific functionality exists to convert a JPEG image into grayscale. However, writing this functionality yourself would not be difficult.

Assuming that the JPEG image uses the sRGB primaries (it almost certainly does, so don't worry too much about this), the most common conversion function to grayscale is given by the ITU-R reccomendation BT.709 Luma function:

Y' = 0.2126 R' + 0.7152 G' + 0.0722 B'

You can use traditional R techniques to apply this to each spatial location in your image.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top