BIBIN VARKEY
BIBIN VARKEY

Reputation: 1

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

Upvotes: 0

Views: 3477

Answers (1)

HansHirse
HansHirse

Reputation: 18925

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:

Input 1

Input 2

Input 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:

Output

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!

Upvotes: 2

Related Questions