WPOCT Software Developer's Kit (SDK)
SDK For using Wasatch Photonics OCT Spectrometers
Image.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <string>
9 #include <vector>
10 #include <iostream>
11 #include <fstream>
12 
13 using namespace std;
14 
15 class __declspec(dllexport) WPImage
16 {
17 public:
18  WPImage ();
19  WPImage (int width, int height);
20  WPImage (int width, int height, float* image);
21  WPImage (int width, int height, const float* image);
22  virtual ~WPImage ();
23 
24  int GetWidth () const { return _width; }
25  int GetHeight () const { return _height; }
26 
27  void GetCharData (unsigned char* data) const;
28  void GetShortData (unsigned short int* data) const;
29 
30  float GetThresholdCoeff () const { return _thresholdCoeff; }
31  void GetCropIndices (int &low, int &high) const { low = _cropHeightLow; high = _cropHeightHigh; }
32 
33  void SetWidth (int value) { _width = value; }
34  void SetHeight (int value) { _height = value; }
35  void SetWidthAndHeight (int width, int height) { _width = width; _height = height; }
36  void SetThresholdCoeff (float value) { _thresholdCoeff = value; }
37  void SetCropIndices (int low, int high) { _cropHeightLow = low; _cropHeightHigh = high; }
38 
39  bool TransposeArray (const float* src, float* dst, int inWidth, int inHeight) const;
40  bool TransposeArray (const unsigned short* src, unsigned short* dst, int inWidth,
41  int inHeight) const;
42  bool TransposeArray (const unsigned char* src, unsigned char* dst, int inWidth,
43  int inHeight) const;
44 
45  static bool ConvertFloatToUnsignedChar (const float* inData, unsigned char* outData,
46  int size);
47  static bool ConvertFloatToUnsignedShort (const float* inData, unsigned short* outData,
48  int size);
49 
50  template <typename T>
51  static bool TransposeAnyArray (T* src, T* dst, int inWidth, int inHeight);
52  template <typename T>
53  static bool TransposeAnyArray (const T* src, T* dst, int inWidth, int inHeight);
54 
55  template <typename T>
56  static bool FlipHorizontal (T* data, int width, int height);
57  template <typename T>
58  static bool FlipVertical (T* data, int width, int height);
59  template <typename T>
60  static bool Rotate (const T* dataIn, T* dataOut, int width, int height,
61  unsigned int cw90Multiplier);
62 
63 private:
64  void Init ();
65 
66  bool SmoothImage (const std::vector<float>& inData, int width, int height,
67  int neighbors, std::vector<float>& outData) const;
68  bool ThresholdImage (const std::vector<float>& inData, std::vector<float>& averagedData,
69  float threshold, std::vector<float>& outData) const;
70  bool TakeLogOfImage (const std::vector<float>& inData, std::vector<float>& outData) const;
71  bool CropImageHeight (const std::vector<float>& inData, std::vector<float>& outData,
72  int topCutoff, int botCutoff) const;
73 
74  float GetMinValue () const;
75  float GetMaxValue () const;
76  float GetMinValue (const std::vector<float>& image) const;
77  float GetMaxValue (const std::vector<float>& image) const;
78 
79  float GetMeanOfMaxes (const std::vector<float>& image, int imageHeight,
80  std::vector<float>& rows) const;
81  float GetMeanOfContainer (const std::vector<float>& container) const;
82 
83  bool ReadFile (string name, vector<float>& outdata);
84  bool WriteFile (string name, std::vector<signed char>& outData);
85 
86  virtual bool ThresholdAndScale (const std::vector<float>& inData,
87  std::vector<float>& outData) const;
88  virtual bool NormalizeFor8Bit (const std::vector<float>& intensityData,
89  std::vector<unsigned char>& outData) const;
90 
91  static float GetMinValue (const float* data, int size);
92  static float GetMaxValue (const float* data, int size);
93 
94  static bool NormalizeFor8Bit (const float* inData, unsigned char* outData, int size);
95  static bool NormalizeFor16Bit (const float* inData, unsigned short int* outData, int size);
96 
97  int _width;
98  int _height;
99 
100  int _cropHeightLow, _cropHeightHigh;
101  int _topCutoffHeight;
102  int _botCutoffHeight;
103 
104  float _thresholdCoeff;
105  const float* _floatData;
106 
107 };
108 
109 // Implementation
110 
112 template <typename T>
113 bool WPImage::FlipHorizontal(T* data, int width, int height)
114 {
115  if (data == nullptr || width <= 0 || height <= 0) return false;
116 
117  bool result = false;
118  T temp = 0;
119 
120  try
121  {
122  for (int y = 0; y < height; y++)
123  {
124  int rowStart = y * width;
125  for (int x = 0; x < width / 2; x++)
126  {
127  temp = data[rowStart + x];
128  data[rowStart + x] = data[rowStart + width - 1 - x];
129  data[rowStart + width - 1 - x] = temp;
130  }
131  }
132  }
133  catch (exception&)
134  {
135  }
136 
137  return result;
138 }
139 
141 template <typename T>
142 bool WPImage::FlipVertical(T* data, int width, int height)
143 {
144  if (data == nullptr || width <= 0 || height <= 0) return false;
145 
146  bool result = false;
147  T temp = 0;
148 
149  try
150  {
151  for (int y = 0; y < height / 2; y++)
152  {
153  int rowStartTop = y * width;
154  int rowStartBot = (height - 1 - y) * width;
155  for (int x = 0; x < width; x++)
156  {
157  temp = data[rowStartTop + x];
158  data[rowStartTop + x] = data[rowStartBot + x];
159  data[rowStartBot + x] = temp;
160  }
161  }
162  }
163  catch (exception&)
164  {
165  }
166  return result;
167 }
168 
169 template <typename T>
170 bool WPImage::Rotate(const T* dataIn, T* dataOut, int width, int height, unsigned int cw90Multiplier)
171 {
172  if (dataIn == nullptr || dataOut == nullptr || width <= 0 || height <= 0) return false;
173 
174  bool result = false;
175  int i = 0;
176  cw90Multiplier %= 4;
177  try
178  {
179  // 90 degrees CW rotation.
180  if (cw90Multiplier == 1)
181  {
182  for (int x = 0; x < width; x++)
183  {
184  for (int y = height - 1; y >= 0; y--)
185  {
186  dataOut[i++] = dataIn[y * width + x];
187  }
188  }
189  }
190  // Else, 270 (-90).
191  else if (cw90Multiplier == 3)
192  {
193  for (int x = 0; x < width; x++)
194  {
195  for (int y = 0; y < height; y++)
196  {
197  dataOut[i++] = dataIn[width - 1 - x + y * width];
198  }
199  }
200  }
201  // Else, 180
202  else if (cw90Multiplier == 2)
203  {
204  for (int y = 0; y < height; y++)
205  {
206  for (int x = 0; x < width; x++)
207  {
208  dataOut[i++] = dataIn[(height - 1 - y) * width + width - 1 - x];
209  }
210  }
211  }
212  // Else, 0.
213  else
214  {
215  for (int x = 0; x < width * height; x++)
216  {
217  dataOut[x] = dataIn[x];
218  }
219  }
220  result = true;
221  }
222  catch (exception&)
223  {
224  }
225 
226  return result;
227 }
228 
229 template <typename T>
230 bool WPImage::TransposeAnyArray(T* src, T* dst, int inWidth, int inHeight)
231 {
232  if (src == nullptr || dst == nullptr || inWidth <= 0 || inHeight <= 0) return false;
233 
234  bool result = false;
235  try
236  {
237  int startLoc = 0;
238  for (int i = 0; i < inWidth; i++)
239  {
240  startLoc = i * inHeight;
241  for (int j = 0; j < inHeight ; j++)
242  {
243  dst[j * inWidth + i] = src[startLoc + j];
244  }
245  }
246  result = true;
247  }
248  catch (exception& )
249  {
250  }
251  return result;
252 }
253 
254 template <typename T>
255 bool WPImage::TransposeAnyArray(const T* src, T* dst, int inWidth, int inHeight)
256 {
257  if (src == nullptr || dst == nullptr || inWidth <= 0 || inHeight <= 0) return false;
258 
259  bool result = false;
260  try
261  {
262  int startLoc = 0;
263  for (int i = 0; i < inWidth; i++)
264  {
265  startLoc = i * inHeight;
266  for (int j = 0; j < inHeight; j++)
267  {
268  dst[j * inWidth + i] = src[startLoc + j];
269  }
270  }
271  result = true;
272  }
273  catch (exception& )
274  {
275  }
276  return result;
277 }