Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the size of a window in OpenCV

Tags:

c

opencv

I have searched online and wasn't able to find an answer to this so I figured I could ask the experts here. Is there anyway to get the current window resolution in OpenCV? I've tried the cvGetWindowProperty passing in the named instance of the window, but I can't find a flag to use.

Any help would be greatly appreciated.

like image 779
Seb Avatar asked Jul 11 '11 19:07

Seb


2 Answers

You can get the width and height of the contents of the window by using shape[1] and shape[0] respectively. I think when you use Open CV, the image from the camera is stored as a Numpy array, with the shape being [rows, cols, bgr_channels] like [480,640,3]

code e.g.

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

You could also call getWindowImageRect() which gets a whole rectangle: x,y,w,h

e.g.

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)
like image 193
Lindsay Fowler Avatar answered Nov 12 '22 04:11

Lindsay Fowler


Hmm... it's not really a great answer (pretty hack!), but you could always call cvGetWindowHandle. With that native window handle, I'm sure you could figure out some native calls to get the contained image sizes. Ugly, hackish, and not-very-portable, but that's the best I could suggest given my limited OpenCV exposure.

like image 23
aardvarkk Avatar answered Nov 12 '22 02:11

aardvarkk