简体   繁体   English

如何使用 imshow 在多个窗口中显示多个图像?

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

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?有可能这样做吗?

Yes, it is possible.对的,这是可能的。 The function void imshow(const string& winname, InputArray mat) displays an image in the specified window, where -函数void imshow(const string& winname, InputArray mat)在指定窗口中显示图像,其中 -

  • winname – Name of the window. winname – 窗口的名称。
  • image – Image to be shown. image – 要显示的图像。

The window is identified by its name.该窗口由其名称标识。 So to display two images(img1, img2), in two different window;所以要在两个不同的窗口中显示两个图像(img1,img2); use imshow with different name like :-使用不同名称的imshow ,例如:-

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

I have this working in Python, with a caveat:我有这个在 Python 中工作,有一个警告:

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).需要注意的是,两个窗口都在屏幕上完全相同的位置,因此看起来只打开了一个窗口(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..我现在正在寻找如何自动将两者并排放置,这就是我发现这个问题的方式..

And here is how to do it in Python:下面是如何在 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.您只需创建一个命名窗口并将其名称作为字符串传递给 imshow。

I ran into the problem of having to create an arbitrary number of openCV windows.我遇到了必须创建任意数量的 openCV 窗口的问题。 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:这可以通过使用迭代器和 python 字符串格式化程序来解决:

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.因此,现在将显示所有图像,因为它们已被分配了不同的标签。

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.如果您的图像在 numpy 数组中,您可以使用 numpy.hstack 函数将两个数组合并为一个,并使用 cv2.imshow() 来显示数组。

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

import cv2
import numpy as np

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

I create a module to make the modules easier to display我创建了一个模块,使模块更容易显示

https://github.com/saman202/View_image_video_matrix 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 https://i.stack.imgur.com/5E3b5.png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM