How can I use imshow to display multiple images in multiple windows?

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

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

Can this be done?

Thanks!

+7
source share
5 answers

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

  • winname is the name of the window.
  • image - the image to be displayed.

A window is identified by its name. Therefore, to display two images (img1, img2) in two different windows; use imshow with a different name, for example: -

 imshow("1",img1); imshow("2",img2); 
+10
source

This works for me in Python with the 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 same place on the screen, so it only looks like one open window (Ubuntu 14.4). I can drag to the other side.

Now I'm looking for how to place two side by side automatically, here is how I found this question.

+4
source

And here's 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 a string to imshow.

+3
source

If your images are in numpy arrays, you can use the numpy.hstack function, which combines two arrays into one and uses cv2.imshow () to display the array.

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

0
source

I ran into the problem of having to create an arbitrary number of openCV windows. I saved them in a list and could not just loop it to display them:

 # images is a list of openCV image object for img in images: cv2.imshow('image',i) 

This does not work for a trivial reason when images require a different label, so this loop will only display the last item in the list.

This can be solved using the iterator and Python string formatting tools:

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

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

0
source

Source: https://habr.com/ru/post/950792/


All Articles