|
| 1 | +""" |
| 2 | +File: sensor_bin_to_sensor_raw_burst_capture.py |
| 3 | +Description: converts the image sensor memory dumps (.bin) of |
| 4 | + RAW Burst Capture from the FPGA Platform to |
| 5 | + Bayer RAW frames (.raw) containing valid pixel |
| 6 | + data. |
| 7 | + It also converts the Bayer RAW frames to |
| 8 | + equivalent grayscale .png for visualization. |
| 9 | +Author: 10xEngineers |
| 10 | +------------------------------------------------------------ |
| 11 | +""" |
| 12 | +import numpy as np |
| 13 | +import matplotlib.image as mpimg |
| 14 | +import matplotlib.pyplot as plt |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +# Path of the directory containing "OV5467" and "AR1335" directories |
| 18 | +path = "./" |
| 19 | + |
| 20 | +# Supported Sensors and selected sensor (SENSOR) |
| 21 | +SupportedSensors = { |
| 22 | + "AR1335": 1, |
| 23 | + "OV5647": 2 |
| 24 | +} |
| 25 | +SENSOR = "AR1335" |
| 26 | + |
| 27 | +# start and end index of burst capture frames for converting .bin to .raw |
| 28 | +start, end = 1, 250 |
| 29 | + |
| 30 | +# parent directory |
| 31 | +p = Path(path) |
| 32 | +parent_dir = p.resolve().joinpath(SENSOR) |
| 33 | + |
| 34 | +# Making directories for saving .raw and .png files |
| 35 | +raw_dir = "Burst_Capture_RAW" |
| 36 | +png_dir = "Burst_Capture_PNG" |
| 37 | +bin_dir = "Burst_Capture" # Created by firmware, DO NOT MODIFY |
| 38 | +# joining paths with parent directory |
| 39 | +raw_path = parent_dir/ raw_dir |
| 40 | +Path.mkdir(raw_path, exist_ok=True) |
| 41 | +png_path = parent_dir / png_dir |
| 42 | +Path.mkdir(png_path, exist_ok=True) |
| 43 | +bin_path = parent_dir / bin_dir |
| 44 | + |
| 45 | +# Selecting height and width based on selected sensor |
| 46 | +if(SupportedSensors[SENSOR] == SupportedSensors["AR1335"]): |
| 47 | + h, w = 1536, 2048 |
| 48 | + |
| 49 | +if(SupportedSensors[SENSOR] == SupportedSensors["OV5647"]): |
| 50 | + h, w = 1944, 2592 |
| 51 | + |
| 52 | +h = int(h) |
| 53 | +w = int(w) |
| 54 | + |
| 55 | +# Images are stored in the form of rows where the size of each row in bytes |
| 56 | +# should be a multiple of 256, each such row size is called 'stride' |
| 57 | +# For raw10 format, 3 pixels are packed into 4 bytes |
| 58 | +stride = np.floor(np.floor(np.floor((w+2)/3) *4 +256 - 1) /256) * 256 |
| 59 | +stride = stride.astype (np.uint16) |
| 60 | +pixelsInStride= int((stride/4)*3) |
| 61 | + |
| 62 | +for index in range (start, end+1): |
| 63 | +# reading the dumped binary file |
| 64 | + filename = 'RAW' + str(index) + '.bin' |
| 65 | + filepath = bin_path / filename |
| 66 | + print('Processing ' + 'RAW'+ str(index) + ' ...') |
| 67 | + with open(filepath, 'rb') as f: |
| 68 | + # read the contents of the file into a new array |
| 69 | + arr = np.fromfile(f, dtype=np.uint8) |
| 70 | + |
| 71 | + # Reshape the array into groups of 4 elements |
| 72 | + grouped_array = arr.reshape(-1, 4) |
| 73 | + flipped_array = np.flip(grouped_array, axis=1) |
| 74 | + result_list = [] |
| 75 | + for inner_array in flipped_array: |
| 76 | + # Convert each element to binary and concatenate |
| 77 | + binary_concatenated = ''.join([format(x, '08b') for x in inner_array]) |
| 78 | + result_list.append((int(binary_concatenated[2:12],2),int(binary_concatenated[12:22],2),int(binary_concatenated[22:32],2))) |
| 79 | + img = np.array(result_list).reshape((h, pixelsInStride))[:, 0:w].astype(np.uint16) |
| 80 | + |
| 81 | + # dumping a .raw file for inf_isp |
| 82 | + filename = filename[:-4] |
| 83 | + extension = ".raw" |
| 84 | + |
| 85 | + with open('{}{}'.format(str(raw_path/filename),extension),'wb') as f: |
| 86 | + img.tofile(f) |
| 87 | + |
| 88 | + # dumping a numpy array |
| 89 | + img_norm = np.interp(img, (img.min(), img.max()), (0,1023 )).astype(np.uint8) |
| 90 | + plt.imshow(img_norm,cmap='gray').write_png(str(png_path/filename) + '.png') |
0 commit comments