-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
202 lines (160 loc) · 4.79 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <iostream>
#include <stdint.h>
#include <ctime>
#include <fstream>
#include <string>
#include <sstream>
#include <typeinfo>
#include <argp.h>
#include <sys/time.h>
using namespace std;
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/gpu/gpu.hpp>
using namespace cv;
struct Arguments {
string project;
string input;
string output;
int padding;
int frames;
string extension;
int width;
int height;
int area_min;
int area_max;
int search_win_size;
int blur_radius;
int threshold_win_size;
float threshold_ratio;
string log;
bool verbose;
Arguments() :
input("data/"), output("output.txt"), padding(7), frames(40), extension(
".jpg"), width(24), height(10), area_min(200), area_max(
400), search_win_size(100), blur_radius(3), threshold_win_size(
25), threshold_ratio(0.9), log("wormSeg.log"), verbose(true) {
}
} cla;
int timeToUpload = 0;
int findCentroidFromImage(cv::Mat, int*, int*, int*);
template<typename T> string NumberToString(T pNumber) {
ostringstream oOStrStream;
oOStrStream << pNumber;
return oOStrStream.str();
}
string intToFileName(string fileNameFormat, int fileNumber) {
string temp = NumberToString(fileNumber);
return fileNameFormat.replace(fileNameFormat.size() - temp.size(),
temp.size(), temp);
}
void func(const float*, float*, size_t, const size_t, int, int);
void callKernel(const cv::gpu::GpuMat &src, cv::gpu::GpuMat &dst) {
float* p = (float*) src.data;
float* p2 = (float*) dst.data;
func(p, p2, src.step, dst.step, src.cols, src.rows);
}
int findCentroidFromImage(cv::Mat src, int *pX, int *pY, int *pArea) {
// cout << "size=" << sizeof(bool);
//GPU Mat... Copy from CPU memory to GPU memory...
cv::gpu::GpuMat gpu_src(src);
// cout << "\nsrc\n " << src << endl;
cv::gpu::GpuMat matAfterBlur;
//Filters on GPU...
cv::gpu::blur(gpu_src, matAfterBlur,
Size(cla.blur_radius, cla.blur_radius));
cv::gpu::GpuMat matAfterThreshold;
//Convert into Binary image on GPU...
cv::gpu::threshold(matAfterBlur, matAfterThreshold,
int(cla.threshold_ratio * 255), 255, THRESH_BINARY_INV);
cv::gpu::GpuMat floatMatForKernel;
matAfterThreshold.convertTo(floatMatForKernel, CV_32FC1);
callKernel(floatMatForKernel, gpu_src);
//Copy from GPU memory to CPU memory...
cv::Mat cpu_src(gpu_src);
// cout << "\nafter thr=\n" << cpu_src << endl;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
//findContours on CPU...
//Need to write this method on GPU... To improve overall performance...
//Check Sobel Algorithm...
cv::findContours(cpu_src, contours, hierarchy, CV_RETR_CCOMP,
CV_CHAIN_APPROX_SIMPLE);
if (contours.size() > 0) {
int largest_contour_index = 0;
int largest_area = 0;
for (int i = 0; i < contours.size(); i++) {
double a = cv::contourArea(contours[i], false);
if (a > largest_area) {
largest_area = a;
largest_contour_index = i;
}
}
cv::Rect bRect = cv::boundingRect(contours[largest_contour_index]);
*pX = bRect.x + (bRect.width / 2);
*pY = bRect.y + (bRect.height / 2);
*pArea = largest_area;
} else {
*pX = -1;
*pY = -1;
*pArea = -1;
}
return 0;
}
int wormSegmenter() {
fstream outputFile;
outputFile.open(cla.output.c_str(), ios::out);
int x = -1, y = -1, area = -1;
int adjustX = 0, adjustY = 0;
for (int fileNumber = 0; fileNumber < cla.frames; fileNumber++) {
string fileName = cla.input + intToFileName("0000000", fileNumber)
+ cla.extension;
cv::Mat src = cv::imread(fileName, CV_LOAD_IMAGE_GRAYSCALE);
if (!src.data) {
// cout << endl << "Exited." << endl;
exit(1);
}
if ((x == -1) && (y == -1)) {
findCentroidFromImage(src, &x, &y, &area);
src = cv::imread(fileName, CV_LOAD_IMAGE_GRAYSCALE);
adjustX = x - (cla.search_win_size / 2);
adjustY = y - (cla.search_win_size / 2);
} else {
src = src(
cv::Rect(x - (cla.search_win_size / 2),
y - (cla.search_win_size / 2), cla.search_win_size,
cla.search_win_size));
findCentroidFromImage(src, &x, &y, &area);
if ((x > 0) && (y > 0)) {
x += adjustX;
y += adjustY;
adjustX = x - (cla.search_win_size / 2);
adjustY = y - (cla.search_win_size / 2);
}
}
outputFile << fileNumber << ", " << x << ", " << y << ", " << area
<< endl;
}
outputFile.close();
return 0;
}
int main(int argc, char **argv) {
wormSegmenter();
cout << "time=" << timeToUpload << endl;
return 0;
}
//int main() {
// Mat input = imread("0000001.jpg", 0);
// std::cout << "matrix input=\n " << input;
// Mat float_input;
// input.convertTo(float_input, CV_32FC1);
// cv::gpu::GpuMat d_frame, d_output;
// Size size = float_input.size();
// d_frame.upload(float_input);
// d_output.create(size, CV_32FC1);
// callKernel(d_frame, d_output);
// Mat output(d_output);
// return 0;
//}