Reputation: 1140
I am working on Ubuntu using python 2.7 with OpenCV I am trying to display 4 images in the same window. Because opencv does not have any function for this I will try to make one. I have search in Stack Overflow and I found some complicated solutions but generated in C/C++ which I do not know.
So, my strategy is: I have 4 color BGR images. All 4 same size: (x,y) I will generate a numpy array of zeros 4 times the size of the original images (2x,2y) I will copy each images in the numpy zero array, but in different position, so they will look each image next to the other:
import cv2
import numpy as np
def ShowManyImages():
img1 = cv2.imread('img1.png',1)
img2 = cv2.imread('img2.png',1)
img3 = cv2.imread('img3.png',1)
img4 = cv2.imread('img4.png',1)
x,y,_ = img1.shape
print x, y # show: 500,500
img_final = np.zeros((x*2, y*2, 3), np.uint8)
print img_final.shape # show: 1000,1000,3
img_final[0:500,0:500,:] = img1[:,:,:]
cv2.imshow('final', img_final)
cv2.waitKey()
cv2.destroyAllWindows()
ShowManyImages()
However, the code does not show any image. I can't figure out why. Any ideas?
Note: to make the code shorter, I only show the code for the first img (img1)
Upvotes: 0
Views: 2682
Reputation: 221514
Here's an approach with few stack
operations assuming a0,a1,a2,a3
as the four 3D
(RGB) image arrays -
a01 = np.hstack((a0,a1))
a23 = np.hstack((a2,a3))
out = np.vstack((a01, a23))
Sample run -
In [245]: a0 = np.random.randint(0,9,(2,3,3))
...: a1 = np.random.randint(0,9,(2,3,3))
...: a2 = np.random.randint(0,9,(2,3,3))
...: a3 = np.random.randint(0,9,(2,3,3))
...:
In [246]: a01 = np.hstack((a0,a1))
...: a23 = np.hstack((a2,a3))
...: out = np.vstack((a01, a23))
...:
In [247]: out.shape
Out[247]: (4, 6, 3)
Thus, we would have them stacked like so -
a0 | a1
-------
a2 | a3
Upvotes: 2