DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Camera.cpp
Go to the documentation of this file.
1 // This file is part of DynExp.
2 
3 #include "stdafx.h"
4 #include "Camera.h"
5 
6 namespace DynExpInstr
7 {
9  {
10  auto Image = ObtainImage(Instance);
11 
12  if (Image.isNull())
13  return;
14 
15  CameraData::ComputeHistogramType ComputeHistogram;
16  Util::ImageRGBHistogramType RGBHistogram;
17  Util::ImageHistogramType IntensityHistogram;
18 
19  {
20  auto InstrData = DynExp::dynamic_InstrumentData_cast<Camera>(Instance.InstrumentDataGetter());
21 
22  ComputeHistogram = InstrData->GetComputeHistogram();
23  } // Unlock InstrData for probably expensive calculation.
24 
25  if (ComputeHistogram != CameraData::ComputeHistogramType::NoHistogram)
26  {
28  IntensityHistogram = Util::ComputeIntensityHistogram(Image);
29  else
30  {
31  RGBHistogram = Util::ComputeRGBHistogram(Image);
32 
35  IntensityHistogram = Util::ConvertRGBToIntensityHistogram(RGBHistogram);
36  }
37  }
38 
39  {
40  auto InstrData = DynExp::dynamic_InstrumentData_cast<Camera>(Instance.InstrumentDataGetter());
41 
42  InstrData->SetImage(std::move(Image));
43 
46  InstrData->SetIntensityHistogram(std::move(IntensityHistogram));
47  if (ComputeHistogram == CameraData::ComputeHistogramType::RGBHistogram ||
49  InstrData->SetRGBHistogram(std::move(RGBHistogram));
50  } // InstrData unlocked here.
51 
53  }
54 
55  QImage CameraData::GetImage() const
56  {
57  if (!IsImageAvailbale())
58  throw Util::EmptyException("There is currently no image.");
59 
60  // Move-constructs new object by stealing from internal data.
61  return std::move(CurrentImage);
62  }
63 
64  QImage CameraData::GetImageCopy(const QRect& RegionOfInterest) const
65  {
66  if (!IsImageAvailbale())
67  throw Util::EmptyException("There is currently no image.");
68 
69  return CurrentImage.copy(RegionOfInterest);
70  }
71 
72  void CameraData::SetImage(QImage&& Other)
73  {
74  CurrentImage = std::move(Other);
75  }
76 
78  {
79  CurrentImage = QImage();
80 
81  IntensityHistogram = {};
82  RGBHistogram = {};
83  }
84 
86  {
87  ImageWidth = 0;
88  ImageHeight = 0;
89 
90  CameraModes.clear();
91 
92  MinExposureTime = TimeType();
93  MaxExposureTime = TimeType();
94  CurrentExposureTime = TimeType();
95  CurrentFPS = 0.f;
96 
97  ComputeHistogram = ComputeHistogramType::NoHistogram;
98  ImageTransformation = {};
99 
100  ClearImage();
101 
102  ResetImpl(dispatch_tag<CameraData>());
103  }
104 
106  {
107  }
108 
110  {
111  }
112 
114  {
115  }
116 
117  void Camera::SetCameraMode(size_t ID, DynExp::TaskBase::CallbackType CallbackFunc) const
118  {
120  }
121 
123  {
125  }
126 
127  void Camera::SetExposureTimeSync(const CameraData::TimeType ExposureTime) const
128  {
129  AsSyncTask(&Camera::SetExposureTime, ExposureTime);
130  }
131 
133  {
134  AsSyncTask(&Camera::StopCapturing);
135  }
136 
138  {
139  ResetImpl(dispatch_tag<Camera>());
140  }
141 }
Defines a meta instrument for an image capturing device.
std::chrono::milliseconds TimeType
Time type describing the camera's times like its exposure time.
Definition: Camera.h:101
QImage GetImageCopy(const QRect &RegionOfInterest=QRect()) const
Copying getter for CurrentImage. This function is more expensive than GetImage().
Definition: Camera.cpp:64
QImage GetImage() const
Moving getter for CurrentImage.
Definition: Camera.cpp:55
void SetImage(QImage &&Other)
Setter for CurrentImage.
Definition: Camera.cpp:72
void ResetImpl(dispatch_tag< InstrumentDataBase >) override final
Definition: Camera.cpp:85
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.
void ClearImage() const
Resets CurrentImage to a default-constructed empty image and IntensityHistogram as well as RGBHistogr...
Definition: Camera.cpp:77
virtual ~CameraParams()=0
Definition: Camera.cpp:105
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....
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 void StopCapturing(DynExp::TaskBase::CallbackType CallbackFunc=nullptr) const =0
Stops image capturing.
virtual void StopCapturingSync() const
Synchronized version of StopCapturing(), which blocks until a stop capturing task issued by an overri...
Definition: Camera.cpp:132
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 SetCameraMode(size_t ID, DynExp::TaskBase::CallbackType CallbackFunc=nullptr) const
Sets the image capturing modes the camera should work in.
Definition: Camera.cpp:117
virtual ~Camera()=0
Definition: Camera.cpp:113
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
const InstrumentBase::InstrumentDataGetterType InstrumentDataGetter
Getter for instrument's data. Refer to InstrumentBase::InstrumentDataGetterType.
Definition: Instrument.h:791
Refer to ParamsBase::dispatch_tag.
Definition: Object.h:2018
std::function< void(const TaskBase &, ExceptionContainer &)> CallbackType
Type of a callback function which is invoked when a task has finished, failed or has been aborted....
Definition: Instrument.h:939
Refer to DynExp::ParamsBase::dispatch_tag.
Definition: Instrument.h:1182
Thrown when a list is expected to contain entries and when a query results in an empty answer or an e...
Definition: Exception.h:224
Thrown when a requested feature is either under development and thus not implemented yet or when a sp...
Definition: Exception.h:299
DynExp's instrument namespace contains the implementation of DynExp instruments which extend DynExp's...
Definition: Instrument.h:1254
ImageHistogramType ConvertRGBToIntensityHistogram(const ImageRGBHistogramType &RGBHistogram)
Computes an intensity (grayscale) histogram from a RGB histogram.
Definition: QtUtil.cpp:204
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
ImageHistogramType ComputeIntensityHistogram(const QImage &Image)
Computes an intensity (grayscale) histogram from a QImage object.
Definition: QtUtil.cpp:171
std::tuple< ImageHistogramType, ImageHistogramType, ImageHistogramType > ImageRGBHistogramType
Alias which represents a RGB histogram as a std::tuple of three ImageHistogramType elements....
Definition: QtUtil.h:250
ImageRGBHistogramType ComputeRGBHistogram(const QImage &Image)
Computes a RGB histogram from a QImage object.
Definition: QtUtil.cpp:186
Accumulates include statements to provide a precompiled header.