为什么openCV moveWindow函数不工作?

cyvaqqii  于 2023-04-21  发布在  其他
关注(0)|答案(3)|浏览(176)

下面,我有一个python脚本。不工作的部分是函数cv2.moveWindow(“轨迹条”,宽度,我不知道为什么它不工作,因为我可以在我的另一个“我的凸轮”窗口上使用moveWindow函数。我在尝试使用moveWindow函数之前初始化了“TrackBars”窗口并调整了它的大小。该脚本的目的是创建一个相机窗口,并在另一个窗口中输入更改在相机窗口上绘制的圆的位置。

import cv2
print(cv2.__version__)
cam=cv2.VideoCapture(0)
WIDTH=640
HEIGHT=480
cam.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)
cam.set(cv2.CAP_PROP_FPS, 30)
cam.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*"MJPG"))

xPos = WIDTH//2
yPos = HEIGHT//2
radius = 25
def setX(x):
    global xPos
    xPos = x
def setY(y):
    global yPos
    yPos = y
def setRadius(r):
    global radius
    radius = r
cv2.namedWindow("TrackBars")
cv2.resizeWindow("TrackBars", 250, 100)
cv2.moveWindow("TrackBars", WIDTH, 0)
cv2.createTrackbar("xPos", "TrackBars", WIDTH//2, WIDTH, setX)
cv2.createTrackbar("yPos", "TrackBars", HEIGHT//2, HEIGHT, setY)
cv2.createTrackbar("radius", "TrackBars", 25, HEIGHT//2, setRadius)
while True:
    ignore,  frame = cam.read()
    cv2.circle(frame, (xPos, yPos), radius, (0, 255, 0), 2)
    cv2.imshow('my WEBcam', frame)
    cv2.moveWindow('my WEBcam',0,0)
    if cv2.waitKey(1) == ord('q'):
        break
cam.release()
jfgube3f

jfgube3f1#

根据您的代码,您可以移动“TrackBars”窗口,但不能移动“my WEBcam”窗口,因为您在while循环中使用了固定的坐标值:

while True:
    ...
    cv2.moveWindow('my WEBcam',0,0)
    ...

如果你想移动窗口,只需注解掉这行:

while True:
    ...
    # cv2.moveWindow('my WEBcam',0,0)
    ...

此外,要在“我的WEBcam”窗口之上创建“TrackBars”窗口,您需要再添加一行:

cv2.namedWindow("my WEBcam")
cv2.namedWindow("TrackBars")

我修改了代码来测试“TrackBars”窗口:

import cv2
print(cv2.__version__)
cam=cv2.VideoCapture(0)
WIDTH=640
HEIGHT=480
cam.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)
cam.set(cv2.CAP_PROP_FPS, 30)
cam.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*"MJPG"))

xPos = WIDTH//2
yPos = HEIGHT//2
radius = 25
windowX = 0
def setX(x):
    global xPos
    xPos = x
def setY(y):
    global yPos
    yPos = y
def setRadius(r):
    global radius
    radius = r

def setWindowX(x):
    global windowX
    windowX = x

cv2.namedWindow("my WEBcam")
cv2.namedWindow("TrackBars")
cv2.resizeWindow("TrackBars", 250, 200)
cv2.moveWindow("TrackBars", windowX, 400)
cv2.createTrackbar("xPos", "TrackBars", WIDTH//2, WIDTH, setX)
cv2.createTrackbar("yPos", "TrackBars", HEIGHT//2, HEIGHT, setY)
cv2.createTrackbar("radius", "TrackBars", 25, HEIGHT//2, setRadius)

# Test moveWindow
cv2.createTrackbar("moveWindow", "TrackBars", 25, HEIGHT//2, setWindowX)

while True:
    ignore,  frame = cam.read()
    cv2.circle(frame, (xPos, yPos), radius, (0, 255, 0), 2)
    cv2.imshow('my WEBcam', frame)
    cv2.moveWindow('my WEBcam',0,0)
    cv2.moveWindow("TrackBars", windowX, 400)
    if cv2.waitKey(1) == ord('q'):
        break
cam.release()

macOS

Windows

6ju8rftf

6ju8rftf2#

我没有使用cv2.namedWindow(“TrackBars”)初始化TrackBars窗口,而是发现使用cv2.imshow("TrackBars", np.zeros([10,10], dtype=np.uint8))初始化该窗口由于某种原因可以正常工作。

zzoitvuj

zzoitvuj3#

创建窗口后移动窗口。

cv2.imshow("window_name", image)
cv2.moveWindow("window_name", 800, 0)

相关问题