Skip to content

Instantly share code, notes, and snippets.

@bhive01
Created April 16, 2018 02:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bhive01/7700bb119150214682b51ade25e121e9 to your computer and use it in GitHub Desktop.
Save bhive01/7700bb119150214682b51ade25e121e9 to your computer and use it in GitHub Desktop.
CU40 with Numpy Slicing (efficient for video on Raspberry Pi)
# TESTING HARDWARE
# see3CAM_CU40
# raspberry Pi 3 bf8
# Raspbian Stretch 13-03-2018
# Python 3.5.3
# OpenCV 3.4.1
# load required libraries
import math
import numpy as np
import cv2
# open up camera on port 0
cam = cv2.VideoCapture(0)
# CU40 has the following possible resolution settings
# 672 x 380
# 1280 x 720
# 1920 x 1080
# 2688 x 1520
rows = 1520
cols = 2688
#set dimensions
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, rows)
cam.set(cv2.CAP_PROP_FRAME_WIDTH, cols)
# turn off automatic RGB conversion
# this sets capture to 16bit
cam.set(cv2.CAP_PROP_CONVERT_RGB, False)
# don't need to set the FOURCC, this is handled in OpenCV cap_libv4l.cpp
# cam.set(cv2.CAP_PROP_FOURCC('Y', '1', '6', ''))
# get image from sensor module
ret_val, frame = cam.read()
# release camera
cam = cam.release()
# uncomment this line to write out raw 16 bit image captured
# recall that the image is 16 bit, but the values are 10-bit (0-1023)
#cv2.imwrite('raw.pgm', im)
# CU40 camera
# convert from 10 bit (1024 levels) to 8 bit (256 levels) 256/104 = 0.25
bf8 = np.array(frame//4, dtype = np.uint8)
# uncomment this line to write out scaled (10 bit to 8 bit) raw image
#cv2.imwrite('rawscaled.pgm', bf8)
# Typical Bayer filter is RGGB
# B is 0,0; R is 1,1
# B G
# G R
# econ sensors are RGIRB
# B IR -> B G
# G R -> G R
# create a copy of the bayer array to replace IR with nearest G
bRGGB = np.copy(bf8)
# IR array is 1/4 of the pixels
# create 8-bit array of 0s to fill
IR = np.zeros([rows//2, cols//2], np.uint8)
# copy out IR pixels
IR = bf8[1::2, 0::2]
# copy over IR pixels with nearest G pixel
bf8[1::2, 0::2] = bf8[0::2, 1::2]
# convert Bayer RGGB into BGR image [rows, cols, dim = 3]
# cv2.COLOR_BayerRG2BGR bayer demosaicing
# cv2.COLOR_BayerRG2BGR_EA edge aware demosaicing
BGRim = cv2.cvtColor(bRGGB, cv2.COLOR_BayerRG2BGR_EA)
# write out RGB image
cv2.imwrite('RGB.png', BGRim)
# write out IR image
cv2.imwrite('IR.png', IR)
# TESTING HARDWARE
# see3CAM_CU40
# raspberry Pi 3 bf8
# Raspbian Stretch 13-03-2018
# Python 3.5.3
# OpenCV 3.4.1
# load required libraries
import math
import numpy as np
import cv2
# open up camera on port 0
cam = cv2.VideoCapture(0)
# CU40 has the following possible resolution settings
# 672 x 380
# 1280 x 720
# 1920 x 1080
# 2688 x 1520
rows = 1520
cols = 2688
#set dimensions
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, rows)
cam.set(cv2.CAP_PROP_FRAME_WIDTH, cols)
# turn off automatic RGB conversion
# this sets capture to 16bit
cam.set(cv2.CAP_PROP_CONVERT_RGB, False)
# don't need to set the FOURCC, this is handled in OpenCV cap_libv4l.cpp
# cam.set(cv2.CAP_PROP_FOURCC('Y', '1', '6', ''))
# get image from sensor module and trash return read value
_, frame = cam.read()
# release camera
cam = cam.release()
# uncomment this line to write out raw 16 bit image captured
# recall that the image is 16 bit, but the values are 10-bit (0-1023)
#cv2.imwrite('raw.pgm', im)
# CU40 camera
# convert from 10 bit (1024 levels) to 8 bit (256 levels) 256/104 = 0.25
bf8 = np.array(frame//4, dtype = np.uint8)
# uncomment this line to write out scaled (10 bit to 8 bit) raw image
#cv2.imwrite('rawscaled.pgm', bf8)
# Typical Bayer filter is RGGB
# B is 0,0; R is 1,1
# B G
# G R
# econ sensors are RGIRB
# B IR -> B G
# G R -> G R
# create a copy of the bayer array to replace IR with nearest G
#bRGGB = np.copy(bf8)
# IR array is 1/4 of the pixels
# create 8-bit array of 0s to fill
IR = np.zeros([rows//2, cols//2], np.uint8)
BGR = np.zeros([rows//2, cols//2, 3], np.uint8)
#set R
BGR[:, :, 2] = bf8[1::2, 1::2]
#set G
BGR[:, :, 1] = bf8[0::2, 1::2]
#set B
BGR[:, :, 0] = bf8[0::2, 0::2]
#set IR data into new array
IR = bf8[1::2, 0::2]
# write out RGB image
cv2.imwrite('RGB.png', BGR)
# write out IR image
cv2.imwrite('IR.png', IR)
import time
#import subprocess
import math
import numpy as np
import cv2
# subprocess.call("v4l2-ctl -d /dev/video0 brightness=0", shell=True)
cv2.namedWindow("bf8", cv2.WINDOW_AUTOSIZE)
cv2.namedWindow("IR", cv2.WINDOW_AUTOSIZE)
vs = cv2.VideoCapture(0)
# CU40 has the following possible resolution settings
# 672 x 380
# 1280 x 720
# 1920 x 1080
# 2688 x 1520
rows = 380
cols = 672
# create array for IR image to fill
IR = np.zeros([rows//2, cols//2], np.uint8)
vs.set(cv2.CAP_PROP_FRAME_WIDTH, cols)
vs.set(cv2.CAP_PROP_FRAME_HEIGHT, rows)
vs.set(cv2.CAP_PROP_CONVERT_RGB, False) # turn off RGB conversion
while True:
#grab capture value (into null) and frame
_, frame = vs.read()
# CU40 camera
# convert from 10 bit (1024 levels) to 8 bit (256 levels) 256/104 = 0.25
bf8 = np.array(frame//4, dtype = np.uint8)
# copy out IR pixels
IR = bf8[1::2, 0::2]
# copy over IR pixels with nearest G pixel
bf8[1::2, 0::2] = bf8[0::2, 1::2]
#convert Bayer to RGB
bf8 = cv2.cvtColor(bf8, cv2.COLOR_BayerRG2BGR)
# show results
cv2.imshow("bf8", bf8)
cv2.imshow("IR", IR)
# detect waitkey of q to quit
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
cv2.destroyAllWindows()
vs.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment