简体   繁体   中英

How to concatenate three images?

ValueError: operands could not be broadcast together with shapes (310,1643,3) (1673,1643,3)

add = (image1 + image2 + image3)

image1 = cv2.imread("OrchidAMHI0011570910_crop.jpg")
image2 = cv2.imread("OrchidAMHI0011570910_crop1.jpg")
image3 = cv2.imread("OrchidAMHI0011570910_crop2.jpg")
add = (image1 + image2 + image3)
cv2.imshow("Addition", add)

I expect the output as concatenation of all three images

I assume, you want a concatenation of your images as mentioned in the question body. This can be done, for example, by using OpenCV's hconcat and vconcat functions.

Here's a short example for a vertical concatenation. Please pay attention, that the width has to be the same for all input images. For horizontal concatenation, the height has to be the same.

Let's have these three input images:

输入1

输入2

输入3

Then, we use this short code snippet:

import cv2
import numpy as np

# Set up sample images for vertical concatenation; width must be identical
image1 =  64 * np.ones((100, 200, 3), np.uint8)
image2 = 128 * np.ones((200, 200, 3), np.uint8)
image3 = 192 * np.ones((300, 200, 3), np.uint8)

# Concatenate using cv2.vconcat
add = cv2.vconcat([image1, image2, image3])

# Visualization
cv2.imshow('image1', image1)
cv2.imshow('image2', image2)
cv2.imshow('image3', image3)
cv2.imshow('Addition', add)
cv2.waitKey(0)

And, the final result looks like this:

产量

Another option could be, since OpenCV uses NumPy under the hood, to use vstack . So, instead of

add = cv2.vconcat([image1, image2, image3])

you could also use

add = np.vstack([image1, image2, image3])

Hope that helps!

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