Home > Enterprise >  How to turn grayscale image into RGB?
How to turn grayscale image into RGB?

Time:09-17

I am making a application in python that allows people to share their screens, but in order to get a decent frame rate I wanted to compress the image into a grayscale format and then on the client side turn it back into an RGB image. But when I tried to do that it still showed a grayscale image.

Then I tried using HSV color conversion which did display the color, but with a red filter for some reason.

I won't show all of the code due to the fact it is at least 2000 lines, but I will show what part of the code where I am having my problem.

Server side:

sct_img = sct.grab(bounding_box)
img_np = np.array(sct_img)
frame = img_np
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
frame = cv2.resize(frame, (0,0), fx = 0.70, fy = 0.70)
data = pickle.dumps(frame)
message_size = struct.pack("L", len(data))
clientsocket.sendall(message_size   data)

Client side:

 frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
 frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
 frame = cv2.resize(frame, (x, y))
 cv2.imshow('frame', frame)

CodePudding user response:

When you reduce a color image to grayscale, you're discarding information. There's no way to get color back. If you want to get an acceptable frame rate, you're going to have to choose some other approach.

CodePudding user response:

When you convert an RGB image to grayscale, color data gets thrown away, hence you won't be able to get the original image back. Observe the output from code below:

import cv2
import numpy as np

# Create image
img = np.full((500, 500, 3), 255, 'uint8')
cv2.rectangle(img, (50, 100), (250, 300), (0, 0, 96), -1)
cv2.circle(img, (300, 350), 100, (0, 50, 0), -1)
cv2.drawContours(img, [np.array([(300, 50), (200, 250), (400, 250)])], 0, (255, 0, 0), -1)

# Convert to grayscale
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print(np.unique(img_gray))

# Show images
cv2.imshow("BGR", img)
cv2.imshow("Gray", img_gray)
cv2.waitKey(0)

Output:

enter image description here

enter image description here

As you can see, with the image of a red, green and blue shape (each a specific shade of its color), converting it into grayscale results in the three colors turning into one; (29, 29, 29). There is no way the computer will be able to tell that the three shapes used to be different colors.

  • Related