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?

prgbenz
  • 1,129
  • 4
  • 13
  • 27
  • possible duplicate of [Show multiple (2,3,4,…) images in the same window in OpenCV](http://stackoverflow.com/questions/5089927/show-multiple-2-3-4-images-in-the-same-window-in-opencv) – GPPK Aug 01 '13 at 07:33
  • many duplicates available! – Khashayar Aug 01 '13 at 08:10

6 Answers6

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);
Saikat
  • 1,209
  • 3
  • 16
  • 30
  • 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 Feb 05 '16 at 01: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 – aquagremlin Apr 03 '16 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..

Gord Wait
  • 152
  • 1
  • 11
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.

bugmenot123
  • 1,069
  • 1
  • 18
  • 33
  • I'm using jupyter and cv2.imshow doesn't show anything whereas plt.imshow() works. Any ideas? – SeanJ Feb 24 '17 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.

Ahmad Moussa
  • 876
  • 10
  • 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.stack.imgur.com/5E3b5.png