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

后端 未结 5 1494
醉酒成梦
醉酒成梦 2021-01-02 06:40

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

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

相关标签:
5条回答
  • 2021-01-02 07:01

    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.

    0 讨论(0)
  • 2021-01-02 07:07

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

    0 讨论(0)
  • 2021-01-02 07:08

    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

    0 讨论(0)
  • 2021-01-02 07:23

    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);
    
    0 讨论(0)
  • 2021-01-02 07:23

    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.

    0 讨论(0)
提交回复
热议问题