Skip to content

Instantly share code, notes, and snippets.

@bhive01
Last active September 27, 2018 05:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhive01/9a06c0736e6a416af6eebac3f5026728 to your computer and use it in GitHub Desktop.
Save bhive01/9a06c0736e6a416af6eebac3f5026728 to your computer and use it in GitHub Desktop.
# 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)
#set dimensions
cam.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
# 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, im = cam.read()
# release camera
cam = cam.release()
# get dimensions of image for later processing
rows = im.shape[0]
cols = im.shape[1]
# 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(im//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)
# loop through x and y to copy out IR and replace with G
for x in range(0, rows, 2): # skips every other row
for y in range(0, cols, 2): #skips every other column
#replace IR data with nearest Green
bRGGB[x+1, y] = bf8[x, y+1]
#set IR data into new array
IR[x//2, y//2] = bf8[x+1,y]
# convert Bayer RGGB into BGR image [rows, cols, dim = 3]
BGRim = cv2.cvtColor(bRGGB, cv2.COLOR_BayerRG2BGR)
# write out RGB image
cv2.imwrite('RGB.png', BGRim)
# write out IR image
cv2.imwrite('IR.png', IR)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment