Search code examples
pythonimageopencv

Images are opened differently when changing the Title


I'm have a list of image paths I'm opening and showing using this:

    for path in image_paths:
        print 'Path for the this image is: "{}"'.format(path)
        img = cv2.imread(path)
        cv2.imshow("",img)
        cv2.waitKey(250)
        cv2.destroyAllWindows()

And it opens each image for 250 ms in the center-ish of the screen and moves to next image, as expected. When I change the 1st parameter of cv2.imshow("",img) into cv2.imshow("image {}".format(path[-7:-4]),img) which shuold show the title "image 001", "image XYZ" and so on, the images are opened differntly:

The 1st is opened in the center-ish screen, and the second is opened a bit to the right and to the bottom, and so on, until it reaches some kind of limit and jumps to the upper left corner of some invisible frame. Why is this happening?


Solution

  • cv2.imshow() displays an image in a window but the first argument (window title) is used to create different windows.

    When multiple calls to cv2.imshow() use the same window title, as in:

    cv2.imshow("",img)
    

    OpenCV creates a single window (with an empty name) that is reused every time a new image has to be displayed.

    On the other hand, calling cv2.imshow("image {}".format(path[-7:-4]), img) in a loop will generate different window titles for every iteration of the loop, which in turn creates a new window on every call. So what you are seeing is the expected behaviour!

    If you want several windows to appear at the same place of the screen, simply call cv2.moveWindow() after cv.imshow() with the appropriate screen coordinates:

    window_title = "image {}".format(path[-7:-4])
    cv2.imshow(window_title, img)
    cv2.moveWindow(window_title, 0, 0)