Skip to content

Commit

Permalink
add downsampling option to camera; switch color channels (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
irkri committed Nov 4, 2024
1 parent fc5c66b commit 1ae2a69
Show file tree
Hide file tree
Showing 5 changed files with 366 additions and 359 deletions.
22 changes: 21 additions & 1 deletion blitz/data/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def __init__(
buffer: int,
frame_rate: int,
grayscale: bool,
downsample: float,
) -> None:
super().__init__()
self.cam = cv2.VideoCapture(cam)
Expand All @@ -29,6 +30,10 @@ def __init__(
self._index = 0
self.grayscale = grayscale
self.frame_rate = frame_rate
self.downsample = downsample
if downsample < 1.0:
width = round(width * downsample)
height = round(height * downsample)
if self.grayscale:
self.output = np.zeros((buffer, width, height))
else:
Expand All @@ -39,6 +44,14 @@ def watch(self) -> None:
while self.watching:
_, frame = self.cam.read()
self.output[:-1] = self.output[1:]
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
if self.downsample < 1.0:
frame = cv2.resize(
frame,
(0, 0),
fx=self.downsample,
fy=self.downsample,
)
if self.grayscale:
frame: np.ndarray = np.sum(
frame * np.array([0.2989, 0.5870, 0.1140]),
Expand All @@ -59,9 +72,16 @@ def __init__(
buffer: int,
frame_rate: int,
grayscale: bool,
downsample: float,
) -> None:
super().__init__()
self._watcher = CamWatcher(cam, buffer, frame_rate, grayscale)
self._watcher = CamWatcher(
cam,
buffer,
frame_rate,
grayscale,
downsample,
)
self._reader_thread = QThread()

@property
Expand Down
37 changes: 3 additions & 34 deletions blitz/layout/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

from .. import __version__, settings
from ..data.image import ImageData
from ..data.web import WebDataLoader
from ..data.live import LiveView
from ..data.web import WebDataLoader
from ..tools import LoadingManager, get_available_ram, log
from .rosee import ROSEEAdapter
from .tof import TOFAdapter
Expand Down Expand Up @@ -232,6 +232,7 @@ def reset_options(self) -> None:
self.ui.spinbox_max_ram.setValue(settings.get("default/max_ram"))
self.ui.spinbox_camera.setValue(0)
self.ui.spinbox_frame_pause.setValue(500)
self.ui.spinbox_downsample.setValue(1.0)
self.ui.button_apply_mask.setChecked(False)
self.ui.checkbox_flipx.setChecked(settings.get("data/flipped_x"))
self.ui.checkbox_flipy.setChecked(settings.get("data/flipped_y"))
Expand Down Expand Up @@ -370,39 +371,6 @@ def image_mask(self) -> None:
with LoadingManager(self, "Masking..."):
self.ui.image_viewer.image_mask(Path(file_path))

# def _normalization_update(self) -> None:
# name = None
# if self.ui.checkbox_norm_subtract.isChecked():
# name = "subtract"
# if self.ui.checkbox_norm_divide.isChecked():
# name = "divide"
# if name is not None:
# bounds = None
# if self.ui.checkbox_norm_range.isChecked():
# bounds = (
# self.ui.spinbox_norm_range_start.value(),
# self.ui.spinbox_norm_range_end.value(),
# )
# window_lag = None
# if self.ui.checkbox_norm_lag.isChecked():
# window_lag = (
# self.ui.spinbox_norm_window.value(),
# self.ui.spinbox_norm_lag.value(),
# )
# with LoadingManager(self, "Calculating...") as lm:
# self.ui.image_viewer.norm(
# operation=name,
# use=self.ui.combobox_norm.currentText(),
# beta=self.ui.spinbox_norm_beta.value() / 100.0,
# gaussian_blur=self.ui.spinbox_norm_blur.value(),
# bounds=bounds,
# background=self.ui.checkbox_norm_bg.isChecked(),
# window_lag=window_lag,
# force_calculation=True,
# )
# log(f"Normalized in {lm.duration:.2f}s")
# self.update_statusbar()

def _normalization(self, name: str) -> None:
if ((not self.ui.checkbox_norm_range.isChecked()
and not self.ui.checkbox_norm_bg.isChecked()
Expand Down Expand Up @@ -621,6 +589,7 @@ def start_live_view(self) -> None:
buffer=self.ui.spinbox_buffer_size.value(),
frame_rate=self.ui.spinbox_frame_pause.value(),
grayscale=self.ui.checkbox_load_grayscale.isChecked(),
downsample=self.ui.spinbox_downsample.value(),
)
if not self._live_view.available:
self._live_view = None
Expand Down
12 changes: 10 additions & 2 deletions blitz/layout/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
from PyQt5.QtWidgets import (QApplication, QCheckBox, QComboBox,
QDoubleSpinBox, QFrame, QGridLayout, QHBoxLayout,
QLabel, QLayout, QLineEdit, QMenu, QMenuBar,
QPushButton, QScrollArea, QSpinBox, QStatusBar,
QStyle, QTabWidget, QVBoxLayout, QWidget, QSizePolicy)
QPushButton, QScrollArea, QSizePolicy, QSpinBox,
QStatusBar, QStyle, QTabWidget, QVBoxLayout,
QWidget)
from pyqtgraph.dockarea import Dock, DockArea

from .. import __version__, resources, settings
Expand Down Expand Up @@ -313,13 +314,20 @@ def setup_option_dock(self) -> None:
self.spinbox_buffer_size = QSpinBox()
self.spinbox_buffer_size.setMinimum(1)
self.spinbox_buffer_size.setMaximum(1000)
label_downsample = QLabel("Downsample")
self.spinbox_downsample = QDoubleSpinBox()
self.spinbox_downsample.setSingleStep(0.01)
self.spinbox_downsample.setMinimum(0.01)
self.spinbox_downsample.setMaximum(1.0)
gridlay_camera = QGridLayout()
gridlay_camera.addWidget(label_camera, 0, 0, 1, 1)
gridlay_camera.addWidget(self.spinbox_camera, 0, 1, 1, 1)
gridlay_camera.addWidget(label_rate, 1, 0, 1, 1)
gridlay_camera.addWidget(self.spinbox_frame_pause, 1, 1, 1, 1)
gridlay_camera.addWidget(label_buffer, 2, 0, 1, 1)
gridlay_camera.addWidget(self.spinbox_buffer_size, 2, 1, 1, 1)
gridlay_camera.addWidget(label_downsample, 3, 0, 1, 1)
gridlay_camera.addWidget(self.spinbox_downsample, 3, 1, 1, 1)
file_layout.addLayout(gridlay_camera)
self.button_watch_start = QPushButton("start")
self.button_watch_stop = QPushButton("stop")
Expand Down
Loading

0 comments on commit 1ae2a69

Please sign in to comment.