Why doesn't OpenCV cv2.moveWindow move always to the same XY-position?

Viewed 30

The question Why is opencv's moveWindow command inconsistent? was asked 3 years, 1 month ago and was related to OpenCV (version 4.1.0) in Python (version 3.7.3). The question remains still unanswered. The statement

OpenCV's GUI functionality is not great, and is mostly available for debugging purposes. I would not rely on it. [...] – alkasm

provided as comment there doesn't answer the question about a possible reason for such behavior. But knowing the reason for such behavior is a necessary step toward a fix or debugging of the OpenCV code.

Today I am using OpenCV 4.5.5 in Python 3.9 and experience the same problem positioning windows displaying following image:

tetris_blocks.png

along with two other images demonstrating finding of contours with OpenCV using following code making it possible for you to reproduce the issue:

import time
import cv2 as cv
cv.namedWindow("Tetris Blocks") # flags=cv.CV_WINDOW_AUTOSIZE # to image size
time.sleep(2.0) # required to place the window correctly
cv.moveWindow(winname="Tetris Blocks",          x= 1150, y=  10)
cv.namedWindow("Tetris Blocks Gray") # flags=cv.CV_WINDOW_AUTOSIZE # to image size
time.sleep(2.0) # required to place the window correctly
cv.moveWindow(winname="Tetris Blocks Gray",     x= 1150, y= 380)
cv.namedWindow("Tetris Blocks Contours") # flags=cv.CV_WINDOW_AUTOSIZE # to image size
time.sleep(2.0) # required to place the window correctly
cv.moveWindow(winname="Tetris Blocks Contours", x= 1150, y= 730)
cv_img = cv.imread("tetris_blocks.png")
cv.imshow("Tetris Blocks", cv_img)
# cv.waitKey(0)
cv_img_gray = cv.cvtColor(cv_img, cv.COLOR_BGR2GRAY)
cv.imshow("Tetris Blocks Gray", cv_img_gray)
# cv.waitKey(0)
thresh = cv.threshold(cv_img_gray, 225, 255, cv.THRESH_BINARY_INV)[1]
(cnts, _) = cv.findContours(thresh.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
cv.drawContours(cv_img, cnts, -1, (0, 0, 0), 10)
cv.imshow("Tetris Blocks Contours", cv_img)
cv.waitKey(0)

placing the windows in one column and not in one row as in the mentioned old unanswered question.

The pictures below show the result of running the above code in the same way multiple times obtaining different results. Often is the x-position the same:

Result A

where the y-position is not:

Result B

but the displacement occur also in both directions:

Result C

Looking at the provided code you can see an attempt to fix the problem as I have experienced much larger differences in the positions without 'sleeping' between positioning the windows. Inserting time.sleep() reduces the problem, but does not reliably solve it.

I suppose an easy to find and fix bug in OpenCV code behind this behavior and wonder how it comes that the problem persists in the timescale of years.

0 Answers
Related