OpenCV / Python에서 카메라 매개 변수 설정
저는 Thorlabs (DC1545M)의 USB 카메라와 함께 OpenCV (2.4)와 Python (2.7.3)을 사용하고 있습니다.
비디오 스트림에서 일부 이미지 분석을 수행 중이며 비디오 스트림에서 일부 카메라 매개 변수를 변경할 수 있기를 원합니다. 혼란스러운 점은 일부 카메라 속성을 변경할 수 있지만 전부는 아니고 내가 뭘 잘못하고 있는지 잘 모르겠다는 것입니다.
다음은 Python에서 cv2 바인딩을 사용하는 코드이며 실행되는지 확인할 수 있습니다.
import cv2
#capture from camera at location 0
cap = cv2.VideoCapture(0)
#set the width and height, and UNSUCCESSFULLY set the exposure time
cap.set(3,1280)
cap.set(4,1024)
cap.set(15, 0.1)
while True:
ret, img = cap.read()
cv2.imshow("input", img)
#cv2.imshow("thresholded", imgray*thresh2)
key = cv2.waitKey(10)
if key == 27:
break
cv2.destroyAllWindows()
cv2.VideoCapture(0).release()
참고로 cap.set () 명령 의 첫 번째 인수는 아래 나열된 카메라 속성의 열거 형을 참조합니다.
0. CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
1. CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
2. CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file
3. CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
4. CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
5. CV_CAP_PROP_FPS Frame rate.
6. CV_CAP_PROP_FOURCC 4-character code of codec.
7. CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
8. CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
9. CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
10. CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
11. CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
12. CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
13. CV_CAP_PROP_HUE Hue of the image (only for cameras).
14. CV_CAP_PROP_GAIN Gain of the image (only for cameras).
15. CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
16. CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
17. CV_CAP_PROP_WHITE_BALANCE Currently unsupported
18. CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
내 질문은 다음과 같습니다.
python / opencv를 통해 카메라 노출 시간 (또는 다른 카메라 매개 변수)을 설정할 수 있습니까?
그렇지 않은 경우 이러한 매개 변수를 어떻게 설정해야합니까?
참고 :이 작업을 수행하는 방법을 보여주는 카메라 제조업체에서 제공 한 C ++ 코드가 있지만 저는 C ++ 전문가가 아니므로 모든 파이썬 기반 솔루션을 고맙게 생각합니다.
미리 감사드립니다!
모든 매개 변수가 모든 카메라에서 지원되는 것은 아닙니다. 실제로 OpenCV 라이브러리에서 가장 문제가되는 부분 중 하나입니다. Android 카메라에서 USB 카메라, 전문가 용 카메라에 이르기까지 각 카메라 유형은 매개 변수를 수정할 수있는 다른 인터페이스를 제공합니다. OpenCV 코드에는 많은 분기를 지원하기 위해 많은 분기가 있지만 물론 모든 가능성이 적용되는 것은 아닙니다.
할 수있는 일은 카메라 드라이버를 조사하고 OpenCV 용 패치를 작성하여 code.opencv.org로 보내는 것입니다. 이렇게하면 다른 사람들이 당신의 일을 즐기는 것처럼 다른 사람들이 당신의 일을 즐길 수 있습니다.
There is also a possibility that your camera does not support your request - most USB cams are cheap and simple. Maybe that parameter is just not available for modifications.
If you are sure the camera supports a given param (you say the camera manufacturer provides some code) and do not want to mess with OpenCV, you can wrap that sample code in C++ with boost::python, to make it available in Python. Then, enjoy using it.
To avoid using integer values to identify the VideoCapture
properties, one can use, e.g., cv2.cv.CV_CAP_PROP_FPS
in OpenCV 2.4 and cv2.CAP_PROP_FPS
in OpenCV 3.0. (See also Stefan's comment below.)
Here a utility function that works for both OpenCV 2.4 and 3.0:
# returns OpenCV VideoCapture property id given, e.g., "FPS"
def capPropId(prop):
return getattr(cv2 if OPCV3 else cv2.cv,
("" if OPCV3 else "CV_") + "CAP_PROP_" + prop)
OPCV3
is set earlier in my utilities code like this:
from pkg_resources import parse_version
OPCV3 = parse_version(cv2.__version__) >= parse_version('3')
I had the same problem with openCV on Raspberry Pi... don't know if this can solve your problem, but what worked for me was
import time
import cv2
cap = cv2.VideoCapture(0)
cap.set(3,1280)
cap.set(4,1024)
time.sleep(2)
cap.set(15, -8.0)
the time you have to use can be different
I wasn't able to fix the problem OpenCV either, but a video4linux (V4L2) workaround does work with OpenCV when using Linux. At least, it does on my Raspberry Pi with Rasbian and my cheap webcam. This is not as solid, light and portable as you'd like it to be, but for some situations it might be very useful nevertheless.
Make sure you have the v4l2-ctl application installed, e.g. from the Debian v4l-utils package. Than run (before running the python application, or from within) the command:
v4l2-ctl -d /dev/video1 -c exposure_auto=1 -c exposure_auto_priority=0 -c exposure_absolute=10
It overwrites your camera shutter time to manual settings and changes the shutter time (in ms?) with the last parameter to (in this example) 10. The lower this value, the darker the image.
If anyone is still wondering what the value in CV_CAP_PROP_EXPOSURE
might be:
Depends. For my cheap webcam I have to enter the desired value directly, e.g. 0.1 for 1/10s. For my expensive industrial camera I have to enter -5 to get an exposure time of 2^-5s = 1/32s.
참고URL : https://stackoverflow.com/questions/11420748/setting-camera-parameters-in-opencv-python
'Nice programing' 카테고리의 다른 글
MongoDB 스키마 설계-작은 문서가 많거나 큰 문서가 적습니까? (0) | 2020.10.19 |
---|---|
왜 0.1 + 0.2 == 0.3 in D입니까? (0) | 2020.10.19 |
Homebrew를 사용하여 OS X에서 OpenSSL 업데이트 (0) | 2020.10.19 |
재귀 적으로 Git LFS 트랙 폴더 (0) | 2020.10.19 |
Angular Cli Webpack, 외부 js 파일을 추가하거나 번들링하는 방법은 무엇입니까? (0) | 2020.10.19 |