环境一览:WINXP SP3+DX9.0C+VC6.0 SP6+IPP5.3.2.073+DX9.0bSDK
环境变量设置:
set OPENCV=E:\Program Files\OpenCV\
set include=%include%;%OPENCV%\CXCORE\INCLUDE;%OPENCV%\CV\INCLUDE;%OPENCV%\CVAUX\INCLUDE;%OPENCV%\ML\INCLUDE;%OPENCV%\OTHERLIBS\HIGHGUI;%OPENCV\OTHERLIBS\_GRAPHICS
\INCLUDE
set lib=%lib%;%OPENCV%\LIB
对于VC IDE 在工具-选项-目录里面设置与以上相应的include和libs
参考书籍:
OReilly-LearningOpenCV
第二章例子更正:
example 1
cvLoadImage( argv[1],CV_LOAD_IMAGE_COLOR );
example 2
不是所有格式avi都能放,可安装XVID,或者K-lite codec pack
支持格式可参考http://opencv.willowgarage.com/wiki/VideoCodecs
mencoder in.avi -ovc raw -vf format=i420 -o out.avi
example 3
注释掉Hack代码
拖放使能:去注释cvGetCaptureProperty
不需要#include "cv.h"
example 4
图片不要放在Debug文件夹里
example 5
filter
cvPyrDown有第三个参数filter
example 10
cvCreateVideoWriter中CV_FOURCC改成你机子上支持的格式,否则不生成AVI
TO remember:
although it’s important to take care of garbage collection in OpenCV, we should only clean up the garbage that we have created.
另外example 9 应该是用的vfw,可用Directshow的实现
参考链接:
http://www.opencv.org.cn/index.php/%E4%BD%BF%E7%94%A8DirectShow%E9%87%87%E9%9B%86%E5%9B%BE%E5%83%8F

Code
1
第1章习题答案:
2
3-4
3
4
5
#include "cv.h"
6
#include "highgui.h"
7
8
IplImage* doPyrDown(
9
IplImage* in,
10
int filter = IPL_GAUSSIAN_5x5)
11

{
12
13
// Best to make sure input image is divisible by two.
14
//
15
assert( in->width%2 == 0 && in->height%2 == 0 );
16
17
IplImage* out = cvCreateImage(
18
cvSize( in->width/2, in->height/2 ),
19
in->depth,
20
in->nChannels
21
);
22
cvPyrDown( in, out );
23
return( out );
24
};
25
26
int main( int argc, char** argv )
{
27
cvNamedWindow( "MyApp", CV_WINDOW_AUTOSIZE );
28
cvNamedWindow( "Scale", CV_WINDOW_AUTOSIZE );
29
CvCapture* capture;
30
if (argc==1)
{
31
capture = cvCreateCameraCapture( -1 );
32
} else
{
33
capture = cvCreateFileCapture( argv[1] );
34
}
35
assert( capture != NULL );
36
37
IplImage* frame;
38
39
double fps = 25.0;
40
41
CvSize size = cvSize(
42
(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),
43
(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT)
44
);
45
46
IplImage* scaleimg = cvCreateImage( size, IPL_DEPTH_8U,
47
3);
48
49
CvVideoWriter* writer = cvCreateVideoWriter( // On linux Will only work if you've installed ffmpeg development files correctly,
50
"write.avi", // otherwise segmentation fault. Windows probably better.
51
CV_FOURCC('x','v','i','d'),
52
fps,
53
size
54
);
55
56
while(1)
{
57
58
59
frame = cvQueryFrame( capture );
60
if( !frame ) break;
61
cvShowImage( "MyApp", frame );
62
scaleimg=doPyrDown(frame);
63
cvShowImage("Scale",scaleimg);
64
cvWriteToAVI( writer, scaleimg );
65
66
char c = cvWaitKey(10);
67
if( c == 27 ) break;
68
}
69
cvReleaseVideoWriter( &writer );
70
71
cvReleaseImage( &scaleimg );
72
cvReleaseCapture( &capture );
73
cvDestroyWindow( "MyApp" );
74
cvDestroyWindow("Scale");
75
}
76
77
5
78
79
#include <iostream>
80
81
#include "cv.h"
82
#include "highgui.h"
83
/**//*
84
OK, you caught us. Video playback under linux is still just bad. Part of this is due to FFMPEG, part of this
85
is due to lack of standards in video files. But the position slider here will often not work. We tried to at least
86
find number of frames using the "getAVIFrames" hack below. Terrible. But, this file shows something of how to
87
put a slider up and play with it. Sorry.
88
*/
89
90
91
using namespace std;
92
93
int g_slider_position = 0;
94
CvCapture* g_capture = NULL;
95
96
97
IplImage* doPyrDown(IplImage* in,
98
int filter = IPL_GAUSSIAN_5x5)
99

{
100
101
// Best to make sure input image is divisible by two.
102
assert( in->width%2 == 0 && in->height%2 == 0 );
103
104
IplImage* out = cvCreateImage(
105
cvSize( in->width/2, in->height/2),
106
in->depth,
107
in->nChannels
108
);
109
cvPyrDown( in, out );
110
return( out );
111
};
112
113
void onTrackbarSlide(int pos)
{
114
cvSetCaptureProperty(
115
g_capture,
116
CV_CAP_PROP_POS_FRAMES,
117
pos
118
);
119
}
120
121
int main( int argc, char** argv )
{
122
cvNamedWindow( "MyApp2", CV_WINDOW_AUTOSIZE );
123
cvNamedWindow( "Scale", CV_WINDOW_AUTOSIZE );
124
g_capture = cvCreateCameraCapture( 0 );
125
IplImage *foo = cvQueryFrame( g_capture);
126
127
128
int frames = 3;
129
130
int tmpw = (int) cvGetCaptureProperty(
131
g_capture,
132
CV_CAP_PROP_FRAME_WIDTH
133
);
134
135
int tmph = (int) cvGetCaptureProperty(
136
g_capture,
137
CV_CAP_PROP_FRAME_HEIGHT
138
);
139
140
141
cvCreateTrackbar(
142
"Position",
143
"MyApp2",
144
&g_slider_position,
145
frames,
146
onTrackbarSlide
147
);
148
IplImage* frame;
149
IplImage* img2 = cvCreateImage( cvSize(foo->width/2,foo->height/2), IPL_DEPTH_8U, 3);
150
while(1)
{
151
152
frame = cvQueryFrame( g_capture );
153
if( !frame ) break;
154
//int frames = cvGetCaptureProperty( g_capture, CV_CAP_PROP_POS_FRAMES);//This should work, sometimes it does not on linux
155
switch (g_slider_position)
156
{
157
case 0:
158
img2=doPyrDown(frame);
159
break;
160
case 1:
161
img2=doPyrDown(frame);
162
img2=doPyrDown(img2);
163
break;
164
default:
165
img2=doPyrDown(frame);
166
img2=doPyrDown(img2);
167
img2=doPyrDown(img2);
168
break;
169
}
170
171
172
cvShowImage( "MyApp2", frame );
173
cvShowImage("Scale",img2);
174
175
176
char c = (char)cvWaitKey(10);
177
if( c == 27 ) break;
178
}
179
cvReleaseImage( &img2);
180
cvReleaseCapture( &g_capture );
181
cvDestroyWindow( "MyApp2" );
182
cvDestroyWindow( "Scale" );
183
return(0);
184
}
185
186