我在网上搜索了一下,没有找到答案,所以想来这里问问专家。有没有一种方式可以在OpenCV中获取当前窗口的分辨率?我尝试了使用cvGetWindowProperty传入窗口的实例名称,但是我找不到要使用的标志。
非常感谢任何帮助。
非常感谢任何帮助。
您可以使用 shape[1] 和 shape[0] 来获取窗口内容的宽度和高度。我认为当您使用 Open CV 时,从相机获得的图像将被存储为 Numpy 数组,其形状为 [rows, cols, bgr_channels],例如 [480,640,3]。
示例代码:
import cv2 as cv2
cv2.namedWindow("myWindow")
cap = cv2.VideoCapture(0) #open camera
ret,frame = cap.read() #start streaming
windowWidth=frame.shape[1]
windowHeight=frame.shape[0]
print(windowWidth)
print(windowHeight)
cv2.waitKey(0) #wait for a key
cap.release() # Destroys the capture object
cv2.destroyAllWindows() # Destroys all the windows
console output:
640
480
import cv2 as cv2
cv2.namedWindow("myWindow")
cap = cv2.VideoCapture(0) #open camera
ret,frame = cap.read() #start streaming
windowWidth=cv2.getWindowImageRect("myWindow")[2]
windowHeight=cv2.getWindowImageRect("myWindow")[3]
print(windowWidth)
print(windowHeight)
cv2.waitKey(0) #wait for a key
cap.release() # Destroys the capture object
cv2.destroyAllWindows() # Destroys all the windows
-which very curiously printed 800 500 (the actual widescreen format from the camera)
嗯...这并不是一个很好的答案(相当的hack!),但是你可以调用cvGetWindowHandle
。通过该本地窗口句柄,我相信你可以找到一些本地调用来获取包含图像的大小。这种方法丑陋、hackish,并且不太可移植,但这是我能提供的最好建议,考虑到我对OpenCV的有限了解。