coderide
coderide

Reputation: 93

Skimage: how to combine two grayscale images into a channel two image

I have two grayscale images. There I would like to make a channel 2 image using that two grayscale images. There I tried using np.dstack and np.concatenate, There all return a image size of 540x1440. I could not get 540 x 720 x 2 image format. Any help is appreciated. Thank you in advance

from skimage.io import imread
from skimage.color import rgb2gray
import numpy as np

image1 = imread("main_directory/test1.png")
grayImage1 = rgb2gray(image1) # 540 x 720

image2 = imread("main_directory/test2.png")
grayImage2 = rgb2gray(image2) # 540 x 720

# channel two image
# concatenateGray = np.dstack((grayImage1 , grayImage2)) # 540 x 1440 which seems incorrect
# concatenateGray = np.concatenate((grayImage1, grayImage2), axis=2) # 540x 1440 which seems also incorrect

Upvotes: 0

Views: 1717

Answers (2)

  1. Add the third axis or channel to your images:
grayImage1_new = grayImage1.reshape(540,720,1)
grayImage2_new = grayImage2.reshape(540,720,1) 
  1. Use Alpha blending in python
alpha = 0.4
combined_image = np.zeros(grayImage1_new.shape,dtype=grayImage1_new.dtype)
combined_image[:,:,:] = (alpha * grayImage1_new[:,:,:]) + ((1-alpha) * grayImage2_new[:,:,:])

The final Code:

from skimage.io import imread
from skimage.color import rgb2gray
import numpy as np

image1 = imread("main_directory/test1.png")
grayImage1 = rgb2gray(image1) # 540 x 720

image2 = imread("main_directory/test2.png")
grayImage2 = rgb2gray(image2) # 540 x 720

grayImage1_new = grayImage1.reshape(540,720,1)
grayImage2_new = grayImage2.reshape(540,720,1) 

alpha = 0.4
combined_image= np.zeros(grayImage1_new.shape,dtype=grayImage1_new.dtype)
combined_image[:,:,:] = (alpha * grayImage1_new[:,:,:]) + ((1-alpha) * grayImage2_new[:,:,:])

Upvotes: 1

Tim Roberts
Tim Roberts

Reputation: 54649

You just need to reshape them to add a third axis, then concatenate along that axis. The reshape operation takes no time, since the data doesn't change.

concatenateGray = np.concatenate((grayImage1.reshape((20,20,1)), grayImage2.reshape((20,20,1)), axis=2)

BTW, there is no standard image format with two components per pixel, so I'm not sure what you're going to do with that.

Upvotes: 2

Related Questions