/
FrameAcquirer.cpp
114 lines (85 loc) · 3.82 KB
/
FrameAcquirer.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
#include <OpenCV/OpenCV.h>
#include <cassert>
#include <iostream>
#include "Pixelator.h"
#include "FrameAcquirer.h"
#include "SinGen.h"
const char * WINDOW_NAME = "PhotoChango - Brandon Lucia 2011 - http://cs.washington.edu/homes/blucia0a - 'q' to quit";
extern bool useMotion;
extern bool useMotionAndLight;
FrameAcquirer::FrameAcquirer(SinGen *s)
{
this->sg = s;
// create all necessary instances
cvNamedWindow (WINDOW_NAME, CV_WINDOW_AUTOSIZE);
CvCapture * camera = cvCreateCameraCapture (CV_CAP_ANY);
CvMemStorage* storage = cvCreateMemStorage(0);
assert (storage);
// you do own an iSight, don't you ?!?
if (! camera)
abort ();
if(useMotion == false){
// get an initial frame and duplicate it for later work
IplImage * current_frame = cvQueryFrame (camera);
IplImage * gray_image = cvCreateImage(cvSize (current_frame->width, current_frame->height), IPL_DEPTH_8U, 1);
IplImage * gray_image2 = cvCreateImage(cvSize (current_frame->width, current_frame->height), IPL_DEPTH_8U, 1);
assert (current_frame && gray_image);
// as long as there are images ...
while (current_frame = cvQueryFrame (camera))
{
// convert to gray and downsize
cvCvtColor (current_frame, gray_image, CV_BGR2GRAY);
cvConvert( gray_image, gray_image2);
float vals[NUM_WAVES];
pixelate(gray_image,vals);
this->sg->setAmplitudes(vals);
// just show the image
cvAddWeighted( gray_image2, 0.5, gray_image, 0.5, 0.5, gray_image);
cvShowImage (WINDOW_NAME, gray_image);
// cvShowImage (WINDOW_NAME, current_frame);
// wait a tenth of a second for keypress and window drawing
int key = cvWaitKey (30);
if (key == 'q' || key == 'Q')
break;
}
}else{
IplImage * current_frame = cvQueryFrame (camera);
IplImage * gray_image = cvCreateImage(cvSize (current_frame->width, current_frame->height), IPL_DEPTH_8U, 1);
IplImage * avg_img = cvCreateImage(cvSize (current_frame->width, current_frame->height), IPL_DEPTH_32F, 1);
IplImage * gavg_img = cvCreateImage(cvSize (current_frame->width, current_frame->height), IPL_DEPTH_8U, 1);
IplImage * diff_img = cvCreateImage(cvSize (current_frame->width, current_frame->height), IPL_DEPTH_8U, 1);
IplImage * diff_img2 = cvCreateImage(cvSize (current_frame->width, current_frame->height), IPL_DEPTH_8U, 1);
IplImage * diff_img3 = cvCreateImage(cvSize (current_frame->width, current_frame->height), IPL_DEPTH_8U, 1);
// as long as there are images ...
while (current_frame = cvQueryFrame (camera))
{
// convert to gray and downsize
cvCvtColor (current_frame, gray_image, CV_BGR2GRAY);
cvSmooth( gray_image, gray_image);
cvRunningAvg( gray_image, avg_img, .250, NULL);
cvConvert( avg_img, gavg_img );
cvAbsDiff( gray_image, gavg_img, diff_img );
cvConvert( diff_img, diff_img2 );
float vals[NUM_WAVES];
pixelate(diff_img,vals);
this->sg->setAmplitudes(vals);
if(useMotionAndLight){
pixelate(gray_image,vals);
for(int i = 0; i < NUM_WAVES; i++){
vals[i] *= C8;
}
this->sg->setFrequencies(vals);
cvAddWeighted( diff_img2, 0.5, gray_image, 0.5, 0.5, diff_img);
cvShowImage ( WINDOW_NAME, diff_img);
}else{
cvAddWeighted( diff_img, 0.5, diff_img2, 0.5, 0.5, diff_img);
cvShowImage ( WINDOW_NAME, diff_img);
}
int key = cvWaitKey (30);
if (key == 'q' || key == 'Q')
break;
}
}
// be nice and return no error
return;
}