-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
126 lines (111 loc) · 3.41 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <fstream>
#include <sstream>
//#include <string>
#include <cmath>
#include <random>
#include <dirent.h>
#include "data.h"
#include "test.h"
int numberOfLayers = 3;
string imagePath = "data/images/";
string locationPath = "data/locations/";
string rotationPath = "data/rotations/";
void copyChars(char *source, char *target){
int i= 0;
while(source[i] != '\0'){
target[i] = source[i];
++i;
}
target[i] = '\0';
}
void setRotationAngle(data * data, string currentLocation){
//Look, if rotation has already been calculated and stored in data/locations/
ifstream location(currentLocation);
if (location.is_open()){
// cout << "Previous calulation of rotation-angle found."<< endl;
std::string line;
std::getline(location, line);
std::stringstream iss(line);
// cout << line << endl;
double x = 0;
if (!(iss >> data->angle)){
cout << " oh no, no parsing "<< endl;
}
}
else{
cout << "No previous calulation of rotation-angle found. Calculate Rotation."<< endl;
data->calc_best_rot_angle();
std::ofstream outfile (currentLocation);
outfile << (double)data->angle << std::endl;
outfile.close();
}
}
void showStomata(data data){
string windowName = "Image ";
cv::namedWindow(windowName);
cv::Mat temp = data.getImage();
for(int j = 0; j < data.numberOfStomata(); ++j)
cv::circle(temp, data.getCoordinate(j), 20, CV_RGB(255,255,255),10);
cv::imshow(windowName , temp);
cv::waitKey(-1);
cv::destroyWindow(windowName);
}
std::vector<data> getData(){
std::vector<data> datasets;
struct dirent *entry;
DIR *dir = opendir(imagePath.c_str());
if (dir != NULL){
//Skip first 2 entries . & ..
entry = readdir(dir);
entry = readdir(dir);
while ((entry = readdir(dir))){
ifstream location(locationPath + entry->d_name + ".txt");
//Add the picture only if there is ground-truth data
if (location.is_open()){
char *name = new char[128];
copyChars(entry->d_name, name);
cv::Mat image= cv::imread(imagePath + name, CV_LOAD_IMAGE_GRAYSCALE);
std::vector<cv::Point> coordinates;
std::string line;
while (std::getline(location, line)){
std::istringstream iss(line);
int x, y;
char c;
if (!(iss >> x >> c >> y)){
cout << " oh no, no parsing "<< endl;
break;
}
coordinates.push_back(cv::Point(x,y));
}
data currentData = data(image, coordinates, name);
setRotationAngle(¤tData, rotationPath + entry->d_name + ".rot");
//showStomata(currentData);
/*
string windowName = "Image ";
cv::namedWindow(windowName);
image = currentData.getImage();
cv::Point center(image.cols/2.0, image.rows/2.0);
//cv::Mat rot = cv::getRotationMatrix2D(center, currentData.angle, 1.0);
//cv::warpAffine(image, image, rot , image.size());
for(int j = 0; j < currentData.numberOfStomata(); ++j)
cv::circle(image, currentData.getCoordinate(j), 2, CV_RGB(255,255,255),2);
cv::imshow(windowName + currentData.name, image);
cv::waitKey(-1);
cv::destroyWindow(windowName + currentData.name);
*/
datasets.push_back(currentData);
location.close();
}
}
}
cout << datasets.size() << " images with corresponding stomata coordinates found." << endl;
return datasets;
}
int main(void){
cout << "OpenCV version : " << CV_VERSION << endl;
std::vector<data> datasets = getData();
test testInstance = test(&datasets, 240, 6360);
testInstance.startTesting();
return EXIT_SUCCESS;
}