DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Loading...
Searching...
No Matches
ImageViewer.h
Go to the documentation of this file.
1// This file is part of DynExp.
2
8#pragma once
9
10#include "stdafx.h"
11#include "DynExpCore.h"
12#include "../../MetaInstruments/Camera.h"
13#include "../../MetaInstruments/AnalogOut.h"
14#include "../../Instruments/InterModuleCommunicator.h"
15
16#include "ImageViewerEvents.h"
17
18#include <QWidget>
19#include "ui_ImageViewer.h"
20
22{
23 class ImageViewer;
24
34
36
38 {
39 Q_OBJECT
40
41 public:
43 ~ImageViewerWidget() = default;
44
45 bool AllowResize() const noexcept override final { return true; }
46
47 void SetImage(const QImage& NewImage) noexcept;
48 void SetImageViewEnabled(bool Enable);
49 void SetIntensityHistogram(Util::ImageHistogramType&& NewIntensityHistogram) noexcept;
50 void SetRGBHistogram(Util::ImageRGBHistogramType&& NewRGBHistogram) noexcept;
51 auto GetComputeHistogram() const noexcept;
52 void UpdateScene();
53 auto GetSaveImageFilename() const { return SaveImageFilename; }
55
56 Ui::ImageViewer ui;
57
58 private:
59 bool eventFilter(QObject* obj, QEvent* event) override;
60 virtual void resizeEvent(QResizeEvent* event) override;
61
62 void UpdateHistogram();
63
70
75 QBarSeries* HistogramBarSeries;
77 QValueAxis* HistogramXAxis;
78 QValueAxis* HistogramYAxis;
79
81 QPixmap Pixmap;
82 QGraphicsPixmapItem* GraphicsPixmapItem;
83 QGraphicsScene* GraphicsScene;
84
87
89
90 private slots:
91 void OnHistogramContextMenuRequested(const QPoint& Position);
92 void OnSaveImageClicked();
93 void OnZoomResetClicked();
94 void OnZoomInClicked();
95 void OnZoomOutClicked();
96 void OnZoomFitClicked(bool Checked);
97 void OnImageMouseMove(QMouseEvent* Event);
98 };
99
138
140 {
141 public:
143 virtual ~ImageViewerParams() = default;
144
145 virtual const char* GetParamClassTag() const noexcept override { return "ImageViewerParams"; }
146
148 "Camera", "Camera", "Underlying camera to be used as an image source", DynExpUI::Icons::Instrument };
150 "Focus", "Focusing voltage (AO)", "Analog output (voltage) to adjust the focus", DynExpUI::Icons::Instrument, true };
151 Param<ParamsConfigDialog::NumberType> AutofocusNumSteps = { *this, "AutofocusNumSteps", "Autofocus step count",
152 "Specifies how fine the focus is scanned during the autofocus algorithm. Higher values lead to higher precision and longer execution times.",
153 false, 10, 3, 10000, 10, 0};
155 "Focus change time (ms)", "Time it takes to change the focus after applying a new focus voltage.",
156 false, 500, 0, 10000, 10, 0 };
158 "InterModuleCommunicator", "Inter-module communicator", "Inter-module communicator to control this module with", DynExpUI::Icons::Instrument, true };
159
160 private:
162 };
163
165 {
166 public:
169
171 virtual ~ImageViewerConfigurator() = default;
172
173 private:
174 virtual DynExp::ParamsBasePtrType MakeParams(DynExp::ItemIDType ID, const DynExp::DynExpCore& Core) const override final { return DynExp::MakeParams<ImageViewerConfigurator>(ID, Core); }
175 };
176
178 {
179 public:
183
184 constexpr static auto Name() noexcept { return "Image Viewer"; }
185 constexpr static auto Category() noexcept { return "Image Capturing"; }
186
188 virtual ~ImageViewer();
189
190 virtual std::string GetName() const override { return Name(); }
191 virtual std::string GetCategory() const override { return Category(); }
192
193 // approx. 63 fps
194 std::chrono::milliseconds GetMainLoopDelay() const override final { return std::chrono::milliseconds(16); }
195
196 // Events which the UI thread might enqueue.
197 void OnSaveImage(DynExp::ModuleInstance* Instance, QString Filename) const;
198
199 private:
201
202 void ResetImpl(dispatch_tag<QModuleBase>) override final;
203
204 std::unique_ptr<DynExp::QModuleWidget> MakeUIWidget() override final;
205 void UpdateUIChild(const ModuleBase::ModuleDataGetterType& ModuleDataGetter) override final;
206
207 // Helper functions
208 bool IsReadyState() const;
209 bool IsAutofocusingState() const;
210 void FinishAutofocus(Util::SynchronizedPointer<ModuleDataType>& ModuleData, const FinishedAutofocusEvent& Event) const;
211
212 // Function and types for autofocusing
213 // ->
215 {
219 std::chrono::milliseconds WaitTimeBeforeCapture{};
220
221 constexpr auto GetVoltageDiff() const noexcept { return MaxVoltage - MinVoltage; }
222 constexpr auto GetVoltageIncrement(bool Fine = false) const noexcept { return GetVoltageDiff() / NumSteps / (Fine ? NumSteps : 1); }
223 };
224
226 {
227 bool Success = false;
228 double FocusVoltage = std::numeric_limits<double>::quiet_NaN();
229 };
230
232 {
233 double Voltage{};
234 double Result{};
235 };
236 // <-
237
238 // Events, run in module thread
239 void OnInit(DynExp::ModuleInstance* Instance) const override final;
240 void OnExit(DynExp::ModuleInstance* Instance) const override final;
241 void OnCameraModeChanged(DynExp::ModuleInstance* Instance, int Index) const;
242 void OnExposureTimeChanged(DynExp::ModuleInstance* Instance, int Value) const;
243 void OnCaptureSingle(DynExp::ModuleInstance* Instance, bool) const;
244 void OnCaptureContinuously(DynExp::ModuleInstance* Instance, bool Checked) const;
245 void OnPauseImageCapturing(DynExp::ModuleInstance* Instance, bool ResetImageTransformation = false) const;
247 void OnAutofocusClicked(DynExp::ModuleInstance* Instance, bool Checked) const;
248 void OnAutofocus(DynExp::ModuleInstance* Instance, bool ResetImageTransformation = false) const;
249
250 // State functions for state machine
258
259 // States for state machine
263 &ImageViewer::AutofocusInitStateFunc, "Autofocusing...");
271 &ImageViewer::AutofocusStepStateFunc, "Autofocusing...");
273 &ImageViewer::AutofocusFinishedStateFunc, "Autofocusing...");
274
275 // Logical const-ness: allow events to set the state machine's current state.
277
278 // Variables for autofocusing
282 std::vector<AutofocusSampleType> AutofocusSamples;
283 std::vector<AutofocusSampleType>::iterator AutofocusCurrentSample;
284 std::chrono::system_clock::time_point AutofocusWaitingEndTimePoint;
285
286 const std::shared_ptr<std::atomic<bool>> PauseUpdatingUI;
288 };
289}
Defines DynExp's core module as an interface between the UI and DynExp objects.
Events for inter-module communication handled by the DynExpModule::ImageViewer::ImageViewer module.
CapturingStateType
Type indicating whether the camera is currently capturing images.
Definition Camera.h:106
@ Stopped
The camera is not capturing.
std::chrono::milliseconds TimeType
Time type describing the camera's times like its exposure time.
Definition Camera.h:101
std::vector< std::string > CameraModesType
List type containing strings of modes the camera can operate in.
Definition Camera.h:100
ComputeHistogramType
Type indicating whether histograms should be computed for newly captured images.
Definition Camera.h:115
@ NoHistogram
Histogram computation is disabled.
SampleT SampleType
Alias for SampleT.
virtual DynExp::ParamsBasePtrType MakeParams(DynExp::ItemIDType ID, const DynExp::DynExpCore &Core) const override final
Override to make derived classes call DynExp::MakeParams with the correct configurator type derived f...
Util::ImageRGBHistogramType RGBHistogram
void ResetImpl(dispatch_tag< QModuleDataBase >) override final
DynExp::LinkedObjectWrapperContainer< DynExpInstr::InterModuleCommunicator > Communicator
double CalcBrennerGradientFromImage() const
Calculates the Brenner gradient of CurrentImage. Refer to J. F. Brenner et al. J. Histochem....
DynExpInstr::CameraData::TimeType TimeType
DynExpInstr::CameraData::CameraModesType CameraModes
DynExpInstr::CameraData::ComputeHistogramType ComputeHistogram
DynExpInstr::CameraData::CapturingStateType CapturingState
DynExp::LinkedObjectWrapperContainer< DynExpInstr::Camera > Camera
Util::ImageHistogramType IntensityHistogram
DynExp::LinkedObjectWrapperContainer< DynExpInstr::AnalogOut > Focus
virtual void ResetImpl(dispatch_tag< ImageViewerData >)
Param< ParamsConfigDialog::NumberType > AutofocusNumSteps
Param< DynExp::ObjectLink< DynExpInstr::Camera > > Camera
Param< DynExp::ObjectLink< DynExpInstr::InterModuleCommunicator > > Communicator
virtual const char * GetParamClassTag() const noexcept override
This function is intended to be overridden once in each derived class returning the name of the respe...
Param< DynExp::ObjectLink< DynExpInstr::AnalogOut > > Focus
void ConfigureParamsImpl(dispatch_tag< QModuleParamsBase >) override final
Param< ParamsConfigDialog::NumberType > AutofocusFocusChangeTime
ImageViewerParams(DynExp::ItemIDType ID, const DynExp::DynExpCore &Core)
Util::ImageRGBHistogramType RGBHistogram
Definition ImageViewer.h:86
void OnHistogramContextMenuRequested(const QPoint &Position)
void SetImage(const QImage &NewImage) noexcept
virtual void resizeEvent(QResizeEvent *event) override
Util::MarkerGraphicsView * GraphicsView
Definition ImageViewer.h:80
bool eventFilter(QObject *obj, QEvent *event) override
bool AllowResize() const noexcept override final
Indicates the resizing behavior of the user interface window. Override to adjust.
Definition ImageViewer.h:45
void SetIntensityHistogram(Util::ImageHistogramType &&NewIntensityHistogram) noexcept
void SetRGBHistogram(Util::ImageRGBHistogramType &&NewRGBHistogram) noexcept
Util::ImageHistogramType IntensityHistogram
Definition ImageViewer.h:85
static constexpr auto AutofocusGotoSampleState
StateType AutofocusStepStateFunc(DynExp::ModuleInstance &Instance)
StateType AutofocusInitStateFunc(DynExp::ModuleInstance &Instance)
void FinishAutofocus(Util::SynchronizedPointer< ModuleDataType > &ModuleData, const FinishedAutofocusEvent &Event) const
static constexpr auto AutofocusFinishedState
void OnResumeImageCapturing(DynExp::ModuleInstance *Instance) const
void OnAutofocus(DynExp::ModuleInstance *Instance, bool ResetImageTransformation=false) const
StateType AutofocusFinishedStateFunc(DynExp::ModuleInstance &Instance)
void OnCaptureSingle(DynExp::ModuleInstance *Instance, bool) const
std::unique_ptr< DynExp::QModuleWidget > MakeUIWidget() override final
Used by InitUI() as a factory function for the module's user interface widget. Create the widget here...
void OnInit(DynExp::ModuleInstance *Instance) const override final
This event is triggered right before the module thread starts. Override it to lock instruments this m...
Util::DynExpErrorCodes::DynExpErrorCodes ModuleMainLoop(DynExp::ModuleInstance &Instance) override final
Module main loop. The function is executed periodically by the module thread. Also refer to GetMainLo...
void OnCaptureContinuously(DynExp::ModuleInstance *Instance, bool Checked) const
virtual std::string GetCategory() const override
Returns the category of this Object type.
static constexpr auto AutofocusInitState
virtual std::string GetName() const override
Returns the name of this Object type.
StateType ReadyStateFunc(DynExp::ModuleInstance &Instance)
StateType AutofocusGotoSampleStateFunc(DynExp::ModuleInstance &Instance)
void OnExposureTimeChanged(DynExp::ModuleInstance *Instance, int Value) const
static constexpr auto AutofocusStepState
void OnExit(DynExp::ModuleInstance *Instance) const override final
This event is triggered right before the module thread terminates (not due to an exception,...
StateType AutofocusWaitForImageStateFunc(DynExp::ModuleInstance &Instance)
std::vector< AutofocusSampleType >::iterator AutofocusCurrentSample
static constexpr auto AutofocusWaitForImageState
std::vector< AutofocusSampleType > AutofocusSamples
StateType AutofocusWaitBeforeCaptureStateFunc(DynExp::ModuleInstance &Instance)
std::chrono::milliseconds GetMainLoopDelay() const override final
Specifies in which time intervals the module's event queue runs to handle pending events.
void OnAutofocusClicked(DynExp::ModuleInstance *Instance, bool Checked) const
void UpdateUIChild(const ModuleBase::ModuleDataGetterType &ModuleDataGetter) override final
const std::shared_ptr< std::atomic< bool > > PauseUpdatingUI
Util::StateMachine< StateMachineStateType > StateMachine
static constexpr auto AutofocusWaitBeforeCaptureState
void ResetImpl(dispatch_tag< QModuleBase >) override final
void OnCameraModeChanged(DynExp::ModuleInstance *Instance, int Index) const
void OnSaveImage(DynExp::ModuleInstance *Instance, QString Filename) const
std::chrono::system_clock::time_point AutofocusWaitingEndTimePoint
static constexpr auto Name() noexcept
static constexpr auto Category() noexcept
void OnPauseImageCapturing(DynExp::ModuleInstance *Instance, bool ResetImageTransformation=false) const
DynExp's core class acts as the interface between the user interface and DynExp's internal data like ...
Definition DynExpCore.h:127
This class holds a pointer (LinkedObjectWrapperPointer) to a LinkedObjectWrapper. Intances of this cl...
Definition Object.h:3160
Util::CallableMemberWrapper< ModuleBase, ModuleDataTypeSyncPtrType(ModuleBase::*)(const std::chrono::milliseconds)> ModuleDataGetterType
Invoking an instance of this alias is supposed to call ModuleBase::GetModuleData() of the instance th...
Definition Module.h:587
const std::unique_ptr< ModuleDataType > ModuleData
Module data belonging to this ModuleBase instance.
Definition Module.h:743
ModuleBase(const std::thread::id OwnerThreadID, ParamsBasePtrType &&Params)
Constructs a ModuleBase instance.
Definition Module.cpp:189
Refer to ParamsBase::dispatch_tag.
Definition Module.h:189
Defines data for a thread belonging to a ModuleBase instance. Refer to RunnableInstance.
Definition Module.h:793
const std::thread::id OwnerThreadID
Thread id of the thread which has constructed (and owns) this Object instance.
Definition Object.h:2302
const ParamsBasePtrType Params
Pointer to the parameter class instance belonging to this Object instance.
Definition Object.h:2303
const auto & GetCore() const noexcept
Returns a reference to DynExp's core.
Definition Object.h:1677
const ItemIDType ID
ID of the Object this parameter class instance belongs to.
Definition Object.h:1779
const DynExpCore & Core
Reference to DynExp's core.
Definition Object.h:1780
Tag for function dispatching mechanism within this class used when derived classes are not intended t...
Definition Object.h:349
Base class for modules with a Qt-based user interface. Derive from this class to implement modules wi...
Definition Module.h:1344
Configurator class for QModuleBase.
Definition Module.h:1330
Data class for QModuleBase.
Definition Module.h:1220
Parameter class for QModuleBase.
Definition Module.h:1306
QModuleParamsBase(ItemIDType ID, const DynExpCore &Core)
Constructs the parameters for a QModuleBase instance.
Definition Module.h:1312
Window class for Qt-based user interfaces belonging to DynExp modules. User interface Qt window class...
Definition Module.h:1079
QModuleBase & Owner
Module owning this user interface window (reference, because it should never change nor be nullptr).
Definition Module.h:1162
QModuleWidget(QModuleBase &Owner, QWidget *Parent=nullptr)
Constructs a QModuleWidget instance.
Definition Module.cpp:417
double NumberType
Number type used for numeric parameters (DynExp::ParamsBase::Param)
Implements a QGraphicsView the user can interact with to insert graphical markers....
Definition QtUtil.h:379
State machine state as used by class StateMachine. A state mainly wraps a state function of the membe...
Definition Util.h:1305
This class models a state machine. It keeps track of the current state and allows to invoke its assoc...
Definition Util.h:1435
constexpr auto Instrument
DynExp's main namespace contains the implementation of DynExp including classes to manage resources (...
std::unique_ptr< ParamsBase > ParamsBasePtrType
Alias for a pointer to the parameter system base class ParamsBase.
Definition Object.h:1807
size_t ItemIDType
ID type of objects/items managed by DynExp.
DynExpErrorCodes
DynExp's error codes
Definition Exception.h:22
DynExp's Util namespace contains commonly used functions and templates as well as extensions to Qt an...
std::array< unsigned long long, 256 > ImageHistogramType
Alias which represents a histogram as a std::array with 256 numeric bins. The lowest (highest) index ...
Definition QtUtil.h:244
std::tuple< ImageHistogramType, ImageHistogramType, ImageHistogramType > ImageRGBHistogramType
Alias which represents a RGB histogram as a std::tuple of three ImageHistogramType elements....
Definition QtUtil.h:250
Accumulates include statements to provide a precompiled header.
constexpr auto GetVoltageIncrement(bool Fine=false) const noexcept