Below, I have a python script. The part that is not working is the function cv2.moveWindow("TrackBars", WIDTH, 0). I haven't a clue why it isn't working because I can use the moveWindow function on my other "my Cam" window. I initialized the "TrackBars" window and resized it before I try to use the moveWindow function. The point of the script is to make a camera window and have another window with inputs that change the position of a circle that is drawn on the camera window.
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()
CodePudding user response:
According to your code, you can move "TrackBars" window, but can't move "my WEBcam" window, because you used fixed coordinate values in the while loop:
while True:
...
cv2.moveWindow('my WEBcam',0,0)
...
If you want to move the window, just comment out the line:
while True:
...
# cv2.moveWindow('my WEBcam',0,0)
...
In addition, to make the "TrackBars" window on top of the "my WEBcam" window, you need to add one more line:
cv2.namedWindow("my WEBcam")
cv2.namedWindow("TrackBars")
I've modified the code to test the "TrackBars" window:
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
CodePudding user response:
Instead of initiating the TrackBars window with cv2.namedWindow("TrackBars"), I found that initiating the window with cv2.imshow("TrackBars", np.zeros([10,10], dtype=np.uint8))
worked for some reason.