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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17987598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:38:25  来源:igfitidea点击:

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

c++opencv

提问by prgbenz

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?

有可能这样做吗?

Thanks!

谢谢!

回答by Saikat

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.
  • image – Image to be shown.
  • winname – 窗口的名称。
  • image – 要显示的图像。

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

该窗口由其名称标识。所以要在两个不同的窗口中显示两个图像(img1,img2);使用imshow不同的名称,如:-

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

回答by bugmenot123

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。

回答by Gord Wait

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 lookslike one window opened up (Ubuntu 14.4). I can mouse drag one to the side of the other.

需要注意的是,两个窗口都在屏幕上完全相同的位置,因此看起来只打开了一个窗口(Ubuntu 14.4)。我可以用鼠标将一个拖到另一个的一侧。

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

我现在正在寻找如何自动将两者并排放置,这就是我发现这个问题的方式..

回答by Ahmad Moussa

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:

我遇到了必须创建任意数量的 openCV 窗口的问题。我将它们存储在一个列表中,不能简单地循环显示它们:

# 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.

因此,现在将显示所有图像,因为它们已被分配了不同的标签。

回答by Waqar Arshad

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