forked from scivision/pibayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getrawimage.py
executable file
·72 lines (68 loc) · 2.65 KB
/
getrawimage.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
"""
Demo of reading raw Bayer 10-bit data from Raspberry Pi camera chip using PiCamera module.
Notes:
1) can only read full chip, no binning or ROI: 2592x1944 pixel image with current imaging chip
2) captures a single image
3) sudo apt-get install python3-picamera python3-scipy
Basis from Michael Hirsch https://scivision.co
Inspiration from Camera Particle Detector (Magdalen College School)
Pieter Kuiper
"""
from __future__ import division,absolute_import
from time import sleep,time
from scipy.misc import bytescale,imsave
from matplotlib.pyplot import figure,draw,pause
from numpy import unravel_index
#
from picamera import PiCamera
#
from params import getparams,setparams
from rawbayer import grabframe
def pibayerraw(fn,exposure_sec,bit8):
with PiCamera() as cam: #load camera driver
print('camera startup gain autocal')
#LED automatically turns on, this turns it off
cam.led = False
sleep(0.75) # somewhere between 0.5..0.75 seconds to let camera settle to final gain value.
setparams(cam,exposure_sec) #wait till after sleep() so that gains settle before turning off auto
getparams(cam)
counter = 1
#%% main loop
while True:
# tic = time()
img10 = grabframe(cam)
# print('{:.1f} sec. to grab frame'.format(time()-tic))
#%% linear scale 10-bit to 8-bit
if bit8:
img = bytescale(img10,0,1024,255,0)
else:
img = img10
#%% write to PNG or JPG or whatever based on file extension
max_value = img.max()
print(max_value)
if max_value > 50:
idx = unravel_index(img.argmax(), img.shape)
xidx = idx[0]
yidx = idx[1]
print(xidx, yidx)
xlow = max(0, xidx-25)
ylow = max(0, yidx-25)
xhi = min(1944, xidx+25)
yhi = min(2592, yidx+25)
imsave(fn+'%03d' % counter + '.png',img[xlow:xhi,ylow:yhi])
counter = counter + 1
# break
return img
if __name__ == '__main__':
from argparse import ArgumentParser
p = ArgumentParser(description='Raspberry Pi Picamera demo with raw Bayer data')
p.add_argument('-e','--exposure',help='exposure time [seconds]',type=float)
p.add_argument('-8','--bit8',help="convert output to 8-bit",action='store_true')
p.add_argument('filename',help='output filename to write [png,jpg]',nargs='?')
p = p.parse_args()
try:
print('press Ctrl c to end program')
img = pibayerraw(p.filename, p.exposure,p.bit8)
except KeyboardInterrupt:
pass