-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseline_segmenter.cpp~
202 lines (165 loc) · 6.23 KB
/
baseline_segmenter.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
//#include <opencv2/imgproc/imgproc.hpp>
#include <segmentation_and_tracking/scene.h>
#include <segmentation_and_tracking/hand_segmenter_view_controller.h>
#include <utility>
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace std;
namespace bfs = boost::filesystem;
static const int INF = 9999999;
static const int DEBUG = 2;
string usageString()
{
ostringstream oss;
oss << "Usage: baseline_segmenter DATA_DIR" << endl;
return oss.str();
}
void drawBoxOnScene(Scene& sc, cv::Rect& bounding_box);
void segmentObjects(Scene& sc, vector<cv::Mat>& histograms) {
// Convert the scene to hsv format and extract the hue channel.
// Calculate back projection using each histogram in 'histograms'
// vector
// Use camShift to determine rectangle of the tracked objects
// Draw rectangle on the image
/* vector<TrackedObject> tracked_objects(RGBRange.size());
Eigen::MatrixXi cam_points = sc.cam_points_;
for (size_t i = 0; i < cam_points.rows(); ++i) {
cv::Point point(cam_points(i, 0), cam_points(i, 1));
cv::Vec3b point_color = sc.img_.at<cv::Vec3b>(point);
for (size_t j = 0; j < RGBRange.size(); ++j) {
if (inRGBRange(point_color, RGBRange[j].first, RGBRange[j].second)) {
tracked_objects[j].indices_.push_back(i);
/*cout << "Adding point (" << cam_points(i,0) << ", " << cam_points(i,1)
<<") to object " << j << endl;
}
}
}
for (size_t i = 0; i < tracked_objects.size(); ++i) {
sc.addTrackedObject(tracked_objects[i]);
}
sc.saveSegmentation();*/
}
cv::Rect findBoundingBox(TrackedObject& object,
Scene& sc) {
object.generateImageCoords(sc);
int min_x = INF, min_y = INF;
int max_x = -1, max_y = -1;
vector<cv::Point>& image_coords = object.image_coords_;
cout << "image_coords size: " << image_coords.size();
for (size_t i = 0; i < image_coords.size(); ++i) {
if(DEBUG==1)cout << "(" << image_coords[i].x << "," << image_coords[i].y
<< ")" << " ";
if (image_coords[i].x > max_x) {
max_x = image_coords[i].x;
}
if (image_coords[i].x < min_x) {
min_x = image_coords[i].x;
}
if (image_coords[i].y > max_y) {
max_y = image_coords[i].y;
}
if (image_coords[i].y < min_y) {
min_y = image_coords[i].y;
}
}
return cv::Rect(min_x, min_y, max_x - min_x, max_y - min_y);
}
cv::Rect camShiftTracking(cv::Rect originalBox, cv::Mat originalImage, Scene& sc)
{
//tracking algorithms camshift
cv::Rect trackBox = originalBox;
int hsize = 16;
float hranges[] = {0,180};
const float* phranges = hranges;
cv::Rect trackWindow = originalBox; // start from the previous tracking windows
cv::RotatedRect trackRotatedBox;
int vmin = 10, vmax = 256, smin = 30;
cv::Mat frame, hsv, hue, mask, hist = cv::Mat::zeros(originalImage.cols, originalImage.rows, CV_8UC3), backproj;
cv::cvtColor(originalImage, hsv, CV_BGR2HSV);// from rgb to hsv
cv::inRange(hsv, cv::Scalar(0, smin, MIN(vmin,vmax)),
cv::Scalar(180, 256, MAX(vmin, vmax)), mask);
int ch[] = {0, 0};
hue.create(hsv.size(), hsv.depth());
cv::mixChannels(&hsv, 1, &hue, 1, ch, 1);// copy h from hsv to hue
cv::Mat roi(hue, originalBox);
calcHist(&roi, 1, 0, cv::Mat(), hist, 1, &hsize, &phranges);
normalize(hist, hist, 0, 255, CV_MINMAX);
cv::calcBackProject(&hue, 1, 0, hist, backproj, &phranges);
//backproj &= mask;// do
trackRotatedBox = cv::CamShift(backproj, trackWindow,
cv::TermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ));
trackBox = trackRotatedBox.boundingRect();
if(DEBUG==2)printf("\n## originRect x,y,width,height %d %d %d %d; targetRect x,y,width,height %d %d %d %d\n",
originalBox.x, originalBox.y, originalBox.width, originalBox.height,
trackBox.x, trackBox.y, trackBox.width, trackBox.height);
return trackBox;
}// end of CamShiftTracking
void calcObjectHistograms(vector<TrackedObject>& objects,
Scene& sc,
vector<cv::Mat>* histograms) {
for (size_t i = 0; i < objects.size(); ++i) {
cv::Rect bounding_box = findBoundingBox(objects[i], sc);
//drawBoxOnScene(sc, bounding_box);
if(DEBUG==1)
cout << "bounding box for object " << i
<< "(" << bounding_box.x << ", " << bounding_box.y << ")"
<< "(" << bounding_box.x + bounding_box.width
<< ", " << bounding_box.y + bounding_box.height << ")" << endl;
}
}
void drawBoxOnScene(Scene& sc, cv::Rect& bounding_box)
{
rectangle(sc.img_,
cv::Point(bounding_box.x, bounding_box.y),
cv::Point(bounding_box.x + bounding_box.width,
bounding_box.y + bounding_box.height),
cv::Scalar(0, 0, 255));
}
int main(int argc, char** argv)
{
if(argc != 2) {
cout << usageString();
return 1;
}
string dirpath = argv[1];
if(!bfs::exists(dirpath)) {
cout << dirpath << " does not exist." << endl;
return 1;
}
// cv::Mat img1 = cv::imread("/home/sandra/cs231a/sequence01/1288572831.002852.jpg");
// cv::namedWindow("test", CV_WINDOW_AUTOSIZE);
Sequence seq(dirpath);
// Read in seed frame data
Scene& seed_frame = *seq.getScene(0);
vector<TrackedObject>& seed_objects = seed_frame.segmentation_->tracked_objects_;
// Converts the seed image to hsv format and extract hue channel
// Calculate histograms for each tracked object in seed frame
vector<cv::Mat> histograms;
calcObjectHistograms(seed_objects, seed_frame, &histograms);
size_t i = 10;
Scene& targetframe = *seq.getScene(i);
for(int j=0; j< seed_objects.size(); j++)
{
cv::Rect originalBox = findBoundingBox(seed_objects[1], seed_frame);
cv::Rect trackedBox = camShiftTracking(originalBox, seed_frame.img_, targetframe);
drawBoxOnScene(targetframe, trackedBox);
}
cv::imwrite("/home/sandra/cs231a/test.jpg", targetframe.img_);
//double scale = 0.5;
//if(getenv("SCALE"))
// scale = atof(getenv("SCALE"));
//OpenCVView view("Image", scale);
//HandSegmenterViewController vc(&view, dirpath);
//view.setDelegate(&vc);
//vc.run();
/* for(size_t i = 1; i < seq.size(); ++i) {
Scene& sc = *seq.getScene(i);
cout << "Segmenting objects for scene " << i << "..." << endl;
segmentObjects(sc, RGBRange);
//cv::Mat overlay = sc.getDepthOverlay();
//cv::imshow("test", overlay);
//cv::waitKey(0);
}*/
return 0;
}