-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgb.hpp
235 lines (184 loc) · 6.02 KB
/
rgb.hpp
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
#include "stdafx.h"
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
using namespace std;
#include "stdafx.h"
int width = 640;
int height = 480;
Mat image(cv::Size(640, 480), CV_8UC3);
Mat hsv, hue, mask, histimg;
IplImage *img_out;
Mat hist;
bool selectObject = false;
bool backprojMode = false;
int trackObject = 0;
bool showHist = true;
Point origin;
Rect selection;
Rect trackWindow, trackWindow_temp;
RotatedRect trackBox; // tracking 返回带角度的區域 box
CvConnectedComp track_comp;
int hdims = 80; // 劃分HIST的個數,越高越精確
int hsize = 16;
float hranges[] = {0, 180};
const float *phranges = hranges;
int vmin = 10, vmax = 256, smin = 30;
Point mousePosition; //用來儲存 camshift 得到的 track_box.center.x and y
Point predict_pt; // kalman 的預測座標
const int winHeight = 640; //採集到的影像大小
const int winWidth = 480;
bool bOnceSave = true; //保存數據只執行一次
int minWidth = 0; //保存初始化時,跟蹤地矩形框大小,之後跟蹤的矩形框不能小於這個
int minHeight = 0;
//----------------------------------------
Point NowCursorPos; //存放當前的滑鼠座標
//Point OldCursorPos;
Point OldBox; //跟踪矩形框
Point NowBox;
int iOldSize = 0; //保存第一次運行的矩陣面積
int iNowSize = 0;
int iframe = 0; //統計禎數,每3張帧数进行一次跟踪座標的計算,獲取一次當時滑鼠位置,然後計算
//---------------------------------------
void onMouse(int event, int x, int y, int, void *) //定義滑鼠點擊
{
if (selectObject) //當左鍵按下時開始圈選
{ //矩形大小定位
selection.x = MIN(x, origin.x); //讓滑鼠左右拉都可,但origin的話只可右拉
selection.y = MIN(y, origin.y);
selection.width = abs(x - origin.x);
selection.height = abs(y - origin.y);
selection &= Rect(0, 0, image.cols, image.rows); //確保矩形位在視窗中
}
switch (event)
{
case EVENT_LBUTTONDOWN: //左鍵按下
origin = Point(x, y);
selection = Rect(x, y, 0, 0);
selectObject = true;
break;
case EVENT_LBUTTONUP: //左鍵離開
selectObject = false;
if (selection.width > 0 && selection.height > 0)
trackObject = -1;
break;
}
}
CvScalar hsv2rgb(float hue)
{
int rgb[3], p, sector;
static const int sector_data[][3] =
{{0, 2, 1}, {1, 2, 0}, {1, 0, 2}, {2, 0, 1}, {2, 1, 0}, {0, 1, 2}};
hue *= 0.033333333333333333333333333333333f;
sector = cvFloor(hue);
p = cvRound(255 * (hue - sector));
p ^= sector & 1 ? 255 : 0;
rgb[sector_data[sector][0]] = 255;
rgb[sector_data[sector][1]] = 0;
rgb[sector_data[sector][2]] = p;
#ifdef _DEBUG
printf("\n # Convert HSV to RGB:");
printf("\n HUE = %f", hue);
printf("\n R = %d, G = %d, B = %d", rgb[0], rgb[1], rgb[2]);
#endif
return cvScalar(rgb[2], rgb[1], rgb[0], 0);
}
//讀Red初始化圖片,以便進行tracking
bool loadTemplateImage_R()
{
Mat tempimage = imread("d:/red.png");
if (!tempimage.data)
{
return false;
}
cvtColor(tempimage, hsv, CV_BGR2HSV);
int _vmin = vmin, _vmax = vmax;
inRange(hsv, Scalar(0, smin, MIN(_vmin, _vmax), 0), Scalar(180, 256, MAX(_vmin, _vmax), 0), mask);
Mat chan[3];
split(hsv, chan);
int ch[] = {0, 0};
mixChannels(&hsv, 1, &hue, 1, ch, 1);
selection.x = 1;
selection.y = 1;
selection.width = winHeight - 1; //640:480
selection.height = winWidth - 1;
Mat roi(hue, selection); //得到ROI的選擇區域
Mat maskroi(mask, selection); //mask保存的是hsv的最小值
calcHist(&tempimage, 1, 0, maskroi, hist, 1, &hsize, &phranges);
normalize(hist, hist, 0, 255, CV_MINMAX);
trackWindow = selection;
trackObject = 1;
return true;
}
//讀取Green初始化圖片,以便進行tracking
bool loadTemplateImage_G()
{
Mat tempimage = imread("d:/green.png", 1);
if (!tempimage.data)
{
return false;
}
cvtColor(tempimage, hsv, CV_BGR2HSV);
int _vmin = vmin, _vmax = vmax;
inRange(hsv, Scalar(0, smin, MIN(_vmin, _vmax), 0), Scalar(180, 256, MAX(_vmin, _vmax), 0), mask);
Mat chan[3];
//int chan[]={0,0,0};
split(hsv, chan);
int ch[] = {0, 0};
mixChannels(&hsv, 1, &hue, 1, ch, 1);
selection.x = 1;
selection.y = 1;
selection.width = winHeight - 1; //640:480
selection.height = winWidth - 1;
Mat roi(hue, selection); //得到ROI的選擇區域
Mat maskroi(mask, selection); //mask保存的是hsv的最小值
calcHist(&tempimage, 1, 0, maskroi, hist, 1, &hsize, &phranges);
normalize(hist, hist, 0, 255, CV_MINMAX);
trackWindow = selection;
trackObject = 1;
return true;
}
//讀取Blue的初始化圖片,以便進行tracking
bool loadTemplateImage_B()
{
Mat tempimage = imread("d:/blue.png", 1);
if (!tempimage.data)
{
return false;
}
cvtColor(tempimage, hsv, CV_BGR2HSV);
int _vmin = vmin, _vmax = vmax;
inRange(hsv, Scalar(0, smin, MIN(_vmin, _vmax), 0), Scalar(180, 256, MAX(_vmin, _vmax), 0), mask);
Mat chan[3];
//int chan[]={0,0,0};
*(Mat_<float>(4, 4) << 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1);
split(hsv, chan);
int ch[] = {0, 0};
mixChannels(&hsv, 1, &hue, 1, ch, 1);
selection.x = 1;
selection.y = 1;
selection.width = winHeight - 1; //640:480
selection.height = winWidth - 1;
Mat roi(hue, selection); //得到ROI的選擇區域
Mat maskroi(mask, selection); //mask保存的是hsv的最小值
calcHist(&tempimage, 1, 0, maskroi, hist, 1, &hsize, &phranges);
normalize(hist, hist, 0, 255, CV_MINMAX);
trackWindow = selection;
trackObject = 1;
return true;
}
//减法求絕對值的
int iAbsolute(int a, int b)
{
int c = 0;
if (a > b)
{
c = a - b;
}
else
{
c = b - a;
}
return c;
}