Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamshift.cpp
More file actions
Latest commit
130 lines (100 loc) · 3.64 KB
/
Copy pathcamshift.cpp
File metadata and controls
130 lines (100 loc) · 3.64 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
#include"camshift.h"
#include"widget.h"
#include<AnnotatorLib/Annotation.h>
#include<AnnotatorLib/Commands/NewAnnotation.h>
#include<AnnotatorLib/Frame.h>
#include<AnnotatorLib/Session.h>
#include<QDebug>
#include<QtGui/QPainter>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/video/tracking.hpp>
#include<ctype.h>
#include<iostream>
usingnamespaceAnnotator::Plugins;
Annotator::Plugins::CamShift::CamShift() {
hranges[0] = 0.0;
hranges[1] = 180.0;
phranges = hranges;
}
CamShift::~CamShift() {}
QString CamShift::getName() { return"CamShift"; }
QWidget *CamShift::getWidget() { return &widget; }
boolCamShift::setFrame(shared_ptr<Frame> frame, cv::Mat image) {
this->lastFrame = this->frame;
this->frame = frame;
this->frameImg = image;
return lastFrame != frame;
}
// first call
voidCamShift::setObject(shared_ptr<Object> object) {
if (object != this->object) {
this->object = object;
this->trackerStarted = false;
}
}
shared_ptr<Object> CamShift::getObject() const { return object; }
// second call
voidCamShift::setLastAnnotation(shared_ptr<Annotation> annotation) {
if (trackerStarted || annotation == nullptr ||
annotation->getObject() != object)
return;
if (lastAnnotation != nullptr &&
annotation->getObject() == lastAnnotation->getObject())
return;
this->lastAnnotation = annotation;
selection = cv::Rect(lastAnnotation->getX(), lastAnnotation->getY(),
lastAnnotation->getWidth(), lastAnnotation->getHeight());
this->lastFrame = this->lastAnnotation->getFrame();
newSelection = true;
trackerStarted = true;
}
std::vector<shared_ptr<Commands::Command>> CamShift::getCommands() {
std::vector<shared_ptr<Commands::Command>> commands;
if (object == nullptr || frame == nullptr || lastFrame == nullptr ||
this->lastAnnotation == nullptr || frame == lastFrame)
return commands;
try {
cv::Rect found_rect = findObject();
int w = found_rect.width;
int h = found_rect.height;
int x = found_rect.x;
int y = found_rect.y;
shared_ptr<Commands::NewAnnotation> nA =
std::make_shared<Commands::NewAnnotation>(getProject()->getSession(),
lastAnnotation->getObject(),
frame, x, y, w, h);
commands.push_back(nA);
} catch (std::exception &e) {
qDebug() << e.what();
}
return commands;
}
cv::Rect CamShift::findObject() {
cv::cvtColor(frameImg, hsv, cv::COLOR_BGR2HSV);
cv::inRange(hsv, cv::Scalar(0, smin, std::min(vmin, vmax)),
cv::Scalar(180, 256, std::max(vmin, vmax)), mask);
int ch[2] = {0, 0};
hue.create(hsv.size(), hsv.depth());
cv::mixChannels(&hsv, 1, &hue, 1, ch, 1);
if (newSelection) {
selection &= cv::Rect(0, 0, frameImg.cols, frameImg.rows);
cv::Mat roi(hue, selection), maskroi(mask, selection);
cv::calcHist(&roi, 1, 0, maskroi, hist, 1, &hsize, &phranges);
cv::normalize(hist, hist, 0, 255, CV_MINMAX);
trackWindow = selection;
newSelection = false;
}
cv::calcBackProject(&hue, 1, 0, hist, backproj, &phranges);
backproj &= mask;
cv::TermCriteria criteria(cv::TermCriteria::COUNT | cv::TermCriteria::EPS, 10,
1);
cv::RotatedRect found_object = cv::CamShift(backproj, trackWindow, criteria);
// debug
cv::ellipse(frameImg, found_object, cv::Scalar(0, 0, 255), 3, CV_AA);
// cv::rectangle(frameImg, found_object.boundingRect(), cv::Scalar(0,0,255),
// 3, CV_AA);
// cv::imshow("CamShift Debug", frameImg);
return found_object.boundingRect();
}