QTrk
lv_cputrack_api.cpp
Go to the documentation of this file.
1 /*
2 Labview API for CPU tracker
3 */
4 #include "std_incl.h"
5 #include <Windows.h>
6 #undef min
7 #undef max
8 
9 #include "random_distr.h"
10 #include "labview.h"
11 #include "cpu_tracker.h"
12 
18 {
19  try {
20  Sleep(300);
21  return new CPUTracker(w,h,xcorw);
22  }
23  catch(const std::exception& e)
24  {
25  dbgout("Exception: " + std::string(e.what()) + "\n");
26  return 0;
27  }
28 }
29 
31 {
32  try {
33  delete tracker;
34  }
35  catch(const std::exception& e)
36  {
37  dbgout("Exception: " + std::string(e.what()) + "\n");
38  }
39 }
40 
41 CDLL_EXPORT void DLL_CALLCONV compute_com(CPUTracker* tracker, float* out)
42 {
43  vector2f com = tracker->ComputeMeanAndCOM();
44  out[0] = com.x;
45  out[1] = com.y;
46 }
47 
48 CDLL_EXPORT int DLL_CALLCONV compute_xcor(CPUTracker* tracker, vector2f* position, int iterations, int profileWidth)
49 {
50  bool boundaryHit;
51  *position = tracker->ComputeXCorInterpolated(*position, iterations, profileWidth, boundaryHit);
52 
53  return boundaryHit ? 1 : 0;
54 }
55 
56 CDLL_EXPORT int DLL_CALLCONV compute_qi(CPUTracker* tracker, vector2f* position, int iterations, int radialSteps, int angularStepsPerQ, float minRadius, float maxRadius, LVArray<float>** radialweights)
57 {
58  bool boundaryHit;
59 
60  float* rw = 0;
61  if (radialweights && (*radialweights)->dimSize == radialSteps)
62  rw = (*radialweights)->elem;
63 
64  *position = tracker->ComputeQI(*position, iterations, radialSteps, angularStepsPerQ, 1, minRadius,maxRadius, boundaryHit, rw);
65  return boundaryHit ? 1 : 0;
66 }
67 
69 {
70  LVArray2D<uchar>* data = *pData;
71  if (data->dimSizes[0] != tracker->GetWidth() || data->dimSizes[1] != tracker->GetHeight()) {
72  ArgumentErrorMsg(error, "Given image has invalid dimensions");
73  return;
74  }
75  tracker->SetImage8Bit( data->elem, tracker->GetWidth() );
76 }
77 
79 {
80  LVArray2D<uchar>* data = *pData;
81  if (data->dimSizes[0] != tracker->GetWidth() || data->dimSizes[1] != tracker->GetHeight()) {
82  ArgumentErrorMsg(error, "Given image has invalid dimensions");
83  return;
84  }
85  tracker->SetImage8Bit( data->elem, tracker->GetWidth() );
86 }
87 
89 {
90  LVArray2D<ushort>* data = *pData;
91  if (data->dimSizes[0] != tracker->GetWidth() || data->dimSizes[1] != tracker->GetHeight()) {
92  ArgumentErrorMsg(error, "Given image has invalid dimensions");
93  return;
94  }
95  tracker->SetImage16Bit( data->elem, tracker->GetWidth()*sizeof(ushort) );
96 }
97 
99 {
100  LVArray2D<float>* data = *pData;
101  if (data->dimSizes[0] != tracker->GetWidth() || data->dimSizes[1] != tracker->GetHeight()) {
102  ArgumentErrorMsg(error, "Given image has invalid dimensions");
103  return;
104  }
105  tracker->SetImageFloat( data->elem );
106 }
107 
108 CDLL_EXPORT float DLL_CALLCONV compute_z(CPUTracker* tracker, float* center, int angularSteps, int zlut_index, uint *error, LVArray<float>** profile, int* bestIndex, LVArray<float>** errorCurve)
109 {
110  bool boundaryHit=false;
111  if (profile)
112  ResizeLVArray(profile, tracker->zlut_res);
113 
114  if (errorCurve) {
115  ResizeLVArray(errorCurve, tracker->zlut_planes);
116  }
117 
118  int maxPos;
119  float* prof = profile ? (*profile)->elem : ALLOCA_ARRAY(float, tracker->zlut_res);
120  tracker->ComputeRadialProfile(prof,tracker->zlut_res,angularSteps, tracker->zlut_minradius, tracker->zlut_maxradius,*(vector2f*) center, false, &boundaryHit, true);
121  float z= tracker->LUTProfileCompare(prof, zlut_index, errorCurve ? (*errorCurve)->elem : 0, CPUTracker::LUTProfMaxQuadraticFit, 0, &maxPos);
122 
123  if (bestIndex) *bestIndex=maxPos;
124 
125  if (error)
126  *error = boundaryHit?1:0;
127  return z;
128 }
129 
131 {
132  float* src = tracker->GetDebugImage();
133  if (src) {
134  ResizeLVArray2D(pdbgImg, tracker->GetHeight(), tracker->GetWidth());
135  LVArray2D<float>* dst = *pdbgImg;
136 
137  // dbgout(SPrintf("copying %d elements to Labview array\n", len));
138  for (int i=0;i<dst->numElem();i++)
139  dst->elem[i] = src[i];
140  }
141 }
142 
143 CDLL_EXPORT void DLL_CALLCONV compute_crp(CPUTracker* tracker, LVArray<float>** result, int radialSteps, float *radii, float* center, uint* boundaryHit, LVArray2D<float>** crpmap)
144 {
145 }
146 
147 CDLL_EXPORT float DLL_CALLCONV compute_asymmetry(CPUTracker* tracker, LVArray<float>** result, int radialSteps, float *radii, float* center, uint* boundaryHit)
148 {
149  LVArray<float>* dst = *result;
150  bool bhit = false;
151  float asym = tracker->ComputeAsymmetry(*(vector2f*)center, radialSteps, dst->dimSize, radii[0], radii[1], dst->elem);
152  if (boundaryHit) *boundaryHit = bhit ? 1 : 0;
153  return asym;
154 }
155 
156 CDLL_EXPORT void DLL_CALLCONV compute_radial_profile(CPUTracker* tracker, LVArray<float>** result, int angularSteps, float *radii, float* center, uint* boundaryHit)
157 {
158  LVArray<float>* dst = *result;
159  bool bhit = false;
160  tracker->ComputeRadialProfile(&dst->elem[0], dst->dimSize, angularSteps, radii[0], radii[1], *(vector2f*)center, false, &bhit);
161 
162  if (boundaryHit) *boundaryHit = bhit ? 1 : 0;
163 }
164 
165 CDLL_EXPORT void DLL_CALLCONV set_ZLUT(CPUTracker* tracker, LVArray3D<float>** pZlut, float *radii, int angular_steps, bool useCorrelation, LVArray<float>** radialweights, bool normalize)
166 {
167  LVArray3D<float>* zlut = *pZlut;
168 
169  int numLUTs = zlut->dimSizes[0];
170  int planes = zlut->dimSizes[1];
171  int res = zlut->dimSizes[2];
172 
173  if (normalize) {
174  NormalizeZLUT( zlut->elem , numLUTs, planes, res);
175  }
176 
177  tracker->SetRadialZLUT(zlut->elem, planes, res, numLUTs, radii[0], radii[1], true, useCorrelation);
178  if (radialweights)
179  tracker->SetRadialWeights( ((*radialweights)->dimSize>0) ? (*radialweights)->elem : 0);
180 }
181 
182 CDLL_EXPORT void DLL_CALLCONV get_ZLUT(CPUTracker* tracker, int zlutIndex, LVArray2D<float>** dst)
183 {
184  float* zlut = tracker->GetRadialZLUT(zlutIndex);
185  ResizeLVArray2D(dst, tracker->zlut_planes, tracker->zlut_res);
186  std::copy(zlut, zlut+(tracker->zlut_planes*tracker->zlut_res), (*dst)->elem);
187 }
188 
189 CDLL_EXPORT void DLL_CALLCONV generate_test_image(LVArray2D<float> **img, float xp, float yp, float size, float photoncount)
190 {
191  try {
192  ImageData data((*img)->elem, (*img)->dimSizes[1],(*img)->dimSizes[0]);
193  GenerateTestImage(data, xp, yp, size, photoncount);
194  }
195  catch(const std::exception& e)
196  {
197  dbgout("Exception: " + std::string(e.what()) + "\n");
198  }
199 }
200 
202  float *LUTradii, vector3f* position, float pixel_max, int useSplineInterp, int samplesPerPixel)
203 {
204  ImageData img((*image)->elem, (*image)->dimSizes[1], (*image)->dimSizes[0]);
205  ImageData zlut((*lut)->elem, (*lut)->dimSizes[1], (*lut)->dimSizes[0]);
206 
207  GenerateImageFromLUT(&img, &zlut, LUTradii[0], LUTradii[1], *position, useSplineInterp!=0, samplesPerPixel);
208  if (pixel_max>0) {
209  img.normalize();
210  ApplyPoissonNoise(img, pixel_max);
211  }
212 }
213 
T elem[1]
Definition: labview.h:59
void GenerateTestImage(ImageData &img, float xp, float yp, float size, float SNratio)
Definition: utils.cpp:162
int32_t dimSizes[3]
Definition: labview.h:58
void ComputeRadialProfile(float *dst, int radialSteps, int angularSteps, float minradius, float maxradius, vector2f center, bool crp, bool *boundaryHit=0, bool normalize=true)
Wrapper to compute a radial profile.
CDLL_EXPORT CPUTracker *DLL_CALLCONV create_tracker(uint w, uint h, uint xcorw)
float zlut_maxradius
Maximum radius in pixels of the ZLUT profile sampling circle.
Definition: cpu_tracker.h:67
void dbgout(const std::string &s)
Definition: utils.cpp:143
CDLL_EXPORT void DLL_CALLCONV set_image_u8(CPUTracker *tracker, LVArray2D< uchar > **pData, ErrorCluster *error)
int32_t dimSize
Definition: labview.h:34
int32_t dimSizes[2]
Definition: labview.h:42
CDLL_EXPORT float DLL_CALLCONV compute_asymmetry(CPUTracker *tracker, LVArray< float > **result, int radialSteps, float *radii, float *center, uint *boundaryHit)
float zlut_minradius
Minimum radius in pixels to start sampling for a ZLUT profile.
Definition: cpu_tracker.h:66
int zlut_planes
Number of planes per ZLUT.
Definition: cpu_tracker.h:63
CDLL_EXPORT int DLL_CALLCONV compute_qi(CPUTracker *tracker, vector2f *position, int iterations, int radialSteps, int angularStepsPerQ, float minRadius, float maxRadius, LVArray< float > **radialweights)
Class with all CPU algorithm implementations.
Definition: cpu_tracker.h:38
int zlut_res
ZLUT resolution = number of radial steps in a plane.
Definition: cpu_tracker.h:64
unsigned int uint
Definition: std_incl.h:127
CDLL_EXPORT void DLL_CALLCONV compute_com(CPUTracker *tracker, float *out)
CDLL_EXPORT int DLL_CALLCONV compute_xcor(CPUTracker *tracker, vector2f *position, int iterations, int profileWidth)
void GenerateImageFromLUT(ImageData *image, ImageData *zlut, float minradius, float maxradius, vector3f pos, bool splineInterp, int oversampleSubdiv)
Definition: utils.cpp:354
void ResizeLVArray(LVArrayND< T, N > **&d, int *dims)
Definition: labview.h:118
void normalize(TPixel *d, uint w, uint h)
Definition: utils.h:27
unsigned short ushort
Definition: std_incl.h:128
float LUTProfileCompare(float *profile, int zlutIndex, float *cmpProf, LUTProfileMaxComputeMode maxPosMethod, float *fitcurve=0, int *maxPos=0, int frameNum=0)
Compare a profile to a LUT and calculate the optimum Z value for it.
void SetImage8Bit(uchar *srcImage, uint srcpitch)
Set an image with 8 bit type.
Definition: cpu_tracker.h:254
T elem[1]
Definition: labview.h:35
void normalize()
Definition: utils.h:101
LabVIEW Array template.
Definition: labview.h:33
CDLL_EXPORT void DLL_CALLCONV get_ZLUT(CPUTracker *tracker, int zlutIndex, LVArray2D< float > **dst)
float * GetRadialZLUT(int index)
Get the start of the ZLUT of a specific bead.
Definition: cpu_tracker.h:90
vector2f ComputeMeanAndCOM(float bgcorrection=0.0f)
Calculate the center of mass of the image.
void ArgumentErrorMsg(ErrorCluster *e, const std::string &msg)
#define CDLL_EXPORT
Definition: dllmacros.h:9
CDLL_EXPORT void DLL_CALLCONV set_ZLUT(CPUTracker *tracker, LVArray3D< float > **pZlut, float *radii, int angular_steps, bool useCorrelation, LVArray< float > **radialweights, bool normalize)
CDLL_EXPORT float DLL_CALLCONV compute_z(CPUTracker *tracker, float *center, int angularSteps, int zlut_index, uint *error, LVArray< float > **profile, int *bestIndex, LVArray< float > **errorCurve)
void ResizeLVArray2D(LVArray2D< T > **&d, int rows, int cols)
Definition: labview.h:98
CDLL_EXPORT void DLL_CALLCONV set_image_u16(CPUTracker *tracker, LVArray2D< ushort > **pData, ErrorCluster *error)
vector2f ComputeXCorInterpolated(vector2f initial, int iterations, int profileWidth, bool &boundaryHit)
Compute the cross correlation offsets and resulting position.
int GetWidth()
Get the width of the image.
Definition: cpu_tracker.h:123
CDLL_EXPORT void DLL_CALLCONV set_image_float(CPUTracker *tracker, LVArray2D< float > **pData, ErrorCluster *error)
void SetRadialZLUT(float *data, int planes, int res, int num_zluts, float minradius, float maxradius, bool copyMemory, bool useCorrelation)
Tell the tracker where in memory the LUT is located.
#define ALLOCA_ARRAY(T, N)
Definition: std_incl.h:153
void SetRadialWeights(float *radweights)
Set the radial weights to be used for profile comparisons.
T elem[1]
Definition: labview.h:43
Default. Use a 7-point quadratic fit around the error curve&#39;s maximum.
Definition: cpu_tracker.h:379
LabVIEW 3D Array template.
Definition: labview.h:57
CDLL_EXPORT void DLL_CALLCONV compute_crp(CPUTracker *tracker, LVArray< float > **result, int radialSteps, float *radii, float *center, uint *boundaryHit, LVArray2D< float > **crpmap)
LabVIEW 2D Array template.
Definition: labview.h:41
CDLL_EXPORT void DLL_CALLCONV set_image_from_memory(CPUTracker *tracker, LVArray2D< uchar > **pData, ErrorCluster *error)
CDLL_EXPORT void DLL_CALLCONV generate_test_image(LVArray2D< float > **img, float xp, float yp, float size, float photoncount)
int GetHeight()
Get the height of the image.
Definition: cpu_tracker.h:124
#define DLL_CALLCONV
Definition: dllmacros.h:3
CDLL_EXPORT void DLL_CALLCONV destroy_tracker(CPUTracker *tracker)
float * GetDebugImage()
Get the debug image.
Definition: cpu_tracker.h:440
CDLL_EXPORT void DLL_CALLCONV get_debug_img_as_array(CPUTracker *tracker, LVArray2D< float > **pdbgImg)
vector2f ComputeQI(vector2f initial, int iterations, int radialSteps, int angularStepsPerQuadrant, float angStepIterationFactor, float minRadius, float maxRadius, bool &boundaryHit, float *radialweights=0)
Execute the quadrant interpolation algorithm.
CDLL_EXPORT void DLL_CALLCONV compute_radial_profile(CPUTracker *tracker, LVArray< float > **result, int angularSteps, float *radii, float *center, uint *boundaryHit)
float ComputeAsymmetry(vector2f center, int radialSteps, int angularSteps, float minRadius, float maxRadius, float *dstAngProf=0)
Find a measure for the asymmetry of the ROI.
void SetImageFloat(float *srcImage)
Set an image with float type.
Definition: cpu_tracker.cpp:88
void NormalizeZLUT(float *zlut, int numBeads, int planes, int radialsteps)
Definition: utils.cpp:291
void ApplyPoissonNoise(ImageData &img, float poissonMax, float maxval)
Definition: utils.cpp:432
CDLL_EXPORT void DLL_CALLCONV generate_image_from_lut(LVArray2D< float > **image, LVArray2D< float > **lut, float *LUTradii, vector3f *position, float pixel_max, int useSplineInterp, int samplesPerPixel)
void SetImage16Bit(ushort *srcImage, uint srcpitch)
Set an image with 16 bit type.
Definition: cpu_tracker.h:247