-
Notifications
You must be signed in to change notification settings - Fork 0
/
ips_simple.cpp
148 lines (123 loc) · 4.83 KB
/
ips_simple.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
#include "ips_simple.h"
IPS_simple::IPS_simple()
{
}
Mat IPS_simple::applyBlur(Mat src){
Mat dst = src.clone();
clock_t startStep = clock();
GaussianBlur( src, dst, Size( Constants::BLUR_KERNEL_LENGTH, Constants::BLUR_KERNEL_LENGTH ), 0, 0 );
clock_t stopStep = clock();
double elapsedStep = (double)difftime(startStep, stopStep) * 1000.0 / CLOCKS_PER_SEC;
printf("Tiempo de ejecucion de GaussianBlur:\t%f\tms \n", std::abs(elapsedStep) );
return dst;
}
Mat IPS_simple::applyLaplacian(Mat src){
Mat dst = src.clone();
clock_t startStep = clock();
Laplacian( src, dst, Constants::LAPLACE_DDEPTH, Constants::LAPLACE_KERNEL_SIZE, Constants::LAPLACE_SCALE,
Constants::LAPLACE_DELTA, BORDER_DEFAULT );
clock_t stopStep = clock();
double elapsedStep = (double)difftime(startStep, stopStep) * 1000.0 / CLOCKS_PER_SEC;
printf("Tiempo de ejecucion de Laplacian:\t%f\tms \n", std::abs(elapsedStep) );
return dst;
}
Mat IPS_simple::applyEdge(Mat src){
Mat dst = src.clone();
Mat src_gray = src.clone();
//cvtColor( src, src_gray, COLOR_BGR2GRAY );
clock_t startStep = clock();
Canny( src_gray, dst, Constants::EDGES_LOW_THRESHOLD, Constants::EDGES_LOW_THRESHOLD*Constants::EDGES_RATIO, Constants::EDGES_KERNEL_SIZE );
clock_t stopStep = clock();
double elapsedStep = (double)difftime(startStep, stopStep) * 1000.0 / CLOCKS_PER_SEC;
printf("Tiempo de ejecucion de Canny Edges:\t%f\tms \n", std::abs(elapsedStep) );
return dst;
}
Mat IPS_simple::applySubs(Mat raw_src, Mat ref_src){
Mat dst = raw_src.clone();
clock_t startStep = clock();
absdiff(raw_src,ref_src,dst);
clock_t stopStep = clock();
double elapsedStep = (double)difftime(startStep, stopStep) * 1000.0 / CLOCKS_PER_SEC;
printf("Tiempo de ejecucion de Substraction:\t%f\tms \n", std::abs(elapsedStep) );
return dst;
}
Mat IPS_simple::applyContourns(Mat src){
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
RNG rng(12345);
Mat src2 = src.clone();
Mat dst = src.clone();
Mat src_gray = src.clone();
Mat final = imread(Constants::IMG_RAW);
cvtColor( src, src_gray, CV_BGR2GRAY );
// Eliminar ruido
src2 = applyBlur(src_gray);
findContours( src2, contours, hierarchy,CV_CHAIN_APPROX_SIMPLE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
qDebug() << contours.data();
// Rotar los rectangulos buscando el area minima
vector<RotatedRect> minRect( contours.size() );
for( int i = 0; i < contours.size(); i++ ){
minRect[i] = minAreaRect( Mat(contours[i]) );
}
// Dibujar rectangulos
Scalar color = Scalar( rng.uniform(0,0), rng.uniform(250,250), rng.uniform(0,0) );
for( int i = 0; i< contours.size(); i++ ){
Point2f rect_points[4]; minRect[i].points( rect_points );
if( pow((int)(rect_points[0].x-(rect_points[1]).x), 2.0) + pow((int)(rect_points[0].y-(rect_points[1]).y), 2.0) > pow(Constants::CONTOURS_MIN_CONTOURS, 2.0) &&
pow((int)(rect_points[1].x-(rect_points[2]).x), 2.0) + pow((int)(rect_points[1].y-(rect_points[2]).y), 2.0) > pow(Constants::CONTOURS_MIN_CONTOURS, 2.0))
{
for(int j = 0; j < 4; j++ ){
line( dst, rect_points[j], rect_points[(j+1)%4], color, 2, 8 );
line( final, rect_points[j], rect_points[(j+1)%4], color, 2, 8 );
}
}
}
imwrite(Constants::IMG_FINAL,final);
return dst;
}
int IPS_simple::processBlur(){
//Raw Image
Mat src = imread( Constants::IMG_RAW_LAPLACE );
if (!src.data) return 1;
Mat dst = applyBlur(src);
imwrite( Constants::IMG_RAW_BLUR, dst );
//Ref Image
src = imread( Constants::IMG_REF_LAPLACE );
if (!src.data) return 1;
dst = applyBlur(src);
imwrite( Constants::IMG_REF_BLUR, dst );
return 0;
}
int IPS_simple::processLaplacian(){
//Raw Image
Mat src = imread( Constants::IMG_RAW );
if (!src.data) return 1;
Mat dst = applyLaplacian(src);
imwrite( Constants::IMG_RAW_LAPLACE, dst );
//Ref Image
src = imread( Constants::IMG_REF );
if (!src.data) return 1;
dst = applyLaplacian(src);
imwrite( Constants::IMG_REF_LAPLACE, dst );
return 0;
}
int IPS_simple::processEdge(){
Mat src = imread( Constants::IMG_SUBS );
if (!src.data) return 1;
Mat dst = applyEdge(src);
imwrite( Constants::IMG_EDGES, dst );
return 0;
}
int IPS_simple::processSubs(){
Mat src_raw = imread( Constants::IMG_RAW_BLUR );
Mat src_ref = imread( Constants::IMG_REF_BLUR );
if (!src_raw.data || !src_ref.data) return 1;
Mat dst = applySubs(src_raw,src_ref);
imwrite( Constants::IMG_SUBS, dst );
}
int IPS_simple::processContourns(){
Mat src = imread( Constants::IMG_EDGES );
if (!src.data) return 1;
Mat dst = applyContourns(src);
imwrite( Constants::IMG_CONTOURNS, dst );
}