简体   繁体   中英

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 http://i59.tinypic.com/2q1usqs.jpg filter http://i59.tinypic.com/35iq612.jpg result http://i62.tinypic.com/21c9vmf.jpg

Thanks for your help !

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM