-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeobjects.cpp
110 lines (93 loc) · 3.45 KB
/
mergeobjects.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
#include "mergeobjects.h"
#include "widget.h"
#include <ctype.h>
#include <iostream>
#include <utility>
#include <AnnotatorLib/Annotation.h>
#include <AnnotatorLib/Commands/MergeObjects.h>
#include <AnnotatorLib/Frame.h>
#include <AnnotatorLib/Session.h>
#include <dlib/opencv.h>
#include <QDebug>
#include <QtGui/QPainter>
#include <opencv2/core/core.hpp>
#include <opencv2/video/tracking.hpp>
using namespace Annotator::Plugins;
MergeObjects::MergeObjects() {}
MergeObjects::~MergeObjects() {}
QString MergeObjects::getName() { return "MergeObjects"; }
QWidget *MergeObjects::getWidget() { return &widget; }
bool MergeObjects::setFrame(shared_ptr<Frame> frame, cv::Mat image) {
this->lastFrame = this->frame;
this->lastFrameImg = this->frameImg;
this->frame = frame;
this->frameImg = image;
return lastFrame != frame;
}
void MergeObjects::setObject(shared_ptr<Object> object) {}
shared_ptr<Object> MergeObjects::getObject() const {
return shared_ptr<Object>();
}
void MergeObjects::setLastAnnotation(shared_ptr<Annotation> annotation) {}
std::vector<shared_ptr<Commands::Command>> MergeObjects::getCommands() {
std::vector<shared_ptr<Commands::Command>> commands;
if (frame == nullptr || lastFrame == nullptr ||
lastFrame->getId() != frame->getId() - 1)
return commands;
try {
dlib::cv_image<dlib::bgr_pixel> cvimg(this->lastFrameImg);
for (auto &pair : lastFrame->getAnnotations()) {
auto annotation = pair.second.lock();
if (annotation->isLast()) {
tracker.start_track(
cvimg,
dlib::rectangle(annotation->getX(), annotation->getY(),
annotation->getX() + annotation->getWidth(),
annotation->getY() + annotation->getHeight()));
std::pair<cv::Rect, float> found = findObject();
for (auto &pair2 : frame->getAnnotations()) {
auto a2 = pair2.second.lock();
if (a2->isFirst()) {
cv::Rect rect(a2->getX(), a2->getY(), a2->getWidth(),
a2->getHeight());
float IoU = intersectionOverUnion(found.first, rect);
if (IoU > threshold) {
shared_ptr<Commands::MergeObjects> mO =
std::make_shared<Commands::MergeObjects>(
project->getSession(), annotation->getObject(),
a2->getObject());
commands.push_back(mO);
}
}
}
}
}
} catch (std::exception &e) {
qDebug() << e.what();
}
return commands;
}
void MergeObjects::setThreshold(float threshold) {
this->threshold = threshold;
}
// source:
// https://putuyuwono.wordpress.com/2015/06/26/intersection-and-union-two-rectangles-opencv/
float MergeObjects::intersectionOverUnion(cv::Rect &r1, cv::Rect &r2) {
cv::Rect rectIntersection = r1 & r2;
cv::Rect rectUnion = r1 | r2;
return 1.0f * rectIntersection.area() / rectUnion.area();
}
std::pair<cv::Rect, float> MergeObjects::findObject() {
auto ret = std::make_pair(cv::Rect(), 0.0f);
dlib::cv_image<dlib::bgr_pixel> cvimg(this->frameImg);
try {
ret.second = tracker.update(cvimg); // returns the peak to side-lobe
// ratio. This is a number that
// measures how
dlib::rectangle found = tracker.get_position();
ret.first =
cv::Rect(found.left(), found.top(), found.width(), found.height());
} catch (std::exception &e) {
}
return ret;
}