I am dealing with a dataset on Kaggle.com: https://www.kaggle.com/sorour/38cloud-cloud-segmentation-in-satellite-images And what I'm trying to do is combine 3 grayscale image represent 3 channels (R, G, B) to one natural color image. So here is what I've tried:
r_np = np.array(cv2.imread(red, 0))
g_np = np.array(cv2.imread(green, 0))
b_np = np.array(cv2.imread(blue, 0))
# Add the channels to the final image
final_img = np.dstack([b_np, g_np, r_np]).astype(np.uint8)
# Directory to save
final_img_dir = store_directory "\\" "img" image_id
# Save the needed multi channel image
cv2.imwrite(final_img_dir, final_img)
And its output is a still a grayscale image. My image format is .TIF. Thank you in advance!
CodePudding user response:
You have several problems in your code:
Firstly, you need to import modules:
import numpy as np
import cv2
Secondly, you are using ugly, manifest constants instead of the nice, legible, maintainable ones that OpenCV provides for free and also you are needlessly wrapping the Numpy array you get from cv2.imread() into a Numpy array.
Finally, you are not checking for errors.
So, rather than:
r_np = np.array(cv2.imread(red, 0))
consider:
r_np = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
assert r_np is not None, "ERROR: Error reading red image"
