forked from tanayrastogi/LineFollower_OpenCV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera_calibration.py
38 lines (37 loc) · 1.37 KB
/
camera_calibration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import picamera
import picamera.array
import numpy as np
import cv2
delta_gain = 0.1
with picamera.PiCamera() as camera:
camera.resolution = (1280, 720)
camera.awb_mode = 'off'
# Start off with ridiculously low gains
rg, bg = (0.5, 0.5)
camera.awb_gains = (rg, bg)
with picamera.array.PiRGBArray(camera, size=(128, 72)) as output:
# Allow 30 attempts to fix AWB
for i in range(30):
# Capture a tiny resized image in RGB format, and extract the
# average R, G, and B values
camera.capture(output, format='rgb', resize=(128, 72), use_video_port=True)
r, g, b = (np.mean(output.array[..., i]) for i in range(3))
cv2.imshow("image", output.array)
key = cv2.waitKey(1) & 0xFF
print('R:%5.2f, B:%5.2f = (%5.2f, %5.2f, %5.2f)' % (
rg, bg, r, g, b))
# Adjust R and B relative to G, but only if they're significantly
# different (delta +/- 2)
if abs(r - g) > 2:
if r > g:
rg -= delta_gain
else:
rg += delta_gain
if abs(b - g) > 1:
if b > g:
bg -= delta_gain
else:
bg += delta_gain
camera.awb_gains = (rg, bg)
output.seek(0)
output.truncate()