Home > OS >  OpenCV: Why is one trackbar shorter than others?
OpenCV: Why is one trackbar shorter than others?

Time:11-10

I have encountered a strange issue and would like someone to explain it to me, so I can avoid it in the future. When I place multiple trackbars (6 in my case) the last trackbar is much shorter. Here is the base code:

import cv2

def nothing(arguments):
    pass

cv2.namedWindow('TrackBars')
cv2.resizeWindow('TrackBars', 500, 300)
cv2.createTrackbar('Slider 1', 'TrackBars', 0, 255, nothing)
cv2.createTrackbar('Slider 2', 'TrackBars', 0, 255, nothing)
cv2.createTrackbar('Slider 3', 'TrackBars', 0, 255, nothing)
cv2.createTrackbar('Slider 4', 'TrackBars', 0, 255, nothing)
cv2.createTrackbar('Slider 5', 'TrackBars', 0, 255, nothing)
cv2.createTrackbar('Slider 6', 'TrackBars', 0, 255, nothing)
cv2.waitKey(0)

I have noticed that making the window larger solves the problem:

cv2.resizeWindow('TrackBars', 500, 320)

Even though there is a lot of space below, I thought that maybe the window crops the trackbar making it behave strange. However, while preparing the code to post here, I discovered that having 2 or 3 trackbars works fine even when one is cropped by the window. Only when there is 4 or more this behavior occurs.

import cv2

def nothing(arguments):
    pass

cv2.namedWindow('TrackBars')
cv2.resizeWindow('TrackBars', 500, 60)
cv2.createTrackbar('Slider 1', 'TrackBars', 0, 255, nothing)
cv2.createTrackbar('Slider 2', 'TrackBars', 0, 255, nothing)
cv2.waitKey(0)

What is happening here? Why is one of the trackbars shorter but only when there is many of them?


Additional infomation:

  • OpenCV: 4.6.0.66
  • OS: Windows 10
  • HighGUI backend: WinAPI

CodePudding user response:

It's a bug in OpenCV that seems to have been lingering there for a while. After a few hours of digging (and uncovering few other issues in the code along the way), I think I've nabbed it and filed a bug report with proposed resolution. I'll be making a pull request in the near future, and hopefully this can be resolved in the 4.7.0 release.

To summarize the problem: trackbar controls are added to a toolbar control, which grows vertically as more controls are added. Since main window size is fixed at the point when the controls are being created, the remaining "client area" not taken up by the toolbar progressively shrinks, until there is no space left (so its size becomes 0).

The bug is that in order to set the width of the trackbar controls, the width of that remaining "client area" is used. Once that "client area" shrinks to nothing, the size of it becomes 0, and Windows uses some other value as default width for the trackbar.

Using the width of the toolbar (which is always available) solves the problem.

With that fix applied, I can get the following:

  • Related