0

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

BIBIN VARKEY
  • 1
  • 1
  • 4
  • You can use `cv2.vconcat()` and `cv2.hconcat()`, see here: https://stackoverflow.com/questions/14579541/how-do-i-concatenate-two-matrices-in-python-opencv/72177160#72177160 – Jeru Luke May 09 '22 at 19:25

1 Answers1

2

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!

HansHirse
  • 18,010
  • 10
  • 38
  • 67
  • # Set up sample images for vertical concatenation; width must be identical image1 = 64 * np.ones(('OrchidAMHI0011570910_crop.jpg'), np.uint8) image2 = 128 * np.ones(('OrchidAMHI0011570910_crop1.jpg'),np.uint8) image3 = 192 * np.ones(('OrchidAMHI0011570910_crop2.jpg'),np.uint8) TypeError: 'str' object cannot be interpreted as an integer – BIBIN VARKEY Sep 03 '19 at 09:30
  • @BIBINVARKEY No, you just leave your initial `image1 = cv2.imread("OrchidAMHI0011570910_crop.jpg")`, etc. The `np.ones(...)` stuff was just to set up sample images. Please have a look at some basic tutorials on Python and OpenCV before continuing to work on "bigger" problems. – HansHirse Sep 03 '19 at 10:22
  • @HansHire: I understand that the above was just an example. However, I have cropped an image into 3 part and done skew correction on all 3 parts and then concatenated all 3 cropped parts together. However, the middle portion of the image which I deskewed after cropping remains the same after concatenation. But I have done concatenation of all three now – BIBIN VARKEY Sep 03 '19 at 10:55