QTrk
cpu_tracker.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "QueuedTracker.h"
4 #include "utils.h"
5 #include "scalar_types.h"
6 #include "kissfft.h"
7 
9 class XCor1DBuffer {
10 public:
12  XCor1DBuffer(int xcorw) : fft_forward(xcorw, false), fft_backward(xcorw, true), xcorw(xcorw)
13  {}
14 
15  kissfft<scalar_t> fft_forward;
16  kissfft<scalar_t> fft_backward;
17  int xcorw;
18 
29  void XCorFFTHelper(complex_t* prof, complex_t* prof_rev, scalar_t* result);
30 };
31 
39 {
40 public:
41  int width;
42  int height;
43  int xcorw;
44  int trackerID;
45 
46  float *srcImage;
47  float *debugImage;
48  float mean;
49  float stdev;
50 #ifdef _DEBUG
51  float maxImageValue;
52 #endif
53 
61  float* zluts;
64  int zlut_res;
65  int zlut_count;
68  std::vector<float> zlut_radialweights;
69  kissfft<scalar_t> *qa_fft_forward;
70  kissfft<scalar_t> *qa_fft_backward;
71 
82  bool testRun;
83 
90  float* GetRadialZLUT(int index) { return &zluts[zlut_res*zlut_planes*index]; }
91 
93  std::vector<vector2f> quadrantDirs;
95  kissfft<scalar_t> *qi_fft_forward;
96  kissfft<scalar_t> *qi_fft_backward;
97 
99  class FFT2D {
100  public:
101  kissfft<float> xfft;
102  kissfft<float> yfft;
103  std::complex<float> *cbuf;
104 
109  FFT2D(int w, int h) : xfft(w,false), yfft(h,false) {cbuf=new std::complex<float>[w*h]; }
110 
112  ~FFT2D() { delete[] cbuf; }
113 
118  void Apply(float* d);
119  };
121 
122  float& GetPixel(int x, int y) { return srcImage[width*y+x]; }
123  int GetWidth() { return width; }
124  int GetHeight() { return height; }
125 
133  CPUTracker(int w, int h, int xcorwindow=128, bool testRun = false);
134 
136  ~CPUTracker();
137 
145  bool CheckBoundaries(vector2f center, float radius);
146 
158  vector2f ComputeXCorInterpolated(vector2f initial, int iterations, int profileWidth, bool& boundaryHit);
159 
176  vector2f ComputeQI(vector2f initial, int iterations, int radialSteps, int angularStepsPerQuadrant,
177  float angStepIterationFactor, float minRadius, float maxRadius, bool& boundaryHit, float* radialweights=0);
178 
180  struct Gauss2DResult {
182  float I0;
183  float bg;
184  };
185 
194  Gauss2DResult Compute2DGaussianMLE(vector2f initial, int iterations, float sigma);
195 
204  scalar_t QI_ComputeOffset(complex_t* qi_profile, int nr, int axisForDebug);
205 
215  scalar_t QuadrantAlign_ComputeOffset(complex_t* profile, complex_t* zlut_prof_fft, int nr, int axisForDebug);
216 
233  float ComputeAsymmetry(vector2f center, int radialSteps, int angularSteps, float minRadius, float maxRadius, float *dstAngProf=0);
234 
240  template<typename TPixel> void SetImage(TPixel* srcImage, uint srcpitch);
241 
247  void SetImage16Bit(ushort* srcImage, uint srcpitch) { SetImage(srcImage, srcpitch); }
248 
254  void SetImage8Bit(uchar* srcImage, uint srcpitch) { SetImage(srcImage, srcpitch); }
255 
260  void SetImageFloat(float* srcImage);
261 
266  void SaveImage(const char *filename);
267 
278  vector2f ComputeMeanAndCOM(float bgcorrection=0.0f);
279 
292  void ComputeRadialProfile(float* dst, int radialSteps, int angularSteps, float minradius, float maxradius, vector2f center, bool crp, bool* boundaryHit=0, bool normalize=true);
293 
305  void ComputeQuadrantProfile(scalar_t* dst, int radialSteps, int angularSteps, int quadrant, float minRadius, float maxRadius, vector2f center, float* radialWeights=0);
306 
319  float ComputeZ(vector2f center, int angularSteps, int zlutIndex, bool* boundaryHit=0, float* profile=0, float* cmpprof=0, bool normalizeProfile=true)
320  {
321  float* prof = profile ? profile : ALLOCA_ARRAY(float, zlut_res);
322  ComputeRadialProfile(prof,zlut_res,angularSteps, zlut_minradius, zlut_maxradius, center, false, boundaryHit, normalizeProfile);
323  return LUTProfileCompare(prof, zlutIndex, cmpprof, LUTProfMaxQuadraticFit);
324  }
325 
330  void FourierTransform2D();
331 
342  void FourierRadialProfile(float* dst, int radialSteps, int angularSteps, float minradius, float maxradius);
343 
350  void Normalize(float *image=0);
351 
368  void SetRadialZLUT(float* data, int planes, int res, int num_zluts, float minradius, float maxradius, bool copyMemory, bool useCorrelation);
369 
375  void SetRadialWeights(float* radweights);
376 
381  LUTProfMaxSimpleInterp
382  };
383 
390  void CalculateErrorCurve(double* errorcurve_dest, float* profile, float* zlut_sel);
391 
401  void CalculateInterpolatedZLUTProfile(float* profile_dest, float z, int zlutIndex);
402 
410  float CalculateErrorFlag(double* prof1, double* prof2);
411 
422  float LUTProfileCompare(float* profile, int zlutIndex, float* cmpProf, LUTProfileMaxComputeMode maxPosMethod, float* fitcurve=0, int *maxPos=0, int frameNum = 0);
423 
432  float LUTProfileCompareAdjustedWeights(float* rprof, int zlutIndex, float z_estim);
433 
440  float* GetDebugImage() { return debugImage; }
441 
451  void ApplyOffsetGain(float *offset, float *gain, float offsetFactor, float gainFactor);
452 
459  void AllocateQIFFTs(int nsteps);
460 
472  vector3f QuadrantAlign(vector3f initial, int beadIndex, int angularStepsPerQuadrant, bool& boundaryHit);
473 };
474 
475 template<typename TPixel>
476 void CPUTracker::SetImage(TPixel* data, uint pitchInBytes)
477 {
478  uchar* bp = (uchar*)data;
479 
480  for (int y=0;y<height;y++) {
481  for (int x=0;x<width;x++) {
482  srcImage[y*width+x] = ((TPixel*)bp)[x];
483  }
484  bp += pitchInBytes;
485  }
486 
487  mean=0.0f;
488 }
std::complex< float > * cbuf
Calculation buffer.
Definition: cpu_tracker.h:103
kissfft< scalar_t > * qa_fft_backward
Handle to backward FFT kissfft instance for quadrant align.
Definition: cpu_tracker.h:70
kissfft< scalar_t > * qi_fft_forward
Handle to forward FFT kissfft instance for QI.
Definition: cpu_tracker.h:95
XCor1DBuffer(int xcorw)
Constructs the buffer and initiates the FFT workers.
Definition: cpu_tracker.h:12
float zlut_maxradius
Maximum radius in pixels of the ZLUT profile sampling circle.
Definition: cpu_tracker.h:67
__device__ void ComputeQuadrantProfile(cudaImageListf &images, int idx, float *dst, const QIParams &params, int quadrant, float2 center, float mean, int angularSteps)
Definition: QI_impl.h:12
kissfft< float > yfft
Handle to FFT kissfft instance for y.
Definition: cpu_tracker.h:102
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
Class with all CPU algorithm implementations.
Definition: cpu_tracker.h:38
void SetImage(TPixel *srcImage, uint srcpitch)
Set the image on which to perform the tracking.
Definition: cpu_tracker.h:476
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
bool testRun
Flag to enable running a test run.
Definition: cpu_tracker.h:82
std::vector< float > zlut_radialweights
Vector with the radialweights used by the error curve calculation.
Definition: cpu_tracker.h:68
int zlut_count
Number of ZLUTs (= # beads) available.
Definition: cpu_tracker.h:65
Structure to group results from the 2D Gaussian fit.
Definition: cpu_tracker.h:180
FFT2D * fft2d
Instance of FFT2D to perform 2D FFTs.
Definition: cpu_tracker.h:120
FFT2D(int w, int h)
Initialize the class.
Definition: cpu_tracker.h:109
void normalize(TPixel *d, uint w, uint h)
Definition: utils.h:27
int height
ROI height.
Definition: cpu_tracker.h:42
vector2f pos
Found position.
Definition: cpu_tracker.h:181
unsigned short ushort
Definition: std_incl.h:128
void SetImage8Bit(uchar *srcImage, uint srcpitch)
Set an image with 8 bit type.
Definition: cpu_tracker.h:254
__global__ void ApplyOffsetGain(BaseKernelParams kp, cudaImageListf calib_gain, cudaImageListf calib_offset, float gainFactor, float offsetFactor)
Definition: Kernels.h:169
float stdev
Standard deviation of values in the ROI. Calculated in ComputeMeanAndCOM.
Definition: cpu_tracker.h:49
float & GetPixel(int x, int y)
Get the image pixel greyscale value at point (x,y).
Definition: cpu_tracker.h:122
int xcorw
Width of the cross correlation.
Definition: cpu_tracker.h:17
float * GetRadialZLUT(int index)
Get the start of the ZLUT of a specific bead.
Definition: cpu_tracker.h:90
float * srcImage
Memory region that holds the image on which tracking is to be performed.
Definition: cpu_tracker.h:46
int xcorw
Cross correlation profile length.
Definition: cpu_tracker.h:43
float scalar_t
Definition: scalar_types.h:9
float mean
Mean intensity of the ROI. Calculated in ComputeMeanAndCOM.
Definition: cpu_tracker.h:48
kissfft< scalar_t > * qa_fft_forward
Handle to forward FFT kissfft instance for quadrant align.
Definition: cpu_tracker.h:69
float ComputeZ(vector2f center, int angularSteps, int zlutIndex, bool *boundaryHit=0, float *profile=0, float *cmpprof=0, bool normalizeProfile=true)
Helper function to calculate the Z position.
Definition: cpu_tracker.h:319
XCor1DBuffer * xcorBuffer
The handle from which to perform cross correlation calculations.
Definition: cpu_tracker.h:92
kissfft< scalar_t > fft_backward
Handle to backward FFT kissfft instance.
Definition: cpu_tracker.h:16
Class to facilitate 2D Fourier transforms.
Definition: cpu_tracker.h:99
float bg
Found fit parameter.
Definition: cpu_tracker.h:183
int GetWidth()
Get the width of the image.
Definition: cpu_tracker.h:123
float * debugImage
Memory in which an intermediate image can optionally be place and retrieved.
Definition: cpu_tracker.h:47
#define ALLOCA_ARRAY(T, N)
Definition: std_incl.h:153
Default. Use a 7-point quadratic fit around the error curve&#39;s maximum.
Definition: cpu_tracker.h:379
int GetHeight()
Get the height of the image.
Definition: cpu_tracker.h:124
~FFT2D()
Free memory and delete the instance.
Definition: cpu_tracker.h:112
void XCorFFTHelper(complex_t *prof, complex_t *prof_rev, scalar_t *result)
Calculates a cross correlation much like https://en.wikipedia.org/wiki/Autocorrelation#Efficient_comp...
LUTProfileMaxComputeMode
Settings to compute the Z coordinate from the error curve.
Definition: cpu_tracker.h:378
Class to facilitate 1D cross correlation calculations.
Definition: cpu_tracker.h:9
kissfft< scalar_t > fft_forward
Handle to forward FFT kissfft instance.
Definition: cpu_tracker.h:15
Compute a spline fit.
Definition: cpu_tracker.h:380
std::vector< vector2f > quadrantDirs
Vector with the sampling points for a single quadrant (cos & sin pairs).
Definition: cpu_tracker.h:93
kissfft< float > xfft
Handle to FFT kissfft instance for x.
Definition: cpu_tracker.h:101
void ComputeRadialProfile(float *dst, int radialSteps, int angularSteps, float minradius, float maxradius, vector2f center, ImageData *img, float mean, bool normalize)
Definition: utils.cpp:298
int qi_radialsteps
Number of radialsteps in the QI calculations.
Definition: cpu_tracker.h:94
float I0
Found fit parameter.
Definition: cpu_tracker.h:182
int trackerID
ID of this tracker (= ID of thread it runs in).
Definition: cpu_tracker.h:44
float * GetDebugImage()
Get the debug image.
Definition: cpu_tracker.h:440
kissfft< scalar_t > * qi_fft_backward
Handle to backward FFT kissfft instance for QI.
Definition: cpu_tracker.h:96
std::complex< scalar_t > complex_t
Definition: scalar_types.h:12
unsigned char uchar
Definition: std_incl.h:130
float * zluts
Pointer to the first data in the ZLUTs.
Definition: cpu_tracker.h:61
bool zlut_memoryOwner
Flag indicating if this instance is the owner of the zluts memory, or is it external. False in all normal operation.
Definition: cpu_tracker.h:62
int width
ROI width.
Definition: cpu_tracker.h:41
void SetImage16Bit(ushort *srcImage, uint srcpitch)
Set an image with 16 bit type.
Definition: cpu_tracker.h:247