Search code examples
pythonimageopencvnumpy

opencv python merge different channel images into one


I have few satellite images each of them represents one channel of main satellite image, these are 11 images in total, each are labled with different channel, all images are in .tiff format with grayscale colorspace, now i simply want's to merge these images into one, to represent all channels into one image, so is this possible, remeber here, I don't want to concat images , which can be done using this:

vis = np.concatenate((img1, img2), axis=1)

I want to merge all of them into one single image , without distorting the data contained within, few channel images are attached below. enter image description here enter image description here enter image description here enter image description here

Any help is appreciated.


Solution

  • As OpenCV 3.x stored image as numpy array, we can simply average each image and add them together, provided that the height and width of the images are exactly the same.

    img_1 = cv2.imread('./imagesStackoverflow/sat_1_331-442.png')
    img_2 = cv2.imread('./imagesStackoverflow/sat_2_331-442.png')
    img_3 = cv2.imread('./imagesStackoverflow/sat_3_331-442.png')
    img_4 = cv2.imread('./imagesStackoverflow/sat_4_331-442.png')
    
    no_img = 4
    img = img_1/no_img + img_2/no_img + img_3/no_img + img_4/no_img
    

    To get a quick result, I manually edited the size of the four images to 442(h) x 331(w) pixels.

    enter image description here Below is the merged image, with 3 channels.

    enter image description here

    To merge 11 images, you may just extend the code as:

    img = img_1/no_img + img_2/no_img + img_3/no_img + ... + img_11/no_img