Search code examples
pythonmatlabopencvimage-processingsimpleitk

How can I make the composite of two images in OpenCV with Python?


I have one image that remains unchanged and another image which is the first one, but with a filter applied on it. I want to create the third image which should be the composite of these first two images.

I know that in MATLAB there is a function called as imfuse() with the default color channel green-magenta. I want to do the same thing in Python, with exactly the same color channel. How can I do this ?

Here are the images (first is the original picture, second is the first picture with the filter applied, third is the MATLAB result):

image filter result

Thanks for your help !


Solution

  • By default, imfuse simply overlays the pair of images in different color bands (Default being Method=falsecolor and ColorChannels=green-magenta).

    Here is an example in MATLAB to illustrate (it should should be easy to write this in Python/OpenCV):

    % a pair of grayscale images
    A = imread('cameraman.tif');
    B = imrotate(A,5,'bicubic','crop');    % image "A" rotated a bit
    
    % use IMFUSE
    C = imfuse(A,B);
    imshow(C)
    
    % use our version where: Red=B, Green=A, Blue=B
    C = cat(3, B, A, B);
    imshow(C)
    

    Both should give you the same thing:

    fused_images


    EDIT:

    Here is the Python/OpenCV version:

    import numpy as np
    import cv2
    
    A = cv2.imread(r"C:\path\to\a.png", 0)
    B = cv2.imread(r"C:\path\to\b.png", 0)
    
    #C = cv2.merge((B,A,B))
    C = np.dstack((B,A,B))
    cv2.imshow("imfuse",C)
    cv2.waitKey(0)
    

    opencv_python_imfuse