-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_to_image.py
More file actions
31 lines (24 loc) · 1.12 KB
/
binary_to_image.py
File metadata and controls
31 lines (24 loc) · 1.12 KB
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
import numpy as np
class BinaryToImage:
def __init__(self):
self.rows = 650
self.cols = 1024
self.num_frames = None
def convert_from_binary(self, file_path):
"""
Converts the file at the specified path from a binary file to image data
:param file_path: Location of the binary file to be processed
:return: the converted image data as a numpy array
"""
# Read in the binary data from the file path
binary_data = np.fromfile(file_path, dtype=np.uint16)
# dimension for the generated image
image_frames = np.reshape(binary_data, (self.num_frames, self.rows, self.cols))
image_frames = np.transpose(image_frames, (1, 2, 0)) # To make it row, col, frame
# Invert and shift the data to fit in an AVI.
max_value = 8191
inverted_frames = max_value - image_frames
# Clamp, throwing out values that are supposed to be below the adjusted hardware offset
inverted_frames[inverted_frames > 8191] = 0
inverted_frames = inverted_frames >> 4 # Shift to fit in 0-255
return inverted_frames