-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
403 lines (342 loc) · 11.5 KB
/
main.cpp
File metadata and controls
403 lines (342 loc) · 11.5 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include <opencv2/core/core.hpp>
#include <opencv2/video/video.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
#include <iostream>
#include <Python.h>
using namespace std;
using namespace cv;
Point fishEye1,fishEye2,fishCenter, fishHead;
Mat cur_img,org_img;
bool draw;
Mat roi;
Point tailPt_a, tailPt_b, topEnd;
Point cursor;
int threshold_val = 22;
Rect rect;
/*
class UserInterface
{
public:
static void on_trackbar_setThreshold(int, void*) {
int max_val = 255;
Mat binaryzation = Mat::zeros(cur_img.size(), CV_8UC1);
threshold(cur_img, binaryzation, threshold_val, max_val, CV_THRESH_BINARY);
imshow("setThreshold", binaryzation);
}
bool setThreshold(Mat org) {
namedWindow("setThreshold", CV_WINDOW_NORMAL);
createTrackbar("Threshold", "setThreshold", &threshold_val, 255, on_trackbar_setThreshold);
on_trackbar_setThreshold(threshold_val, 0);
while (char(waitKey(1)) != 'q') {}
return true;
}
void on_mouse_findHeadAndCenter(int event, int x, int y, int flags, void* ustc) {
if (event == CV_EVENT_LBUTTONDBLCLK) {
fishEye1 = Point(x, y);
circle(cur_img, fishEye1, 2, Scalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0);
imshow("findHeadAndCenter", cur_img);
}
else if (event == CV_EVENT_RBUTTONDBLCLK) {
fishEye2 = Point(x, y);
fishHead = (fishEye1 + fishEye2) / 2;
circle(cur_img, fishHead, 2, Scalar(0, 0, 0, 255), CV_FILLED, CV_AA, 0);
circle(cur_img, fishEye2, 2, Scalar(0, 255, 0, 0), CV_FILLED, CV_AA, 0);
imshow("findHeadAndCenter", cur_img);
}
else if (event == CV_EVENT_MBUTTONDOWN) {
fishCenter = Point(x, y);
circle(cur_img, fishCenter, 2, Scalar(0, 0, 255, 0), CV_FILLED, CV_AA, 0);
imshow("findHeadAndCenter", cur_img);
cout << "fishCenter:" << fishCenter.x << ',' << fishCenter.y << endl;
cout << "fishHead:" << fishHead.x << ',' << fishHead.y << endl;
}
}
bool findHeadAndCenter(Mat org) {
namedWindow("findHeadAndCenter", CV_WINDOW_NORMAL);
imshow("findHeadAndCenter", org);
setMouseCallback("findHeadAndCenter", on_mouse_findHeadAndCenter, 0);
while (char(waitKey(1)) != 'q') {}
return true;
}
};
*/
static void on_trackbar_setThreshold(int, void*) {
int max_val = 255;
Mat binaryzation = Mat::zeros(cur_img.size(), CV_8UC1);
threshold(cur_img, binaryzation, threshold_val, max_val, CV_THRESH_BINARY);
imshow("setThreshold", binaryzation);
}
bool setThreshold() {
namedWindow("setThreshold", CV_WINDOW_NORMAL);
createTrackbar("Threshold", "setThreshold", &threshold_val, 255, on_trackbar_setThreshold);
on_trackbar_setThreshold(threshold_val, 0);
cout << "Press 'q' to exit." << endl;
while (char(waitKey(1)) != 'q') {}
return true;
}
void on_mouse_findHeadAndCenter(int event, int x, int y, int flags, void* ustc) {
if (event == CV_EVENT_LBUTTONDBLCLK) {
fishEye1 = Point(x, y);
circle(cur_img, fishEye1, 2, Scalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0);
imshow("findHeadAndCenter", cur_img);
}
else if (event == CV_EVENT_RBUTTONDBLCLK) {
fishEye2 = Point(x, y);
fishHead = (fishEye1 + fishEye2) / 2;
circle(cur_img, fishHead, 2, Scalar(0, 0, 0, 255), CV_FILLED, CV_AA, 0);
circle(cur_img, fishEye2, 2, Scalar(0, 255, 0, 0), CV_FILLED, CV_AA, 0);
imshow("findHeadAndCenter", cur_img);
}
else if (event == CV_EVENT_MBUTTONDOWN) {
fishCenter = Point(x, y);
circle(cur_img, fishCenter, 2, Scalar(0, 0, 255, 0), CV_FILLED, CV_AA, 0);
imshow("findHeadAndCenter", cur_img);
cout << "fishCenter:" << fishCenter.x << ',' << fishCenter.y << endl;
cout << "fishHead:" << fishHead.x << ',' << fishHead.y << endl;
}
}
bool findHeadAndCenter() {
namedWindow("findHeadAndCenter", CV_WINDOW_NORMAL);
imshow("findHeadAndCenter", cur_img);
setMouseCallback("findHeadAndCenter", on_mouse_findHeadAndCenter, 0);
cout << "Double-click the left mouse button to select one eye." << endl;
cout << "Double-click the right mouse button to select another eye." << endl;
cout << "Click the middle mouse button to select center." << endl;
cout << "Press 'q' to exit." << endl;
while (char(waitKey(1)) != 'q') {}
return true;
}
/*Find the closest point on the contour to the reference point, return the index findClosestPt*/
int findClosestPt(vector<Point>& contour, Point point)
{
double minDist = 1000000;
double tempDist = 0;
Point temp;
int goodIndex = 0;
for (int i = 0; i < contour.size(); i++)
{
temp = contour[i];
tempDist = norm(contour[i] - point);
if (tempDist < minDist)
{
goodIndex = i;
minDist = tempDist;
}
}
return goodIndex;
}
/* Get the Euclidean distance from point P to line AB */
double getPt2LineDistance(Point2f P, Point2f A, Point2f B)
{
Point2f BA = B - A; // the vector from A to B
//Point2f PA = P - A; // the vector from A to P
double dist = abs(BA.y * P.x - BA.x * P.y + B.cross(A)) / norm(BA);
return dist;
}
/* Find 2 intersection points of a line (AB) and contour */
vector<int> findPtsLineIntersectContour(vector<Point> & contour, Point2f A, Point2f B)
{
vector<int> goodIndices(2);
Point2f curPt; // current point
vector<Point> ptList; // store potential intersection points
vector<int> idxList; // store indices of potential intersection points
double distThre = 1.0; // threshold to decide whether it is an intersection pt
for (int i = 0; i < contour.size(); i++)
{
curPt = contour[i];
double pt2line = getPt2LineDistance(curPt, A, B);
if (pt2line < distThre)
{
ptList.push_back(contour[i]);
idxList.push_back(i);
}
}
// assign intersection points to each side
int idxA = findClosestPt(ptList, A); // the one closest to A
int idxB = findClosestPt(ptList, B); // the one closest to B
if (idxA < idxB)
{
goodIndices[0] = idxList[idxA];
goodIndices[1] = idxList[idxB];
}
else
{
goodIndices[0] = idxList[idxB];
goodIndices[1] = idxList[idxA];
}
return goodIndices;
}
/*This function return a radian to describe the fishtailing motion */
bool fishAngleAnalysis(Mat fishImg, Point fishHead, Point fishCenter, Point * fishTail_return, double* fishAngle,int threshold_val) {
//Find the contour of fish
Mat binaryzation;
double max_val = 255, maxFishArea = 15000, minFishArea = 2000;
vector<vector<Point>> allContours, fishContours;
threshold(fishImg, binaryzation, threshold_val, max_val, CV_THRESH_BINARY);
findContours(binaryzation, allContours, CV_RETR_LIST, CHAIN_APPROX_NONE);
for (int i = 0; i < allContours.size(); i++) {
if (contourArea(allContours[i]) < maxFishArea && contourArea(allContours[i]) > minFishArea)
fishContours.push_back(allContours[i]);
}
if (fishContours.size() != 1) {
cout << "Can't find contour of fish!Area of all contours:";
for (int i = 0; i < allContours.size(); i++) {
cout << contourArea(allContours[i]) << ',';
}
cout << endl;
return false;
}
//Find the tail of fish
double Pt2center = norm(fishContours[0][0] - fishCenter);
topEnd = fishContours[0][0];
for (int i = 1; i < fishContours[0].size(); i++)
{
double curPt2center = norm(fishContours[0][i] - fishCenter);
if (Pt2center < curPt2center) {
topEnd = fishContours[0][i];
Pt2center = curPt2center;
circle(fishImg, topEnd, 1, Scalar(255), -1);
}
}
Point tailAxis = topEnd - fishCenter;
tailPt_a = fishCenter + tailAxis * 9 / 10 + Point(tailAxis.y, -tailAxis.x)/4;
tailPt_b = fishCenter + tailAxis * 9 / 10 + Point(-tailAxis.y, tailAxis.x)/4;
vector<int> fishTail = findPtsLineIntersectContour(fishContours[0], tailPt_a, tailPt_b);
//Calculate the angle
Point fishHeadVector, fishTailVector;
fishHeadVector = fishCenter - fishHead;
fishTailVector = (fishContours[0][fishTail[0]]+ fishContours[0][fishTail[1]])/2 - fishCenter;
double sinfi;
sinfi = -(fishHeadVector.x * fishTailVector.y - fishTailVector.x * fishHeadVector.y) / (norm(fishHeadVector) * norm(fishTailVector));
*fishAngle = asin(sinfi);
*fishTail_return = (fishContours[0][fishTail[0]] + fishContours[0][fishTail[1]]) / 2;
//drawContours(fishImg, fishContours, -1, Scalar(255),2);
//imshow("contour", fishImg);
return true;
}
int predict_left(double* boutStart) {
Py_Initialize();
if (Py_IsInitialized() == 0) {
cout << "Py_Initialize failed." << endl;
}
PyObject* pModule = PyImport_ImportModule("predict");
if (pModule == NULL)
cout << "Py_ImportModule failed." << endl;
PyObject * pFunc = PyObject_GetAttrString(pModule, "predict_left");
PyObject * PyList = PyList_New(40);
PyObject * ArgList = PyTuple_New(1);
for (int Index_i = 0; Index_i < PyList_Size(PyList); Index_i++) {
PyList_SetItem(PyList, Index_i, PyFloat_FromDouble(boutStart[Index_i]));
}
PyTuple_SetItem(ArgList, 0, PyList);
PyObject* pReturn = NULL;
pReturn = PyObject_CallObject(pFunc, ArgList);
int result;
PyArg_Parse(pReturn, "i", &result);
cout << "predict:" << result << endl;
Py_Finalize();
return result;
}
bool fishAngleAnalysis_test(String fishVideoAddress, bool isGrey) {
VideoCapture capture(fishVideoAddress);
Point testTail;
Mat curImg;
VideoWriter writer;
writer.open("F:\\fishData\\new_stream5.avi", CV_FOURCC('D', 'I', 'V', 'X'), 25.0, Size(400, 400), true);
namedWindow("output", CV_WINDOW_NORMAL);
//namedWindow("org", CV_WINDOW_NORMAL);
capture >> curImg;
//findFishHeadAndCenter(curImg);
//getchar();
for (int i = 0; i < 780; i++) {
capture >> curImg;
}
int n = 0;
int checkPoint=0;
int boutStart = 0;
int predict;
for (int i = 0; i < 100; i++) {
Mat grey;
Point fishTail = Point(-1, -1);
double fishAngle[10000];
capture >> curImg;
if (!isGrey) {
cvtColor(curImg, grey, CV_BGR2GRAY);
if (!fishAngleAnalysis(grey, fishHead, fishCenter, &fishTail, &fishAngle[i], threshold_val)) {
cout << "AngleAnalysis error!" << endl;
return false;
}
}
else {
if (!fishAngleAnalysis(curImg, fishHead, fishCenter, &fishTail, &fishAngle[i], threshold_val)) {
cout << "AngleAnalysis error!" << endl;
return false;
}
}
/*
if (n == checkPoint) {
if (boutStart > 0) {
predict=predict_left(&fishAngle[boutStart]);
cout << "predict:" << predict << endl;
boutStart = 0;
}
if (fishAngle[i] > 0.2|| fishAngle[i] < -0.2) {
boutStart = i - 4;
checkPoint = checkPoint + 40;
cout << "Find bout!" << endl;
}
}
else
n++;
*/
cout << "fishAngle:" << fishAngle[i] << endl;
circle(curImg, fishHead, 1, Scalar(0, 0, 0, 255), -1);
circle(curImg, fishCenter, 1, Scalar(0, 0, 255, 0), -1);
circle(curImg, fishTail, 1, Scalar(255, 0, 0), -1);
circle(curImg, tailPt_a, 1, Scalar(255, 0, 0), -1);
circle(curImg, tailPt_b, 1, Scalar(255, 0, 0), -1);
circle(curImg, topEnd, 1, Scalar(255, 0, 0), -1);
line(curImg, fishHead, fishCenter, Scalar(0, 255, 0));
line(curImg, fishTail, fishCenter, Scalar(0, 255, 0));
imshow("output", curImg);
writer << curImg;
//waitKey();
}
writer.release();
return true;
}
bool videoEditing(String fishVideoAddress, int start_frame) {
VideoCapture cap(fishVideoAddress);
float fps = cap.get(CV_CAP_PROP_FPS);
long framewidth = cap.get(CV_CAP_PROP_FRAME_WIDTH);
long framehigh = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
float framecount = cap.get(CV_CAP_PROP_FRAME_COUNT);
VideoWriter writer;
writer.open("F:/fishData/new_stream1.mpg", CV_FOURCC('D', 'I', 'V', 'X'), 10, Size(248, 250), false);
int i = 0;
while (1) {
Mat frame;
cap >> frame;
i++;
if (frame.empty())break;
if (i >= start_frame) {
writer << frame;
}
}
return true;
}
int main() {
//
VideoCapture capture("F://fishData//test5.avi");
Point testTail;
Mat curImg;
capture >> curImg;
curImg.copyTo(cur_img);
findHeadAndCenter();
setThreshold();
fishAngleAnalysis_test("F://fishData//test5.avi", false);
waitKey();
//videoEditing("F://fishData//test3.avi", 1400);
}