Home > Blockchain >  Merging three greyscale images into one rgb image
Merging three greyscale images into one rgb image

Time:08-05

I have three greyscale masks generated by OpenCV that filter in three specific colors. I want to be able to quickly merge them without looping through every pixel in the image (my application requires it to run in real-time) and get an output similar to this:

Desired output

I've been able to create the three masks separately, but they still need to be combined into one image, where each mask represents a different channel. The first mask would be the red channel, the second would be green, and the third blue.

Clarification: The masks are basically 1/3 of the final image I want to create. I need a way to interpolate them so that they don't end up being the same color in the output and becoming incomprehensible.

More details: I want to avoid using lots of loops since the current filter takes 4 seconds to process a 272 by 154 image. The masks are just masks created using the cv2.inRange function.

I'm not very good with using numpy or OpenCV yet so any solution that can run reasonably fast (if it can process 15-20 fps it's totally usable) would be of great help.

CodePudding user response:

As @Rotem has said, using cv2.merge to combine the three matrices into one bgr image seems to be one of the best solutions. It's really fast. Thanks!

bgr = cv2.merge((b, g, r))

I don't know how I didn't see it while reading the documentation. Oh well.

CodePudding user response:

Another way I used once

def merge_images(img1, img2, img3):
    img1 = cv2.cvtColor(img1, cv2.COLOR_GRAY2RGB)
    img2 = cv2.cvtColor(img2, cv2.COLOR_GRAY2RGB)
    img3 = cv2.cvtColor(img3, cv2.COLOR_GRAY2RGB)
    img = np.concatenate((img1, img2, img3), axis=1)
    return img
  • Related