18

I would like to do something like the following in order to display two images on the screen:

imshow("1", img1);
imshow('2', 'img2');

Is it possible to do that?

2

6 Answers 6

16

Yes, it is possible. The function void imshow(const string& winname, InputArray mat) displays an image in the specified window, where -

  • winname – Name of the window.
  • image – Image to be shown.

The window is identified by its name. So to display two images(img1, img2), in two different window; use imshow with different name like :-

imshow("1",img1);
imshow("2",img2);
2
  • 1
    Do you know if this is possible in Python with the cv2 bindings? When I call imshow right after one another it kills the process.
    – Jesse
    Commented Feb 5, 2016 at 1:42
  • 1
    yes i would like to know python equivalent code as well. Two succesive calls to cv2.imshow does not put up two windows Commented Apr 3, 2016 at 16:56
7

I have this working in Python, with a caveat:

cv2.imshow("image 1", my_image_1)
cv2.imshow("image 2", my_image_2)
cv2.waitKey(0)

The caveat is that both windows are in the exact same spot on the screen, so it only looks like one window opened up (Ubuntu 14.4). I can mouse drag one to the side of the other.

I'm now looking for how to place the two side by side automagically, which is how I found this question..

1
  • 8
    You can move the window like this: cv2.moveWindow("image 2", 0, 0) Commented Jun 10, 2016 at 17:53
5

And here is how to do it in Python:

    cv2.namedWindow("Channels")
    cv2.imshow("Channels", image_channels)

    cv2.namedWindow("Main")
    cv2.imshow("Main", image_main)

You simply create a named window and pass its name as string to imshow.

1
  • I'm using jupyter and cv2.imshow doesn't show anything whereas plt.imshow() works. Any ideas?
    – SeanJ
    Commented Feb 24, 2017 at 23:57
5

I ran into the problem of having to create an arbitrary number of openCV windows. I had them stored in a list and couldn't simply loop over it to display them:

# images is a list of openCV image object

for img in images:
    cv2.imshow('image',i)

This doesn't work for the trivial reason of images requiring a different label, hence this loop would only display the last item in the list.

That can be solved by using an iterator and python string formatters:

for i, img in enumerator(images):
    cv2.imshow("Image number {}".format(i), img)

Therefore now all images will be displayed since they have been assigned a different label.

1
  • You meant enumerate (in the 2nd snippet).
    – CristiFati
    Commented Jun 28, 2022 at 20:31
0

If your images are in numpy arrays, you could use numpy.hstack function which merges two arrays into a single and use the cv2.imshow() to display the array.

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.hstack.html

-2
import cv2
import numpy as np

towImage = np.vstack((row_0, row_1))
cv2.imwrite('img', twoImage)

I create a module to make the modules easier to display

https://github.com/saman202/View_image_video_matrix

smp = ShowMatrixPic(width=180, height=240, row=3, column=4, atuoTile=True)
  

imgListOne = ['pic/Scott Glenn.jpg','pic/Brad Pitt.jpg',
              'pic/Scott Glenn.jpg','pic/Brad Pitt.jpg',
              'pic/Scott Glenn.jpg','pic/Brad Pitt.jpg',
              'pic/Scott Glenn.jpg','pic/Brad Pitt.jpg',
              'pic/Scott Glenn.jpg','pic/Brad Pitt.jpg'
              ]
numpy_horizontal = smp.showPic(imgListOne)
cv2.imshow('img', numpy_horizontal)
cv2.waitKey(0)
     

https://i.sstatic.net/5E3b5.png

1
  • Does this use one window or multiple windows?
    – Yunnosch
    Commented Aug 16, 2021 at 6:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.