-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViBe_M.cpp
366 lines (336 loc) · 11.4 KB
/
ViBe_M.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
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
#include"ViBe_M.h"
/*
1. cv::Mat -> IplImage
cv::Mat matimg = cv::imread ("heels.jpg");
IplImage* iplimg;
*iplimg = IplImage(matimg);
2. IplImage -> cv::Mat
IplImage* iplimg = cvLoadImage("heels.jpg");
cv::Mat matimg;
matimg = cv::Mat(iplimg);
*/
const static int mask = 65535;
static int isModelInit = 0;
ViBe_M::~ViBe_M(){
if (ptrSample != nullptr){
}
if (pViBeSampleModel != nullptr){
free(pViBeSampleModel);
pViBeSampleModel = nullptr;
}
if (pCapture != nullptr){
cvReleaseCapture(&pCapture);
pCapture = nullptr;
}
if (pGrayImage != nullptr){
cvReleaseImage(&pGrayImage);
pGrayImage = nullptr;
}
if (pFrame != nullptr){
cvReleaseImage(&pFrame);
pFrame = nullptr;
}
if (pGrayForeImage != nullptr){
cvReleaseImage(&pGrayForeImage);
pGrayForeImage = nullptr;
}
}
void ViBe_M::initParams2(const cv::Mat &frame,const Config& config){
//cv::cvtColor(frame, frame, COLOR_RGB2GRAY);
this->imageChannels = frame.channels();
this->imageType = frame.type();
this->imageHeight = frame.rows;
this->imageWidth = frame.cols;
this->oneSampleSize = imageWidth*imageHeight;
//this->oneSampleSize = imageChannels*imageHeight*imageWidth;
this->numOfsample = config.numOfSample;
this->minMatchCount = config.minMatchCount;
this->foregroundCount = config.foregroundCount;
this->radius = config.radius;
this->lastModelSampleIndex = imageHeight*imageWidth*numOfsample;
sample = Mat::zeros(imageWidth*imageHeight*(numOfsample+1), 1, CV_8UC1);
ptrSample = sample.data;
foreImage.create(imageHeight, imageWidth, CV_8UC1);
}
void ViBe_M::InitSampleModelData2(const cv::Mat &image){
for (size_t i = 0; i < numOfsample; i++){
memcpy(ptrSample + oneSampleSize*i, image.data, oneSampleSize);
}
}
void ViBe_M::update2(const cv::Mat &src, cv::Mat &foreImage){
size_t nOffset = 0;//偏移量
size_t nDistance = 0;//当前帧与背景模型的差值
size_t nMatchCount = 0;
static uchar CountNum = 0;
uchar* pCurrentImage = src.data;
uchar* pForegoundImage = foreImage.data;
CountNum++;
if (CountNum > 127){
CountNum = 0;
}
//1.初始化背景模型,没有采用8邻域随机20采样,而是采用了仅采用了同一帧的8个copy,相当于每一个像素复制了8份作为其背景样本集
if (!isModelInit){
InitSampleModelData2(src);
isModelInit = 1;
return;
}
//2.模拟随机数 0-7
//int t = (getTickCount()&mask) % numOfsample;//这样产生随机数,运动前景后会拖一堆的闪点,是更新太随机了吗?
nOffset = (CountNum >> 4)*oneSampleSize;
//3.先将元素都置为前景,再把当前元素与背景样本集进行比较,将与背景样本集相近的设为背景
for (size_t i = 0; i < oneSampleSize; i++){
nMatchCount = 0;
pForegoundImage[i] = 255;
for (size_t j = 0; j < numOfsample; j++){
nDistance = abs(ptrSample[j*oneSampleSize + i] - pCurrentImage[i]);//可以这样操作说明Mat在内存中也是连续排列的
if (nDistance <= radius){//radius
nMatchCount++;
}
if (nMatchCount >= minMatchCount){
pForegoundImage[i] = 0;
break;
}
}
}
//4.前景点计数,更行背景样本集
for (size_t i = 0; i < oneSampleSize; i++){
if (pForegoundImage[i]){//是前景,则进行前景点计数
ptrSample[lastModelSampleIndex + i]++;
if (ptrSample[lastModelSampleIndex + i] > foregroundCount){//foregroundCount
ptrSample[nOffset + i] = pCurrentImage[i];
}
}
else{//如果此点为背景,则对背景模型进行更新
ptrSample[nOffset + i] = pCurrentImage[i];
ptrSample[lastModelSampleIndex + i] = 0;
}
}
}
void ViBe_M::apply2(const cv::Mat &src, cv::Mat &dst){
if (src.data != nullptr)
{
nFrameNum++;
if (nFrameNum == 1)//第一帧时初始化参数
{
initParams2(src, ViBe_M::Config::getGrayConfig());
if (src.channels() == 3){
cvtColor(src, grayImage, CV_BGR2GRAY);
}
update2(grayImage, foreImage);
}
else
{
if (src.channels() == 3){
cvtColor(src, grayImage, CV_BGR2GRAY);
}
timeRecord = (double)cvGetTickCount();
update2(grayImage, foreImage);
timeRecord = (double)cvGetTickCount() - timeRecord;
printf("exec time = %gms\n", timeRecord / (cvGetTickFrequency() * 1000));
dst = foreImage;
if (dst.data != NULL){
//cv::imshow("dst", dst);
//dst = cvarrToMat(pGrayForeImage, false);//true 复制整个矩阵
}
else{
cout << "pGrayForeImage is NULL" << endl;
}
}
}
}
void ViBe_M::initParams(const IplImage *pFrame, const Config& config){
//CV_Assert(image.cols > 0 && image.rows > 0 && (image.type() == CV_8UC3 || image.type() == CV_8UC1));
//cout << pFrame->depth<<endl;
CV_Assert(pFrame->nChannels == 3 && pFrame->height > 0 && pFrame->width > 0 && pFrame->depth == IPL_DEPTH_8U);
this->imageChannels = pFrame->nChannels;
this->imageHeight = pFrame->height;
this->imageWidth = pFrame->width;
this->oneSampleSize = imageHeight*imageWidth;
//this->oneSampleSize = imageChannels*imageHeight*imageWidth;
this->numOfsample = config.numOfSample;
this->minMatchCount = config.minMatchCount;
this->foregroundCount = config.foregroundCount;
this->radius = config.radius;
this->lastModelSampleIndex = imageHeight*imageWidth*numOfsample;
pGrayImage = cvCreateImage(cvSize(imageWidth, imageHeight), IPL_DEPTH_8U, 1);
//pRGBImage = cvCreateImage(cvSize(imageWidth, imageHeight), IPL_DEPTH_8U, 3);
//pRGBForeImage = cvCreateImage(cvSize(imageWidth, imageHeight), IPL_DEPTH_8U, 3);
pGrayForeImage = cvCreateImage(cvSize(imageWidth, imageHeight), IPL_DEPTH_8U, 1);
//this->pViBeSampleModel = (uchar *)malloc(imageHeight*imageWidth*(numOfsample + 1)*IPL_DEPTH_8U);
this->pViBeSampleModel = (uchar *)malloc(imageHeight*imageWidth*(numOfsample + 1)*sizeof(uchar));
}
void ViBe_M::InitSampleModelData(const uchar *pImageData){
for (size_t i = 0; i < numOfsample; i++){
memcpy(pViBeSampleModel + oneSampleSize*i, pImageData, oneSampleSize);
}
}
void ViBe_M::update(const uchar *pCurrentImage, uchar *pForegoundImage){
size_t nOffset = 0;//偏移量
size_t nDistance = 0;//当前帧与背景模型的差值
size_t nMatchCount = 0;
static uchar CountNum = 0;
CountNum++;
if (CountNum > 127){
CountNum = 0;
}
//1.初始化背景模型,没有采用8邻域随机20采样,而是采用了仅采用了同一帧的8个copy,相当于每一个像素复制了8份作为其背景样本集
if (!isModelInit){
InitSampleModelData(pCurrentImage);
isModelInit = 1;
return;
}
//2.模拟随机数 0-7
//int t = (getTickCount()&mask) % numOfsample;//这样产生随机数,运动前景后会拖一堆的闪点,是更新太随机了吗?
nOffset = (CountNum>>4)*oneSampleSize;
//3.先将元素都置为前景,再把当前元素与背景样本集进行比较,将与背景样本集相近的设为背景
for (size_t i = 0; i < oneSampleSize; i++){
nMatchCount = 0;
pForegoundImage[i] = 255;//pGrayForeImage = cvCreateImage(cvSize(imageWidth, imageHeight), IPL_DEPTH_8U, 1);说明数据在内存中是线性排列的
for (size_t j = 0; j < numOfsample; j++){
nDistance = abs(pViBeSampleModel[j*oneSampleSize + i] - pCurrentImage[i]);
if (nDistance <=radius ){//radius
nMatchCount++;
}
if (nMatchCount >= minMatchCount){
pForegoundImage[i] = 0;
break;
}
}
}
//4.前景点计数,更行背景样本集
for (size_t i = 0; i < oneSampleSize; i++){
if (pForegoundImage[i]){//是前景,则进行前景点计数
pViBeSampleModel[lastModelSampleIndex + i]++;
if (pViBeSampleModel[lastModelSampleIndex + i] > foregroundCount){//foregroundCount
pViBeSampleModel[nOffset + i] = pCurrentImage[i];
}
}
else{//如果此点为背景,则对背景模型进行更新
pViBeSampleModel[nOffset + i] = pCurrentImage[i];
pViBeSampleModel[lastModelSampleIndex + i] = 0;
}
}
}
void ViBe_M::apply(const cv::Mat &src, cv::Mat &dst){
//IplImage *IplSrc;
//*pFrame = src;//直接转换会报错,真是奇了怪了,可能是没有重载运算符
//IplImage test = src;
//pFrame = &test;// pFrame是IplImage*类型
pFrame = &(IplImage)src;
if (pFrame != nullptr){
nFrameNum++;
if (nFrameNum == 1){
initParams(pFrame, ViBe_M::Config::getGrayConfig());
cvCvtColor(pFrame, pGrayImage, CV_BGR2GRAY);
update((uchar *)pGrayImage->imageData, (uchar *)pGrayForeImage->imageData);
}
else{
cvCvtColor(pFrame, pGrayImage, CV_BGR2GRAY);
timeRecord = (double)cvGetTickCount();
update((uchar *)pGrayImage->imageData, (uchar *)pGrayForeImage->imageData);
timeRecord = (double)cvGetTickCount() - timeRecord;
printf("exec time = %gms\n", timeRecord / (cvGetTickFrequency() * 1000));
if (pGrayForeImage != NULL){
dst = cvarrToMat(pGrayForeImage, false);//true 复制整个矩阵
}
else{
cout << "pGrayForeImage is NULL" << endl;
}
//imshow("dst", dst);
//cvShowImage("video", pGrayImage);
//cvShowImage("foreground", pGrayForeImage);
}
}
}
//void ViBe_M::apply(IplImage *src, IplImage *dst){//有问题,dst指针的值传不出去
// //IplImage *IplSrc;
// pFrame = src;
//
// if (pFrame != nullptr){
// nFrameNum++;
// if (nFrameNum == 1){
// initParams(pFrame, ViBe_M::Config::getGrayConfig());
// cvCvtColor(pFrame, pGrayImage, CV_BGR2GRAY);
// update((uchar *)pGrayImage->imageData, (uchar *)pGrayForeImage->imageData);
// }
// else{
// cvCvtColor(pFrame, pGrayImage, CV_BGR2GRAY);
// timeRecord = (double)cvGetTickCount();
// update((uchar *)pGrayImage->imageData, (uchar *)pGrayForeImage->imageData);
// timeRecord = (double)cvGetTickCount() - timeRecord;
// printf("exec time = %gms\n", timeRecord / (cvGetTickFrequency() * 1000));
//
// //cvCreateImage(cvSize(imageWidth, imageHeight), IPL_DEPTH_8U, 1);
// //内存在函数内部分配,函数返回时被释放了,所以传不出去值
// dst = cvCreateImage(cvSize((*pGrayForeImage).width, (*pGrayForeImage).height), pGrayForeImage->depth, pGrayForeImage->nChannels);
// *dst = *pGrayForeImage;//改变指针地址的值还是传不出值
// //IplImage* img=cvLoadImage("");
// //IplImage *temp=&dst;
// //temp = pGrayForeImage;
// //cvShowImage("video", pGrayImage);
// //cvShowImage("foreground", pGrayForeImage);
// cvShowImage("foreground2", dst);
// }
// }
//}
IplImage* ViBe_M::apply(IplImage *src){//直接将采用pGrayForeImage的值作为返回值
//IplImage *IplSrc;
pFrame = src;
if (pFrame != nullptr){
nFrameNum++;
if (nFrameNum == 1){
initParams(pFrame, ViBe_M::Config::getGrayConfig());
cvCvtColor(pFrame, pGrayImage, CV_BGR2GRAY);
update((uchar *)pGrayImage->imageData, (uchar *)pGrayForeImage->imageData);
}
else{
cvCvtColor(pFrame, pGrayImage, CV_BGR2GRAY);
timeRecord = (double)cvGetTickCount();
update((uchar *)pGrayImage->imageData, (uchar *)pGrayForeImage->imageData);
timeRecord = (double)cvGetTickCount() - timeRecord;
printf("exec time = %gms\n", timeRecord / (cvGetTickFrequency() * 1000));
cvShowImage("video", pGrayImage);
cvShowImage("foreground", pGrayForeImage);
}
}
return pGrayForeImage;
}
void ViBe_M::applyTest(){
/*
1. cv::Mat -> IplImage
cv::Mat matimg = cv::imread ("heels.jpg");
IplImage* iplimg;
*iplimg = IplImage(matimg);
2. IplImage -> cv::Mat
IplImage* iplimg = cvLoadImage("heels.jpg");
cv::Mat matimg;
matimg = cv::Mat(iplimg);
*/
const char *videoName = "D:\\007\\opencv2\\test\\00008_1.mp4";
double t;
int nFrameNum = 0;
if (!(pCapture = cvCaptureFromAVI(videoName))){
cout << "can not open the video" << endl;
return ;
}
while (pFrame = cvQueryFrame(pCapture))
{
nFrameNum++;
if (nFrameNum == 1){
initParams(pFrame, ViBe_M::Config::getGrayConfig());
cvCvtColor(pFrame, pGrayImage, CV_BGR2GRAY);
update((uchar *)pGrayImage->imageData, (uchar *)pGrayForeImage->imageData);
}
else{
cvCvtColor(pFrame, pGrayImage, CV_BGR2GRAY);
t = (double)cvGetTickCount();
update((uchar *)pGrayImage->imageData, (uchar *)pGrayForeImage->imageData);
t = (double)cvGetTickCount() - t;
printf("exec time = %gms\n", t / (cvGetTickFrequency() * 1000));
cvShowImage("video", pGrayImage);
cvShowImage("foreground",pGrayForeImage);
char c = cvWaitKey(5);
if (c == 27)break;
}
}
}