-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
209 lines (191 loc) · 7.71 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
203
204
205
206
207
208
209
#include <jni.h>
#include <opencv2/opencv.hpp>
#include <bits/stdc++.h>
#include <android/log.h>
#define LOG_TAG "JNI_PART"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG, __VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG, __VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG, __VA_ARGS__)
#define LOGF(...) __android_log_print(ANDROID_LOG_FATAL,LOG_TAG, __VA_ARGS__)
using namespace cv;
using namespace std;
extern "C" {
jstring Java_com_martin_ads_testopencv_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
int thresh = 120;
double max_size_ratio = 0.6;
double min_size_ratio = 0.1;
static double angle(Point pt1, Point pt2, Point pt0) {
double dx1 = pt1.x - pt0.x;
double dy1 = pt1.y - pt0.y;
double dx2 = pt2.x - pt0.x;
double dy2 = pt2.y - pt0.y;
return (dx1 * dx2 + dy1 * dy2) /
sqrt((dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2) + 1e-10);
}
void
selectFeatureBounds(Mat &frame, CvPoint topLeftPoint, CvPoint oppssiteTotopLeft,
MatSize imageSize) {
cv::Mat overlay;
double alpha = 0.3;
frame.copyTo(overlay);
cv::rectangle(overlay, topLeftPoint, oppssiteTotopLeft, cvScalar(0, 0, 255, 0), CV_FILLED, 4);
// cv::rectangle(frame, topLeftPoint, oppssiteTotopLeft, cvScalar(0, 0, 255 * 0.7), CV_FILLED, 4);
cv::addWeighted(overlay, alpha, frame, 1 - alpha, 0, frame);
}
static void findSquares(const Mat &image, vector<vector<Point> > &squares, double resize_scale) {
squares.clear();
Mat pyr, timg, gray0(image.size(), CV_8U), gray;
image.copyTo(timg);
int max_size = image.cols * image.rows;
vector<vector<Point> > contours;
int N = 4;
for (int c = 0; c < 3; c++) {
int ch[] = {c, 0};
mixChannels(&timg, 1, &gray0, 1, ch, 1);
for (int l = 0; l < N; l++) {
if (l == 0) {
Canny(gray0, gray, 0, thresh, 3);
dilate(gray, gray, Mat(), Point(-1, -1));
} else {
gray = (gray0 >= (l + 1) * 255 / N);
}
findContours(gray, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
vector<Point> approx;
for (size_t i = 0; i < contours.size(); i++) {
double area0 = contourArea(contours[i]);
if ((area0 > max_size_ratio * max_size) || (area0 < min_size_ratio * max_size))
continue;
approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true) * 0.015,
true);
if (approx.size() == 4 && isContourConvex(Mat(approx))) {
double maxCosine = 0;
for (int j = 2; j < 5; j++) {
double cosine = fabs(angle(approx[j % 4], approx[j - 2], approx[j - 1]));
maxCosine = MAX(maxCosine, cosine);
}
//if( maxCosine < 0.5 ) //angle must be larger than 60
if (maxCosine < 0.25) //angle must be larger than 75
squares.push_back(approx);
}
}
}
}
//pick the center one
if (squares.size()) {
double min_dis = 100000;
vector<Point> pts;
for (size_t i = 0; i < squares.size(); i++) {
double new_dis =
fabs(squares[i][0].x + squares[i][1].x + squares[i][2].x + squares[i][3].x) /
4.0 +
(squares[i][0].y + squares[i][1].y + squares[i][2].y + squares[i][3].y) / 4.0 -
image.cols / 2.0 - image.rows / 2.0;
if (new_dis < min_dis) {
min_dis = new_dis;
pts = squares[i];
}
}
squares.clear();
for (size_t i = 0; i < pts.size(); i++) {
pts[i].x = int(pts[i].x * (1.0 / resize_scale));
pts[i].y = int(pts[i].y * (1.0 / resize_scale));
}
double max_x = -1, min_x = 10000, max_y = -1, min_y = 100000;
for (size_t i = 0; i < pts.size(); i++) {
max_x = max_x > pts[i].x ? max_x : pts[i].x;
min_x = min_x < pts[i].x ? min_x : pts[i].x;
max_y = max_y > pts[i].y ? max_y : pts[i].y;
min_y = min_y < pts[i].y ? min_y : pts[i].y;
}
pts.clear();
pts.push_back(Point(min_x, min_y));
pts.push_back(Point(max_x, min_y));
pts.push_back(Point(max_x, max_y));
pts.push_back(Point(min_x, max_y));
squares.push_back(pts);
}
}
static void
drawSquares(Mat &image, const vector<vector<Point> > &squares
) {
Size boxSize = image.size();
int height = boxSize.height;
int width = boxSize.width;
int horizontallineLength = boxSize.width / 4;
int verticallineLength = height / 5;
int lineLength = 30;
for (size_t i = 0; i < squares.size(); i++) {
const Point *p = &squares[i][0];
int n = (int) squares[i].size();
cv::line(image,
cvPoint(squares[i][0].x, squares[i][0].y),
cvPoint(squares[i][0].x + lineLength,
squares[i][0].y), cvScalar(0, 0, 255, 0),
8, 4);
cv::line(image,
cvPoint(squares[i][0].x, squares[i][0].y),
cvPoint(squares[i][0].x,
squares[i][0].y + lineLength),
cvScalar(0, 0, 255, 0), 8, 4);
/**
* right bottom
*/
cv::line(image, cvPoint(squares[i][1].x,
squares[i][1].y),
cvPoint(squares[i][1].x - lineLength,
squares[i][1].y), cvScalar(0, 0, 255, 0),
8, 4);
cv::line(image, cvPoint(squares[i][1].x,
squares[i][1].y),
cvPoint(squares[i][1].x,
squares[i][1].y + lineLength),
cvScalar(0, 0, 255, 0), 8, 4);
/**
* left bottom
*/
cv::line(image, cvPoint(squares[i][2].x,
squares[i][2].y),
cvPoint(squares[i][2].x,
squares[i][2].y - lineLength),
cvScalar(255, 255, 255, 0), 8, 4);
cv::line(image, cvPoint(squares[i][2].x,
squares[i][2].y),
cvPoint(squares[i][2].x - lineLength,
squares[i][2].y),
cvScalar(255, 255, 255, 0), 8, 4);
/**
* left top
*/
cv::line(image, cvPoint(squares[i][3].x,
squares[i][3].y),
cvPoint(squares[i][3].x + lineLength,
squares[i][3].y),
cvScalar(255, 255, 255, 0), 8, 4);
cv::line(image, cvPoint(squares[i][3].x,
squares[i][3].y),
cvPoint(squares[i][3].x,
squares[i][3].y - lineLength),
cvScalar(255, 255, 255, 0), 8, 4);
// selectFeatureBounds(image, cvPoint(squares[i][3].x,
// squares[i][3].y),
// cvPoint(squares[i][1].x,
// squares[i][1].y), image.size);
}
}
void main(JNIEnv *, jobject, jlong addrGray,jlong addrRgba) {
Mat &image = *(Mat *) addrRgba;
vector<vector<Point> > squares;
Mat newImage = image.clone();
float scale = 0.25;
resize(image, newImage, Size(), scale, scale);
findSquares(newImage, squares, scale);
drawSquares(image, squares);
newImage.release();
}
}