Hamsa
Hamsa

Reputation: 483

Problem in converting a gray scale image back to an rgb image with user defined transformation function in python

I am working with some RGB image, for some functionality I had to transform the RGB image to Grayscale using some transformation value, then after operations I want to again convert the output image to RGB format with the help of transformation values. How to acheive the same?

Opencv conversion cv2.cvtColor(image, cv2.COLOR_GRAY2RGB) will not produce the original image as it will take values of all 3 channels same, that's why I applied a transformation

I read the RGB image as:

img=cv2.imread("image.png")

Then I extract the RGB values as:

r, g,b = img[:,:,0], img[:,:,1],img[:,:,2]

then I applied transformation to get the gray scale image as:

gray = 0.2989 * r + 0.5870 * g + 0.1140 * b

How to convert the gray scale image after processing once again back to RGB with the given transformation values?

Upvotes: 3

Views: 2569

Answers (3)

Mark Setchell
Mark Setchell

Reputation: 207465

I think you are trying to operate on the channels independently because you have some algorithm that expects a greyscale image. You can tackle that in a couple of different ways.

So, let's start with this image:

enter image description here

You can either do it "in-place" without separating the image into its component channels like this:

#!/usr/bin/env python3

import cv2

# Load image
img=cv2.imread("start.png",cv2.IMREAD_COLOR)

# In-place, zero the Blue, double the Green and halve the Red channels
img[:,:,0]   = 0
img[:,:,1]  *= 2
img[:,:,2] //= 2

# Save
cv2.imwrite('result1.png', img)

Output

enter image description here

Or you can split the image into its component channels, work on them separately (and potentially in parallel) and then recombine them at the end back into a BGR image:

# Load image and split into component channels
img=cv2.imread("start.png",cv2.IMREAD_COLOR)
B, G, R = cv2.split(img)

# Operate on channels independently and out of place
B[...]   = 0
G[...]  *= 2
R[...] //= 2
# Blur the Red too for extra fun
R = cv2.GaussianBlur(R,(25,25),0)

# Recombine channels and save
result = cv2.merge((B,G,R))
cv2.imwrite('result2.png', result)

enter image description here

Upvotes: 3

Masoud
Masoud

Reputation: 1280

Its mathematically impossible to transform an grayscale image back to RGB. But, you can convert RGB to HSV spaces using cv2.cvtColor(image, cv2.COLOR_BGR2HSV), perform processing on value as grayscale, and transform the results back to RGB.

Upvotes: 2

bertilnilsson
bertilnilsson

Reputation: 304

When you're adding the red, green and blue channels together into one gray channel you're throwing away information and there is no way (mathematically) you could infer how to split a single gray value into multiple colours again.

Depending on what transformation you're applying to the image in the grayscale format, maybe there will be away to composite your original colour image and the transformed grayscale image into another image that meets your requirements.

Upvotes: 1

Related Questions