skip to Main Content

I have two microscope images. One in gray scale, one in red. I can merge these in photoshop, imagej, etc. I want to merge these two in opencv so I can perform this operation on many samples.

So far I’ve done the following (where dia=grayscale and epi=red).

# Load images
img_dia = cv2.imread(input_dia)
img_epi = cv2.imread(input_epi)

# Slice red channel from fluorescence image
b_epi, g_epi, r_epi = cv2.split(img_epi)

# Now I want to merge the grey scale and red image

No error messages. I could not find any documentation or other stack exchange pages to resolve this issue. Any help would be appreciated!

2

Answers


  1. considering input_epi is an RGB image(3 channels)

    when you load an image into opencv it is loaded as BGR by default

    # colour image
    img_epi = cv2.imread('input_epi.jpg')
    
    # red channel
    red_epi = img_epi[:,:,2]
    
    # gray image
    img_dia = cv2.imread('input_dia',0)
    
    # creating a resultant image for combining only two channels
    resultant_image = np.ones((img_dia.shape[0],img_dia.shape[1],2),np.uint8)
    
    # merge two channels into a single two channel array
    # first channel red
    resultant_image[:,:,0] = red_epi
    # second channel gray
    resultant_image[:,:,1] = img_dia
    
    

    Splitting and Merging Image Channels

    In OpenCV Documentation

    Warning cv2.split() is a costly operation (in terms of time). So do it only if you need it. Otherwise go for Numpy indexing.

    Actual red colour is tricky to pin point from a RGB image better to use HSV to extract particular range of colour from an image

    Login or Signup to reply.
  2. There are many ways of overlaying and blending images, but I think the nearest result to yours is probably screen-mode blending.

    You can get what you want quite simply with ImageMagick which is included in most Linux distros and is available for macOS and Windows. So, just in Terminal (or Command Prompt on Windows), you can run the following without needing to write any code:

    magick grey.tif red.tif -compose screen -composite  result.png
    

    enter image description here

    If that is not the exact blend-mode you want, there 69 modes available in ImageMagick and you can see them all listed if you run:

    magick -list compose
    

    So, if you want Hard Light blending, use:

    magick grey.tif red.tif -compose HardLight -composite  result.png
    

    You alluded to having lots of images to do, if so, you can get thousands done in parallel just by using GNU Parallel. You can search for answers on Stack Overflow that use ImageMagick and GNU Parallel by putting the following in the Stack Overflow Search box, including the square brackets:

    [imagemagick] [gnu-parallel]
    

    Or, provide some more detail on how your files are named and stored and I can help you further.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search