forked from raspberrypi/picamera2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opencv_mertens_merge.py
executable file
·44 lines (35 loc) · 1.27 KB
/
opencv_mertens_merge.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
39
40
41
42
43
44
#!/usr/bin/python3
import cv2
import time
from null_preview import *
from picamera2 import *
# Simple Mertens merge with 3 exposures. No image alignment or anything fancy.
RATIO = 3.0
picam2 = Picamera2()
preview = NullPreview(picam2)
picam2.configure(picam2.preview_configuration())
picam2.start()
# Run for a second to get a reasonable "middle" exposure level.
time.sleep(1)
metadata = picam2.capture_metadata()
exposure_normal = metadata["ExposureTime"]
gain = metadata["AnalogueGain"] * metadata["DigitalGain"]
picam2.stop()
capture_config = picam2.preview_configuration(main={"size": (1024, 768), "format": "RGB888"})
picam2.configure(capture_config)
picam2.start({"ExposureTime": exposure_normal, "AnalogueGain": gain})
normal = picam2.capture_array()
picam2.stop()
exposure_short = int(exposure_normal / RATIO)
picam2.start({"ExposureTime": exposure_short, "AnalogueGain": gain})
short = picam2.capture_array()
picam2.stop()
exposure_long = int(exposure_normal * RATIO)
picam2.start({"ExposureTime": exposure_long, "AnalogueGain": gain})
long = picam2.capture_array()
picam2.stop()
merge = cv2.createMergeMertens()
merged = merge.process([short, normal, long])
merged = np.clip(merged * 255, 0, 255).astype(np.uint8)
cv2.imwrite("normal.jpg", normal)
cv2.imwrite("merged.jpg", merged)