-
Notifications
You must be signed in to change notification settings - Fork 6
/
bkg.c
136 lines (129 loc) · 4.49 KB
/
bkg.c
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
#include <opencv2/imgproc/imgproc_c.h>
#include "capture.h"
static void calc_mbdiffs(IplImage *img)
{
int w = img->width, h = img->height;
int mx = w/16 + (0 != w % 16), my = h/16 + (0 != h % 16);
int x, y, j;
char *data = img->imageData;
int stride = img->widthStep;
int size = mx*my*sizeof(uint8_t);
uint8_t *mbdiffs = malloc(size);
memset(mbdiffs, 0, size);
for (y = 0; y < h; y++) {
int mby = y/16, mbx = 0;
uint8_t*line = (uint8_t*)data;
for (x = 0; x < w; x+= 16) {
int mbxy = mby*mx + mbx;
int diffs = 0;
for (j = 0; j < 16; j++) { diffs += *line > 0; line++; }
mbdiffs[mbxy] += diffs;
mbx += 1;
}
data += stride;
}
data = img->imageData;
for (y = 0; y < h; y++) {
int mby = y/16, mbx = 0;
uint8_t *line = (uint8_t*)data;
for (x = 0; x < w; x += 16) {
int mbxy = mby*mx + mbx;
int val = (mbdiffs[mbxy] > 10) * 255;
for (j = 0; j < 16; j++) *line++ = val;
mbx += 1;
}
data += stride;
}
free(mbdiffs);
}
int main(int argc, char **argv)
{
IplImage *avg, *i32, *diff, *dxdy, *dx, *dy, *lap, *prev = NULL;
IplImage *sub, *mask, *nmask, *gray, *gray8, *masked;
char *fname = "bbb.mkv";
//char *fname = "/home/josh/Desktop/irene.mkv";
char *windows[] = { "diffavg", "diff", "smoothed", "actual",
"1st derivative", "2nd derivative (Laplacian)", "masked" };
int nb_w = sizeof(windows)/sizeof(char*), i;
capture_t ctx = {0};
ctx.filename = fname;
start_capture(&ctx);
for (i = 0; i < nb_w; i++) cvNamedWindow(windows[i], 1);
CvSize size = { .width = ctx.img->width,
.height = ctx.img->height };
avg = cvCreateImage(size, IPL_DEPTH_32F, 3);
i32 = cvCreateImage(size, IPL_DEPTH_32F, 3);
diff = cvCreateImage(size, IPL_DEPTH_32F, 3);
dx = cvCreateImage(size, IPL_DEPTH_32F, 3);
dy = cvCreateImage(size, IPL_DEPTH_32F, 3);
lap = cvCreateImage(size, IPL_DEPTH_32F, 3);
dxdy = cvCreateImage(size, IPL_DEPTH_32F, 3);
sub = cvCreateImage(size, IPL_DEPTH_32F, 3);
gray = cvCreateImage(size, IPL_DEPTH_32F, 1);
gray8 = cvCreateImage(size, IPL_DEPTH_8U, 1);
mask = cvCreateImage(size, IPL_DEPTH_8U, 1);
nmask = cvCreateImage(size, IPL_DEPTH_8U, 1);
masked = cvCreateImage(size, IPL_DEPTH_8U, 3);
for (i = 0; i < nb_w; i++) {
int x = size.width * (i % 4);
int y = (i >= 4) * (size.height + 50);
cvMoveWindow(windows[i], x, y);
}
while (1) {
IplImage *img = capture_frame(&ctx);
if (!img) goto realerr;
// do processing
if (!prev) {
prev = cvCreateImage(size, IPL_DEPTH_32F, 3);
continue;
}
cvConvertScale(img, i32, 1/255.0, 0);
cvSmooth(i32, i32, CV_GAUSSIAN, 9, 9, 0, 0);
cvAbsDiff(i32, prev, diff);
cvSmooth(diff, diff, CV_GAUSSIAN, 9, 9, 0, 0);
cvSobel(diff, dx, 1, 0, 3);
cvSobel(diff, dy, 0, 1, 3);
cvLaplace(diff, lap, 3);
cvAdd(dx, dy, dxdy, NULL);
cvAdd(diff, dxdy, sub, NULL);
cvCvtColor(diff, gray, CV_RGB2GRAY);
cvConvertScale(gray, gray8, 256.0, 0);
cvAdaptiveThreshold(gray8, mask, 255, CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY, 3, 0);
calc_mbdiffs(mask);
cvSmooth(mask, mask, CV_GAUSSIAN, 9, 9, 0, 0);
cvNot(mask, nmask);
cvXor(masked, masked, masked, NULL);
cvCopy(img, masked, mask);
cvRunningAvg(diff, avg, 0.05, NULL);
cvShowImage("diffavg", avg);
cvShowImage("diff", diff);
cvShowImage("smoothed", i32);
cvShowImage("actual", img);
cvShowImage("1st derivative", dxdy);
cvShowImage("2nd derivative (Laplacian)", lap);
cvShowImage("masked", masked);
release_frame(&ctx);
cvCopy(i32, prev, NULL);
if ((cvWaitKey(1)&255)==27) break; // esc
}
for (i = 0; i < nb_w; i++) cvDestroyWindow(windows[i]);
stop_capture(&ctx);
cvReleaseImage(&avg);
cvReleaseImage(&i32);
cvReleaseImage(&diff);
cvReleaseImage(&dxdy);
cvReleaseImage(&dx);
cvReleaseImage(&dy);
cvReleaseImage(&lap);
cvReleaseImage(&sub);
cvReleaseImage(&mask);
cvReleaseImage(&nmask);
cvReleaseImage(&masked);
cvReleaseImage(&gray);
cvReleaseImage(&gray8);
if (prev) cvReleaseImage(&prev);
return 0;
realerr:
fprintf(stderr, "error capturing\n");
return 1;
}