QTrk
testutils.cpp
Go to the documentation of this file.
1 #include <time.h>
2 #include <string>
3 #include <stdio.h>
4 #include <iostream>
5 #include <fstream>
6 #include <sstream>
7 
8 #include "testutils.h"
9 
10 float distance(vector2f a,vector2f b) { return distance(a.x-b.x,a.y-b.y); }
11 
12 bool DirExists(const std::string& dirName_in)
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 }
23 
24 int NumFilesInDir(const std::string& dirName_in)
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 }
41 
42 int NumJpgInDir(const std::string& dirName_in)
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 }
62 
63 outputter::outputter(int mode) { init(mode); }
64 
66  if(modes.File && outputFile != NULL)
67  fclose(outputFile);
68 }
69 
70 void outputter::outputString(std::string out, bool ConsoleOnly){
71  if(modes.Console || ConsoleOnly){
72  std::cout << out << std::endl;
73  }
74 
75  if(modes.File && !ConsoleOnly){
76  if(!outputFile)
77  newFile("OutputFile");
78  fprintf_s(outputFile,"%s\n",out.c_str());
79  }
80 }
81 
82 void outputter::outputImage(ImageData img, std::string filename){
83  if(modes.Images){
84  std::string file = folder + filename + ".jpg";
85  FloatToJPEGFile(file.c_str(),img.data,img.w,img.h);
86  }
87 }
88 
89 void outputter::newFile(std::string filename, const char* mode){
90  if(modes.File){
91  if(outputFile)
92  fclose(outputFile);
93  std::string outfile = folder + filename + ".txt";
94  while(fopen(outfile.c_str(),"r")){
95  std::cout << "Output file " << filename << " already exists, please specify new filename:\n";
96  std::cin >> filename;
97  outfile = folder + filename + ".txt";
98  }
99  outputFile = fopen(outfile.c_str(),mode);
100  while(!outputFile){
101  std::cout << "Error creating output file "<< outfile << "\n\n";
102  std::cout << "Error " << outfile << " already exists, please specify new file path:\n";
103  std::cin >> outfile;
104  outputFile = fopen(outfile.c_str(),mode);
105  }
106  }
107 }
108 
109 void outputter::init(int mode){
110  modes.Console = (mode & Console) != 0;
111  modes.File = (mode & Files) != 0;
112  modes.Images = (mode & Images) != 0;
113 
114  outputFile = NULL;
115 
116  if(!modes.Console && !modes.File){
117  modes.Console = true;
118  printf_s("No output mode selected, using console by default.\n");
119  }
120 
121  if(modes.File || modes.Images){
122  char date[14];
124  folder = "D:\\TestImages\\TestOutput\\" + std::string(date) + "\\";
125  if(!CreateDirectory((LPCTSTR)folder.c_str(),NULL)){
126  printf_s("Error creating output folder");
127  throw("");
128  }
129  }
130 }
131 
132 ImageData CropImage(ImageData img, int x, int y, int w, int h)
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 }
147 
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 }
163 
164 ImageData AddImages(ImageData img1, ImageData img2, vector2f displacement)
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 }
179 
180 ImageData GaussMask(ImageData img, float sigma)
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 }
193 
194 ImageData SkewImage(ImageData img, float fact)
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 }
211 
212 void GetOuterEdges(float* out,int size, ImageData img){
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 }
234 
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 }
248 
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 }
257 
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 }
ImageData ResizeImage(ImageData img, int factor)
Definition: testutils.cpp:148
void GetFormattedTimeString(char *output)
Definition: utils.cpp:38
float BackgroundRMS(ImageData img)
Definition: testutils.cpp:258
void GetOuterEdges(float *out, int size, ImageData img)
Definition: testutils.cpp:212
std::string folder
Definition: testutils.h:39
int NumJpgInDir(const std::string &dirName_in)
Definition: testutils.cpp:42
int NumFilesInDir(const std::string &dirName_in)
Definition: testutils.cpp:24
void outputImage(ImageData img, std::string filename="UsedImage")
Definition: testutils.cpp:82
void outputString(std::string out, bool ConsoleOnly=false)
Definition: testutils.cpp:70
FILE * outputFile
Definition: testutils.h:48
ImageData AddImages(ImageData img1, ImageData img2, vector2f displacement)
Definition: testutils.cpp:164
outputter(int mode=1)
Definition: testutils.cpp:63
float distance(vector2f a, vector2f b)
Definition: testutils.cpp:10
vector2< float > vector2f
Definition: std_incl.h:39
vector3< T > sqrt(const vector3< T > &a)
Definition: std_incl.h:112
static TImageData alloc(int w, int h)
Definition: utils.h:110
T ComputeStdDev(T *data, int len)
Definition: utils.h:221
outputModes modes
Definition: testutils.h:47
ImageData CropImage(ImageData img, int x, int y, int w, int h)
Definition: testutils.cpp:132
float BackgroundMedian(ImageData img)
Definition: testutils.cpp:235
bool DirExists(const std::string &dirName_in)
Definition: testutils.cpp:12
int h
Definition: utils.h:81
T & at(int x, int y)
Definition: utils.h:96
void FloatToJPEGFile(const char *name, const float *d, int w, int h)
Definition: fastjpg.cpp:189
ImageData GaussMask(ImageData img, float sigma)
Definition: testutils.cpp:180
void newFile(std::string filename, const char *mode="a")
Definition: testutils.cpp:89
ImageData SkewImage(ImageData img, float fact)
Definition: testutils.cpp:194
float BackgroundStdDev(ImageData img)
Definition: testutils.cpp:249
void init(int mode)
Definition: testutils.cpp:109
int w
Definition: utils.h:81
T * data
Definition: utils.h:80