Skip to content

Commit 4d79628

Browse files
authored
Merge pull request #40 from kriboe90/dev_kristian
adding support for reading raw sensordata
2 parents bc39e90 + c4ebb0d commit 4d79628

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
lines changed

ledsa/core/_led_helper_functions_s1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import numpy as np
22

33

4-
def generate_mask_of_led_areas(image):
4+
def generate_mask_of_led_areas(image, threshold_factor):
55
im_mean = np.mean(image)
66
im_max = np.max(image)
7-
th = 0.25 * (im_max - im_mean)
7+
th = threshold_factor * (im_max - im_mean)
88
print("mean pixel value:", im_mean)
99
print("max pixel value:", im_max)
1010
im_set = np.zeros_like(image)

ledsa/core/led_helper.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import numpy as np
88
import matplotlib.pyplot as plt
9+
import rawpy
910

1011
sep = os.path.sep
1112

@@ -40,8 +41,30 @@ def load_file(filename, delim=' ', dtype='float', atleast_2d=False, silent=False
4041
return np.atleast_1d(data)
4142

4243

43-
def read_file(filename, channel=0):
44-
data = plt.imread(filename)
44+
def read_file(filename, channel, colordepth=14):
45+
"""
46+
Returns a 2D array of channel values depending on the colordepth.
47+
8bit is default range for JPG. Bayer array is a 2D array where
48+
all channel values except the selected channel are masked.
49+
"""
50+
extension = os.path.splitext(filename)[-1]
51+
if extension in ['.JPG','.JPEG', '.jpg', '.jpeg', '.PNG', '.png']:
52+
data = plt.imread(filename)
53+
elif extension in ['.CR2']:
54+
with rawpy.imread(filename) as raw:
55+
data = raw.raw_image_visible.copy()
56+
filter_array = raw.raw_colors_visible
57+
black_level = raw.black_level_per_channel[channel]
58+
white_level = raw.white_level
59+
channel_range = 2**colordepth - 1
60+
channel_array = data.astype(np.int16) - black_level
61+
channel_array *= int(channel_range / (white_level - black_level))
62+
channel_array = np.clip(channel_array, 0, channel_range)
63+
if channel == 0 or channel == 2:
64+
channel_array = np.where(filter_array == channel, channel_array, 0)
65+
elif channel == 1:
66+
channel_array = np.where((filter_array == 1) | (filter_array == 3), channel_array, 0)
67+
return channel_array
4568
return data[:, :, channel]
4669

4770

@@ -158,9 +181,9 @@ def generate_image_infos_csv(config, build_experiment_infos=False, build_analysi
158181
# ------------------------------------
159182
# """
160183

161-
def find_search_areas(image, window_radius=10, skip=10):
184+
def find_search_areas(image, window_radius=10, skip=10, threshold_factor=0.25):
162185
print('finding led search areas')
163-
led_mask = generate_mask_of_led_areas(image)
186+
led_mask = generate_mask_of_led_areas(image, threshold_factor)
164187
search_areas = find_pos_of_max_col_val_per_area(image, led_mask, skip, window_radius)
165188
print("\nfound {} leds".format(search_areas.shape[0]))
166189
return search_areas

ledsa/core/ledsa_conf.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
class ConfigData(cp.ConfigParser):
1010
# don't give img_directory a standard value
11-
def __init__(self, load_config_file=True, img_directory='.', window_radius=10, num_of_arrays=None,
12-
multicore_processing=False, num_of_cores=1, reference_img=None, date=None, start_time=None,
13-
time_diff_to_image_time=None, time_img=None, img_name_string=None, first_img=None, last_img=None,
14-
first_analyse_img=None, last_analyse_img=None, skip_imgs=0, skip_leds=0, channel=0):
11+
def __init__(self, load_config_file=True, img_directory='.', window_radius=10, threshold_factor=0.25,
12+
num_of_arrays=None, multicore_processing=False, num_of_cores=1, reference_img=None, date=None,
13+
start_time=None, time_diff_to_image_time=None, time_img=None, img_name_string=None, first_img=None,
14+
last_img=None, first_analyse_img=None, last_analyse_img=None, skip_imgs=0, skip_leds=0, channel=0):
1515
cp.ConfigParser.__init__(self, allow_no_value=True)
1616
if img_directory[-1] != path.sep:
1717
img_directory += path.sep
@@ -22,6 +22,7 @@ def __init__(self, load_config_file=True, img_directory='.', window_radius=10, n
2222
self.set('DEFAULT', '# Variables used in multiple parts of LEDSA')
2323
self['DEFAULT'][' img_directory'] = str(img_directory)
2424
self['DEFAULT'][' window_radius'] = str(window_radius)
25+
self['DEFAULT'][' threshold_factor'] = str(threshold_factor)
2526
self.set('DEFAULT', ' # Number of LED lines')
2627
self['DEFAULT'][' num_of_arrays'] = str(num_of_arrays)
2728
self.set('DEFAULT', ' # Set to True if Multiprocessing should be used')

ledsa/ledsa.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def find_search_areas(self, img_filename):
4646
ref_img_name = "{}{}".format(config['img_directory'], img_filename)
4747
data = led.read_file(ref_img_name, channel=0)
4848

49-
self.search_areas = led.find_search_areas(data, skip=1, window_radius=int(config['window_radius']))
49+
self.search_areas = led.find_search_areas(data, skip=1, window_radius=int(config['window_radius']),
50+
threshold_factor=float(config['threshold_factor']))
5051

5152
out_filename = 'analysis{}led_search_areas.csv'.format(sep)
5253
np.savetxt(out_filename, self.search_areas, delimiter=',',

ledsa/tests/AcceptanceTests/LedsaATestLibrary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def create_test_image(self, amount=1):
2727

2828
@keyword
2929
def create_and_fill_config(self):
30-
conf = ConfigData(False, '.', 10, 1, False, 1, 'test_img_0.png', None, None, 0, None, 'test_img_{}.png',
30+
conf = ConfigData(False, '.', 10, 0.25, 1, False, 1, 'test_img_0.png', None, None, 0, None, 'test_img_{}.png',
3131
0, 0, 0, 0)
3232
conf.set('analyse_positions', ' line_edge_indices', '0 2')
3333
conf.set('DEFAULT', ' date', '2018:11:27')

0 commit comments

Comments
 (0)