0

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)

2
  • 1
    Using numpy.bmat might be cleaner giving a numpy.matrix that would need conversion to array. Commented Apr 27, 2017 at 21:46
  • Thank you for the input. I did not know this option. I tried bmat but I got an error message: 'ValueError: shape too large to be a matrix.' I went to the documentation and I undersand is only for 2D arrays?'out : matrix Returns a matrix object, which is a specialized 2-D array. ' Commented Apr 27, 2017 at 21:57

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.