QTrk
Functions
testutils.cpp File Reference
#include <time.h>
#include <string>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include "testutils.h"

Go to the source code of this file.

Functions

float distance (vector2f a, vector2f b)
 
bool DirExists (const std::string &dirName_in)
 
int NumFilesInDir (const std::string &dirName_in)
 
int NumJpgInDir (const std::string &dirName_in)
 
ImageData CropImage (ImageData img, int x, int y, int w, int h)
 
ImageData ResizeImage (ImageData img, int factor)
 
ImageData AddImages (ImageData img1, ImageData img2, vector2f displacement)
 
ImageData GaussMask (ImageData img, float sigma)
 
ImageData SkewImage (ImageData img, float fact)
 
void GetOuterEdges (float *out, int size, ImageData img)
 
float BackgroundMedian (ImageData img)
 
float BackgroundStdDev (ImageData img)
 
float BackgroundRMS (ImageData img)
 

Function Documentation

§ AddImages()

ImageData AddImages ( ImageData  img1,
ImageData  img2,
vector2f  displacement 
)

Definition at line 164 of file testutils.cpp.

165 {
166  ImageData addedImg = ImageData::alloc(img1.w,img1.h);
167 
168  for(int x_i=0;x_i<img1.w;x_i++){
169  for(int y_i=0;y_i<img2.h;y_i++){
170  if(x_i-displacement.x > 0 && x_i-displacement.x < img1.w && y_i-displacement.y > 0 && y_i-displacement.y < img1.h) {
171  addedImg.at(x_i,y_i) = ( img1.at(x_i,y_i) + img2.at(x_i-displacement.x,y_i-displacement.y) )/2;
172  } else {
173  addedImg.at(x_i,y_i) = img1.at(x_i,y_i);
174  }
175  }
176  }
177  return addedImg;
178 }
static TImageData alloc(int w, int h)
Definition: utils.h:110
int h
Definition: utils.h:81
T & at(int x, int y)
Definition: utils.h:96
int w
Definition: utils.h:81

§ BackgroundMedian()

float BackgroundMedian ( ImageData  img)

Definition at line 235 of file testutils.cpp.

235  {
236  int size = img.w * 2 + img.h * 2 - 4;
237  float* outeredge = new float[size];
238  GetOuterEdges(outeredge,size,img);
239  std::sort(outeredge,outeredge+size);
240  float median;
241  if(size % 2 == 0)
242  median = (outeredge[(int)(size/2-1)] + outeredge[(int)(size/2+1)])/2;
243  else
244  median = outeredge[size/2];
245  delete[] outeredge;
246  return median;
247 }
void GetOuterEdges(float *out, int size, ImageData img)
Definition: testutils.cpp:212
int h
Definition: utils.h:81
int w
Definition: utils.h:81

§ BackgroundRMS()

float BackgroundRMS ( ImageData  img)

Definition at line 258 of file testutils.cpp.

258  {
259  int size = img.w * 2 + img.h * 2 - 4;
260  float* outeredge = new float[size];
261  GetOuterEdges(outeredge,size,img);
262  float sqsum = 0.0f;
263  for(int ii = 0; ii < size; ii++){
264  sqsum += outeredge[ii]*outeredge[ii];
265  }
266  delete[] outeredge;
267  return sqrt(1/(float)size*sqsum);
268 }
void GetOuterEdges(float *out, int size, ImageData img)
Definition: testutils.cpp:212
vector3< T > sqrt(const vector3< T > &a)
Definition: std_incl.h:112
int h
Definition: utils.h:81
int w
Definition: utils.h:81

§ BackgroundStdDev()

float BackgroundStdDev ( ImageData  img)

Definition at line 249 of file testutils.cpp.

249  {
250  int size = img.w * 2 + img.h * 2 - 4;
251  float* outeredge = new float[size];
252  GetOuterEdges(outeredge,size,img);
253  float stddev = ComputeStdDev(outeredge,size);
254  delete[] outeredge;
255  return stddev;
256 }
void GetOuterEdges(float *out, int size, ImageData img)
Definition: testutils.cpp:212
T ComputeStdDev(T *data, int len)
Definition: utils.h:221
int h
Definition: utils.h:81
int w
Definition: utils.h:81

§ CropImage()

ImageData CropImage ( ImageData  img,
int  x,
int  y,
int  w,
int  h 
)

Definition at line 132 of file testutils.cpp.

133 {
134  ImageData croppedImg = ImageData::alloc(w,h);
135 
136  if( x < 0 || y < 0 || x + w > img.w || y + h > img.h){
137  return img;
138  }
139 
140  for(int x_i = x; x_i < x+w; x_i++){
141  for(int y_i = y; y_i < y+h; y_i++){
142  croppedImg.at(x_i-x,y_i-y) = img.at(x_i,y_i);
143  }
144  }
145  return croppedImg;
146 }
static TImageData alloc(int w, int h)
Definition: utils.h:110
int h
Definition: utils.h:81
T & at(int x, int y)
Definition: utils.h:96
int w
Definition: utils.h:81

§ DirExists()

bool DirExists ( const std::string &  dirName_in)

Definition at line 12 of file testutils.cpp.

13 {
14  DWORD ftyp = GetFileAttributesA(dirName_in.c_str());
15  if (ftyp == INVALID_FILE_ATTRIBUTES)
16  return false; //something is wrong with your path!
17 
18  if (ftyp & FILE_ATTRIBUTE_DIRECTORY)
19  return true; // this is a directory!
20 
21  return false; // this is not a directory!
22 }

§ distance()

float distance ( vector2f  a,
vector2f  b 
)

Definition at line 10 of file testutils.cpp.

10 { return distance(a.x-b.x,a.y-b.y); }
float distance(vector2f a, vector2f b)
Definition: testutils.cpp:10

§ GaussMask()

ImageData GaussMask ( ImageData  img,
float  sigma 
)

Definition at line 180 of file testutils.cpp.

181 {
182  ImageData gaussImg = ImageData::alloc(img.w,img.h);
183  vector2f centre = vector2f(img.w/2,img.h/2);
184  for(int x_i=0;x_i<img.w;x_i++){
185  for(int y_i=0;y_i<img.h;y_i++){
186  float gaussfact = expf(- (x_i-centre.x)*(x_i-centre.x) / (2*sigma*sigma) - (y_i-centre.y)*(y_i-centre.y) / (2*sigma*sigma));
187  gaussImg.at(x_i,y_i) = img.at(x_i,y_i)*gaussfact;
188  }
189  }
190 
191  return gaussImg;
192 }
vector2< float > vector2f
Definition: std_incl.h:39
static TImageData alloc(int w, int h)
Definition: utils.h:110
int h
Definition: utils.h:81
T & at(int x, int y)
Definition: utils.h:96
int w
Definition: utils.h:81

§ GetOuterEdges()

void GetOuterEdges ( float *  out,
int  size,
ImageData  img 
)

Definition at line 212 of file testutils.cpp.

212  {
213  int x,y=0;
214  for(int ii = 0; ii < size; ii++){
215  if(ii < img.w){ // Top
216  x = ii;
217  y = 0;
218  }
219  else if(ii < img.w + img.h - 1){ // Right
220  x = img.w-1;
221  y = ii-(img.w-1);
222  }
223  else if(ii < img.w * 2 + img.h - 2){ // Bottom
224  y = img.h-1;
225  x = ii-(img.h+img.w-1);
226  }
227  else{ // Left
228  x = 0;
229  y = ii-(img.h+img.w*2-3);
230  }
231  out[ii] = img.at(x,y);
232  }
233 }
int h
Definition: utils.h:81
T & at(int x, int y)
Definition: utils.h:96
int w
Definition: utils.h:81

§ NumFilesInDir()

int NumFilesInDir ( const std::string &  dirName_in)

Definition at line 24 of file testutils.cpp.

25 {
26  WIN32_FIND_DATA FindFileData;
27  HANDLE hFind;
28 
29  std::string dirName = dirName_in + "*";
30  hFind = FindFirstFile(dirName.c_str(),&FindFileData);
31  if (hFind == INVALID_HANDLE_VALUE) {
32  printf ("FindFirstFile failed (%d)\n", GetLastError());
33  return 0;
34  }
35  int out = -1;
36  while(FindNextFile(hFind,&FindFileData)){
37  out++;
38  }
39  return out;
40 }

§ NumJpgInDir()

int NumJpgInDir ( const std::string &  dirName_in)

Definition at line 42 of file testutils.cpp.

43 {
44  WIN32_FIND_DATA FindFileData;
45  HANDLE hFind;
46 
47  std::string dirName = dirName_in + "*";
48  hFind = FindFirstFile(dirName.c_str(),&FindFileData);
49  if (hFind == INVALID_HANDLE_VALUE) {
50  printf ("FindFirstFile failed (%d)\n", GetLastError());
51  return 0;
52  }
53  int out = -1;
54  while(FindNextFile(hFind,&FindFileData)){
55  std::string name = FindFileData.cFileName;
56  if(name.find(".jpg")!=name.npos) {
57  out++;
58  }
59  }
60  return out;
61 }

§ ResizeImage()

ImageData ResizeImage ( ImageData  img,
int  factor 
)

Definition at line 148 of file testutils.cpp.

149 {
150  ImageData resizedImg = ImageData::alloc(img.w*factor,img.h*factor);
151 
152  for(int x_i=0;x_i<img.w;x_i++){
153  for(int y_i=0;y_i<img.h;y_i++){
154  for(int x_fact=0;x_fact<factor;x_fact++){
155  for(int y_fact=0;y_fact<factor;y_fact++){
156  resizedImg.at(x_i*factor+x_fact,y_i*factor+y_fact) = img.at(x_i,y_i);
157  }
158  }
159  }
160  }
161  return resizedImg;
162 }
static TImageData alloc(int w, int h)
Definition: utils.h:110
int h
Definition: utils.h:81
T & at(int x, int y)
Definition: utils.h:96
int w
Definition: utils.h:81

§ SkewImage()

ImageData SkewImage ( ImageData  img,
float  fact 
)

Definition at line 194 of file testutils.cpp.

195 {
196  ImageData skewImg = ImageData::alloc(img.w,img.h);
197  vector2f centre = vector2f(img.w/2,img.h/2);
198  float median = BackgroundMedian(img);
199  float stddev = BackgroundStdDev(img);
200  float maxskew = median*stddev;
201  for(int x_i=0;x_i<img.w;x_i++){
202  for(int y_i=0;y_i<img.h;y_i++){
203  int diagonalOffset = (y_i - x_i*img.h/img.w);
204  float skew = fact*((float)diagonalOffset/img.h)*maxskew;
205  skewImg.at(x_i,y_i) = img.at(x_i,y_i)+skew;
206  }
207  }
208 
209  return skewImg;
210 }
vector2< float > vector2f
Definition: std_incl.h:39
static TImageData alloc(int w, int h)
Definition: utils.h:110
float BackgroundMedian(ImageData img)
Definition: testutils.cpp:235
int h
Definition: utils.h:81
T & at(int x, int y)
Definition: utils.h:96
float BackgroundStdDev(ImageData img)
Definition: testutils.cpp:249
int w
Definition: utils.h:81