Skip to content

Commit dcbc1c7

Browse files
authored
Improve performance of VideoStreamFrame.on_frame (#131)
* Simplify, improve the performance of `on_frame` by ~40% * Temporarily add the `performance_tracker.py` to test performance on another PC * Remove temporarily `performance_tracker.py` and references * The default dynamic range that does not require scaling on simulated camera is 255, not 256.
1 parent a46596a commit dcbc1c7

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

src/instamatic/config/camera/simulate.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ camera_rotation_vs_stage_xy: -2.24
1212
default_binsize: 1
1313
default_exposure: 0.1
1414
dimensions: [516, 516]
15-
dynamic_range: 11800
15+
dynamic_range: 255
1616
interface: simulate
1717
physical_pixelsize: 0.055
1818
possible_binsizes: [1]

src/instamatic/gui/videostream_frame.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -239,23 +239,15 @@ def on_frame(self, event=None):
239239
self.stream.lock.release()
240240

241241
if frame is not None:
242-
# the display range in ImageTk is from 0 to 256
243-
if self.auto_contrast:
244-
frame = frame * (
245-
256.0 / (1 + np.percentile(frame[::4, ::4], 99.5))
246-
) # use 128x128 array for faster calculation
247-
248-
image = Image.fromarray(frame)
249-
elif self.display_range != self.display_range_default:
250-
image = np.clip(frame, 0, self.display_range)
251-
image = (256.0 / self.display_range) * image
252-
image = Image.fromarray(image)
253-
else:
254-
image = Image.fromarray(frame)
255-
256-
if self.brightness != 1:
257-
image = ImageEnhance.Brightness(image.convert('L')).enhance(self.brightness)
258-
# Can also use ImageEnhance.Sharpness or ImageEnhance.Contrast if needed
242+
# the display range in ImageTk is from 0 to 255
243+
if self.display_range != 255.0 or self.brightness != 1.0:
244+
if self.auto_contrast:
245+
display_range = 1 + np.percentile(frame[::4, ::4], 99.5)
246+
else:
247+
display_range = self.display_range
248+
frame = (self.brightness * 255 / display_range) * frame
249+
frame = np.clip(frame.astype(np.int16), 0, 255).astype(np.uint8)
250+
image = Image.fromarray(frame)
259251

260252
if self.resize_image:
261253
image = image.resize((950, 950))

0 commit comments

Comments
 (0)