DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Camera.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 "Instrument.h"
12 
13 namespace DynExpInstr
14 {
15  class Camera;
16 
20  namespace CameraTasks
21  {
26  {
28 
33  };
34 
39  {
41 
46  };
47 
52  {
54 
59 
68  virtual QImage ObtainImage(DynExp::InstrumentInstance& Instance) = 0;
69  };
70  }
71 
76  {
77  public:
82  {
86  float BrightnessFactor = 0.f;
87 
91  float ContrastFactor = 1.f;
92 
96  bool IsEnabled = false;
97  };
98 
99  using ImageDimensionType = unsigned int;
100  using CameraModesType = std::vector<std::string>;
101  using TimeType = std::chrono::milliseconds;
102 
106  enum class CapturingStateType {
107  Stopped,
110  };
111 
115  enum class ComputeHistogramType {
116  NoHistogram,
118  RGBHistogram,
120  };
121 
122  CameraData() = default;
123  virtual ~CameraData() = default;
124 
125  auto GetImageWidth() const noexcept { return ImageWidth; }
127  auto GetImageHeight() const noexcept { return ImageHeight; }
129 
130  auto GetCameraModes() const { return CameraModes; }
131  void SetCameraModes(CameraModesType CameraModes) { this->CameraModes = CameraModes; }
132 
136  void SetMinExposureTime(TimeType MinExposureTime) { this->MinExposureTime = MinExposureTime; }
137  void SetMaxExposureTime(TimeType MaxExposureTime) { this->MaxExposureTime = MaxExposureTime; }
138  void SetExposureTime(TimeType CurrentExposureTime) { this->CurrentExposureTime = CurrentExposureTime; }
139 
140  auto GetCurrentFPS() const noexcept { return CurrentFPS; }
141  void SetCurrentFPS(float CurrentFPS) noexcept { this->CurrentFPS = CurrentFPS; }
142 
143  auto GetComputeHistogram() const noexcept { return ComputeHistogram; }
145  auto GetIntensityHistogram() const noexcept { return IntensityHistogram; }
147  auto GetRGBHistogram() const noexcept { return RGBHistogram; }
149 
150  const auto& GetImageTransformation() const noexcept { return ImageTransformation; }
151  void SetImageTransformation(const ImageTransformationType& Transformation) const noexcept { ImageTransformation = Transformation; }
152 
159  QImage GetImage() const;
160 
169  QImage GetImageCopy(const QRect& RegionOfInterest = QRect()) const;
170 
175  bool IsImageAvailbale() const noexcept { return !CurrentImage.isNull(); }
176 
181  void SetImage(QImage&& Other);
182 
187  void ClearImage() const;
188 
193  auto GetCapturingState() const noexcept { return GetCapturingStateChild(); }
194 
200  bool IsCapturing() const noexcept { return GetCapturingStateChild() != CapturingStateType::Stopped; }
201 
208 
215 
216  private:
217  void ResetImpl(dispatch_tag<InstrumentDataBase>) override final;
219 
224  virtual CapturingStateType GetCapturingStateChild() const noexcept = 0;
226 
229 
231 
235  float CurrentFPS = 0.f;
236 
243 
250 
257 
264 
270  mutable QImage CurrentImage;
271  };
272 
277  {
278  public:
284 
285  virtual ~CameraParams() = 0;
286 
287  virtual const char* GetParamClassTag() const noexcept override { return "CameraParams"; }
288 
289  private:
292 
293  DummyParam Dummy = { *this };
294  };
295 
300  {
301  public:
304 
305  CameraConfigurator() = default;
306  virtual ~CameraConfigurator() = 0;
307  };
308 
313  {
314  public:
318 
327 
328  constexpr static auto Name() noexcept { return "Camera"; }
329  constexpr static auto Category() noexcept { return "Image Capturing"; }
330 
335  : InstrumentBase(OwnerThreadID, std::move(Params)) {}
336 
337  virtual ~Camera() = 0;
338 
339  virtual std::string GetName() const override { return Name(); }
340  virtual std::string GetCategory() const override { return Category(); }
341 
342  virtual std::chrono::milliseconds GetTaskQueueDelay() const override { return std::chrono::milliseconds(16); /* approx. 63 fps */ }
343 
348 
352  virtual bool CanSetExposureTime() const noexcept { return false; }
353 
358  virtual double GetPixelSizeInMicrons() const noexcept = 0;
360 
367 
374  virtual void SetCameraMode(size_t ID, DynExp::TaskBase::CallbackType CallbackFunc = nullptr) const;
375 
383  virtual void SetExposureTime(const CameraData::TimeType ExposureTime, DynExp::TaskBase::CallbackType CallbackFunc = nullptr) const;
384 
390  virtual void SetExposureTimeSync(const CameraData::TimeType ExposureTime) const;
391 
396  virtual void CaptureSingle(DynExp::TaskBase::CallbackType CallbackFunc = nullptr) const = 0;
397 
402  virtual void StartCapturing(DynExp::TaskBase::CallbackType CallbackFunc = nullptr) const = 0;
403 
408  virtual void StopCapturing(DynExp::TaskBase::CallbackType CallbackFunc = nullptr) const = 0;
409 
414  virtual void StopCapturingSync() const;
416 
417  private:
418  void ResetImpl(dispatch_tag<InstrumentBase>) override final;
419  virtual void ResetImpl(dispatch_tag<Camera>) = 0;
420  };
421 
430  template <typename T>
431  inline T TransformPixel(T Pixel, const CameraData::ImageTransformationType& ImageTransformation)
432  {
433  // Apply user transformations
434  float PixelF = ImageTransformation.ContrastFactor * Pixel + ImageTransformation.BrightnessFactor * std::numeric_limits<T>::max();
435 
436  // Round and apply limits.
437  PixelF = std::min(static_cast<float>(std::numeric_limits<T>::max()), std::round(PixelF));
438  PixelF = std::max(static_cast<float>(std::numeric_limits<T>::lowest()), PixelF);
439 
440  return static_cast<T>(PixelF);
441  }
442 }
Implementation of DynExp instrument objects.
Configurator class for Camera.
Definition: Camera.h:300
Data class for Camera.
Definition: Camera.h:76
CapturingStateType
Type indicating whether the camera is currently capturing images.
Definition: Camera.h:106
@ CapturingSingle
The camera is caturing a single image and will stop afterwards.
@ CapturingContinuously
The camera is capturing one image after the other.
@ Stopped
The camera is not capturing.
void SetIntensityHistogram(Util::ImageHistogramType &&IntensityHistogram) noexcept
Setter for IntensityHistogram. Moves from argument.
Definition: Camera.h:146
std::chrono::milliseconds TimeType
Time type describing the camera's times like its exposure time.
Definition: Camera.h:101
unsigned int ImageDimensionType
Type describing image dimensions such as width and height.
Definition: Camera.h:99
QImage GetImageCopy(const QRect &RegionOfInterest=QRect()) const
Copying getter for CurrentImage. This function is more expensive than GetImage().
Definition: Camera.cpp:64
float BrightnessFactor
Factor to enhance the image brightness (between -1 and 1).
Definition: Camera.h:86
QImage GetImage() const
Moving getter for CurrentImage.
Definition: Camera.cpp:55
auto GetIntensityHistogram() const noexcept
Getter for IntensityHistogram.
Definition: Camera.h:145
auto GetImageWidth() const noexcept
Getter for ImageWidth.
Definition: Camera.h:125
auto GetRGBHistogram() const noexcept
Getter for RGBHistogram.
Definition: Camera.h:147
bool IsEnabled
Determines whether the image transformation is to be applied (enabled).
Definition: Camera.h:96
const auto & GetImageTransformation() const noexcept
Getter for ImageTransformation.
Definition: Camera.h:150
Util::ImageRGBHistogramType RGBHistogram
Color (RGB) histograms belonging to CurrentImage. The histograms are computed if ComputeHistogram is ...
Definition: Camera.h:256
TimeType GetMaxExposureTime() const
Getter for MaxExposureTime.
Definition: Camera.h:134
ImageDimensionType ImageWidth
Width of the images the camera captures.
Definition: Camera.h:227
void SetImage(QImage &&Other)
Setter for CurrentImage.
Definition: Camera.cpp:72
bool IsCapturingSingle() const noexcept
Determines whether the camera is currently capturing a single image.
Definition: Camera.h:207
std::vector< std::string > CameraModesType
List type containing strings of modes the camera can operate in.
Definition: Camera.h:100
auto GetCurrentFPS() const noexcept
Getter for CurrentFPS.
Definition: Camera.h:140
TimeType MinExposureTime
Minimal exposure time the camera supports.
Definition: Camera.h:232
void SetImageTransformation(const ImageTransformationType &Transformation) const noexcept
Setter for ImageTransformation. Adjustable by modules.
Definition: Camera.h:151
void ResetImpl(dispatch_tag< InstrumentDataBase >) override final
Definition: Camera.cpp:85
float CurrentFPS
Current frames per second when the camera is capturing images continuously.
Definition: Camera.h:235
auto GetCapturingState() const noexcept
Returns the camera's current capturing state.
Definition: Camera.h:193
virtual ~CameraData()=default
void SetExposureTime(TimeType CurrentExposureTime)
Setter for CurrentExposureTime.
Definition: Camera.h:138
ImageTransformationType ImageTransformation
Image transformation to be applied to each captured image. Logical const-ness: allow modules to commu...
Definition: Camera.h:263
bool IsImageAvailbale() const noexcept
Determines whether an image is currently available.
Definition: Camera.h:175
void SetMinExposureTime(TimeType MinExposureTime)
Setter for MinExposureTime.
Definition: Camera.h:136
void SetMaxExposureTime(TimeType MaxExposureTime)
Setter for MaxExposureTime.
Definition: Camera.h:137
ComputeHistogramType
Type indicating whether histograms should be computed for newly captured images.
Definition: Camera.h:115
@ IntensityAndRGBHistogram
Combination of IntensityHistogram and RGBHistogram.
@ RGBHistogram
Compute separate histograms for each of the image's color channels (RGB).
@ NoHistogram
Histogram computation is disabled.
@ IntensityHistogram
Compute an intensity histogram by converting the image to grayscale.
virtual CapturingStateType GetCapturingStateChild() const noexcept=0
Returns the camera's current capturing state.
auto GetComputeHistogram() const noexcept
Getter for ComputeHistogram.
Definition: Camera.h:143
void SetImageWidth(ImageDimensionType ImageWidth) noexcept
Setter for ImageWidth.
Definition: Camera.h:126
auto GetImageHeight() const noexcept
Getter for ImageHeight.
Definition: Camera.h:127
CameraModesType CameraModes
Image capturing modes the camera can work in.
Definition: Camera.h:230
TimeType MaxExposureTime
Maximal exposure time the camera supports.
Definition: Camera.h:233
bool IsCapturing() const noexcept
Determines whether the camera is currently capturing an image.
Definition: Camera.h:200
Util::ImageHistogramType IntensityHistogram
Intensity (grayscale) histogram belonging to CurrentImage. The histogram is computed if ComputeHistog...
Definition: Camera.h:249
QImage CurrentImage
Current image captured by the camera. Logical const-ness: allow const member function GetImage() to m...
Definition: Camera.h:270
TimeType CurrentExposureTime
Current exposure time of the camera.
Definition: Camera.h:234
void SetRGBHistogram(Util::ImageRGBHistogramType &&RGBHistogram) noexcept
Setter for RGBHistogram. Moves from argument.
Definition: Camera.h:148
void SetComputeHistogram(ComputeHistogramType ComputeHistogram) const noexcept
Setter for ComputeHistogram. Adjustable by modules.
Definition: Camera.h:144
void ClearImage() const
Resets CurrentImage to a default-constructed empty image and IntensityHistogram as well as RGBHistogr...
Definition: Camera.cpp:77
float ContrastFactor
Factor to enhance the image contrast. Valid interval is (0, Inf).
Definition: Camera.h:91
virtual void ResetImpl(dispatch_tag< CameraData >)
Definition: Camera.h:218
void SetCameraModes(CameraModesType CameraModes)
Setter for CameraModes.
Definition: Camera.h:131
bool IsCapturingContinuously() const noexcept
Determines whether the camera is currently capturing images consecutively.
Definition: Camera.h:214
TimeType GetMinExposureTime() const
Getter for MinExposureTime.
Definition: Camera.h:133
ComputeHistogramType ComputeHistogram
Determines the histogram types to be computed for each captured image. Logical const-ness: allow modu...
Definition: Camera.h:242
void SetImageHeight(ImageDimensionType ImageHeight) noexcept
Setter for ImageHeight.
Definition: Camera.h:128
TimeType GetExposureTime() const
Getter for CurrentExposureTime.
Definition: Camera.h:135
void SetCurrentFPS(float CurrentFPS) noexcept
Setter for CurrentFPS.
Definition: Camera.h:141
auto GetCameraModes() const
Getter for CameraModes.
Definition: Camera.h:130
ImageDimensionType ImageHeight
Height of the images the camera captures.
Definition: Camera.h:228
Type describing an image transformation.
Definition: Camera.h:82
Parameter class for Camera.
Definition: Camera.h:277
CameraParams(DynExp::ItemIDType ID, const DynExp::DynExpCore &Core)
Constructs the parameters for a Camera instance.
Definition: Camera.h:283
virtual ~CameraParams()=0
Definition: Camera.cpp:105
DummyParam Dummy
Dummy parameter which is to be owned once by parameter classes that do not contain any other paramete...
Definition: Camera.h:293
virtual void ConfigureParamsImpl(dispatch_tag< CameraParams >)
Called by DynExp::ParamsBase::ConfigureParams() as a starting point for the tag dispatch mechanism to...
Definition: Camera.h:291
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...
Definition: Camera.h:287
void ConfigureParamsImpl(dispatch_tag< InstrumentParamsBase >) override final
Definition: Camera.h:290
Defines a task for deinitializing an instrument within an instrument inheritance hierarchy....
Definition: Camera.h:39
void ExitFuncImpl(dispatch_tag< ExitTaskBase >, DynExp::InstrumentInstance &Instance) override final
Definition: Camera.h:40
virtual void ExitFuncImpl(dispatch_tag< ExitTask >, DynExp::InstrumentInstance &Instance)
Deinitializes the respective instrument within the instrument inheritance hierarchy....
Definition: Camera.h:45
Defines a task for initializing an instrument within an instrument inheritance hierarchy....
Definition: Camera.h:26
void InitFuncImpl(dispatch_tag< InitTaskBase >, DynExp::InstrumentInstance &Instance) override final
Definition: Camera.h:27
virtual void InitFuncImpl(dispatch_tag< InitTask >, DynExp::InstrumentInstance &Instance)
Initializes the respective instrument within the instrument inheritance hierarchy....
Definition: Camera.h:32
Defines a task for updating an instrument within an instrument inheritance hierarchy....
Definition: Camera.h:52
virtual void UpdateFuncImpl(dispatch_tag< UpdateTask >, DynExp::InstrumentInstance &Instance)
Updates the respective instrument within the instrument inheritance hierarchy. Call UpdateFuncImpl() ...
Definition: Camera.h:58
void UpdateFuncImpl(dispatch_tag< UpdateTaskBase >, DynExp::InstrumentInstance &Instance) override final
Definition: Camera.cpp:8
virtual QImage ObtainImage(DynExp::InstrumentInstance &Instance)=0
Retrieves the current image from the underlying hardware device, applies image transformations (e....
Meta instrument for an image capturing (camera) device.
Definition: Camera.h:313
virtual void SetExposureTime(const CameraData::TimeType ExposureTime, DynExp::TaskBase::CallbackType CallbackFunc=nullptr) const
Sets the camera's exposure time.
Definition: Camera.cpp:122
void ResetImpl(dispatch_tag< InstrumentBase >) override final
Definition: Camera.cpp:137
virtual double GetPixelSizeInMicrons() const noexcept=0
Determines the camera's physical pixel size assuming square pixels.
virtual std::string GetCategory() const override
Returns the category of this Object type.
Definition: Camera.h:340
virtual void StopCapturing(DynExp::TaskBase::CallbackType CallbackFunc=nullptr) const =0
Stops image capturing.
virtual bool CanSetExposureTime() const noexcept
Determines whether the derived camera's exposure time can be set by software.
Definition: Camera.h:352
virtual void StopCapturingSync() const
Synchronized version of StopCapturing(), which blocks until a stop capturing task issued by an overri...
Definition: Camera.cpp:132
virtual std::string GetName() const override
Returns the name of this Object type.
Definition: Camera.h:339
virtual void StartCapturing(DynExp::TaskBase::CallbackType CallbackFunc=nullptr) const =0
Makes the camera capture images continuously.
virtual void SetExposureTimeSync(const CameraData::TimeType ExposureTime) const
Synchronized version of SetExposureTime(), which blocks until a set exposure time task issued by an o...
Definition: Camera.cpp:127
virtual void CaptureSingle(DynExp::TaskBase::CallbackType CallbackFunc=nullptr) const =0
Makes the camera capture a single image.
virtual std::chrono::milliseconds GetTaskQueueDelay() const override
Specifies in which time intervals the instrument's task queue runs to handle pending tasks.
Definition: Camera.h:342
virtual void SetCameraMode(size_t ID, DynExp::TaskBase::CallbackType CallbackFunc=nullptr) const
Sets the image capturing modes the camera should work in.
Definition: Camera.cpp:117
constexpr static auto Category() noexcept
Every derived class has to redefine this function.
Definition: Camera.h:329
virtual ~Camera()=0
Definition: Camera.cpp:113
Camera(const std::thread::id OwnerThreadID, DynExp::ParamsBasePtrType &&Params)
Constructs an instrument instance.
Definition: Camera.h:334
constexpr static auto Name() noexcept
Every derived class has to redefine this function.
Definition: Camera.h:328
DynExp's core class acts as the interface between the user interface and DynExp's internal data like ...
Definition: DynExpCore.h:127
Defines a task for deinitializing an instrument within an instrument inheritance hierarchy....
Definition: Instrument.h:1144
Refer to DynExp::ParamsBase::dispatch_tag.
Definition: Instrument.h:1151
Defines a task for initializing an instrument within an instrument inheritance hierarchy....
Definition: Instrument.h:1113
Refer to DynExp::ParamsBase::dispatch_tag.
Definition: Instrument.h:1120
Base class for instruments. Instruments comprise virtual devices (meta instruments) and physial devic...
Definition: Instrument.h:451
InstrumentBase(const std::thread::id OwnerThreadID, ParamsBasePtrType &&Params)
Constructs an instrument instance.
Definition: Instrument.cpp:210
Configurator class for InstrumentBase.
Definition: Instrument.h:435
Data structure to contain data which is synchronized in between different threads....
Definition: Instrument.h:135
Refer to ParamsBase::dispatch_tag.
Definition: Instrument.h:146
Defines data for a thread belonging to a InstrumentBase instance. Refer to RunnableInstance.
Definition: Instrument.h:772
Parameter class for InstrumentBase.
Definition: Instrument.h:401
InstrumentParamsBase(ItemIDType ID, const DynExpCore &Core)
Constructs the parameters for a InstrumentBase instance.
Definition: Instrument.h:412
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
Refer to ParamsBase::dispatch_tag.
Definition: Object.h:2018
Dummy parameter which is to be owned once by parameter classes that do not contain any other paramete...
Definition: Object.h:522
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
Defines a task for updating an instrument within an instrument inheritance hierarchy....
Definition: Instrument.h:1175
Refer to DynExp::ParamsBase::dispatch_tag.
Definition: Instrument.h:1182
DynExp's instrument namespace contains the implementation of DynExp instruments which extend DynExp's...
Definition: Instrument.h:1254
T TransformPixel(T Pixel, const CameraData::ImageTransformationType &ImageTransformation)
Applies an image transformation to a single pixel.
Definition: Camera.h:431
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.
DynExp's Util namespace contains commonly used functions and templates as well as extensions to Qt an...
Definition: circularbuf.cpp:7
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.