Skip to content

Commit 68f6043

Browse files
committed
adding example_09-04.cpp example_09-10.cpp example_15-02.cpp example_15-03.cpp example_15-04.cpp
1 parent 83b8ac4 commit 68f6043

File tree

6 files changed

+629
-5
lines changed

6 files changed

+629
-5
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ add_executable( example_08-03 example_08-03.cpp )
3030
add_executable( example_09-01 example_09-01.cpp )
3131
add_executable( example_09-02 example_09-02.cpp )
3232
add_executable( example_09-03 example_09-03.cpp )
33+
#add_library(examples_09 example_09-04.cpp)
3334
add_executable( example_10-01 example_10-01.cpp )
3435
add_executable( example_10-02 example_10-02.cpp )
3536
add_executable( example_10-03 example_10-03.cpp )
@@ -47,6 +48,7 @@ add_executable( example_14-03 example_14-03.cpp )
4748
add_executable( example_14-04 example_14-04.cpp )
4849
add_executable( example_15-01 example_15-01.cpp )
4950
add_executable( example_15-02 example_15-02.cpp )
51+
add_executable( example_15-04 example_15-04.cpp )
5052
#...
5153
add_executable( example_16-01 example_16-01.cpp )
5254

@@ -92,5 +94,6 @@ target_link_libraries( example_14-03 ${OpenCV_LIBS} )
9294
target_link_libraries( example_14-04 ${OpenCV_LIBS} )
9395
target_link_libraries( example_15-01 ${OpenCV_LIBS} )
9496
target_link_libraries( example_15-02 ${OpenCV_LIBS} )
97+
target_link_libraries( example_15-04 ${OpenCV_LIBS} )
9598
#...
9699
target_link_libraries( example_16-01 ${OpenCV_LIBS} )

example_09-04.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
//connected to the sliders in Figure 9-6
44

55
#include <opencv2/opencv.hpp>
6+
#include <GL/gl.h>
7+
#include <GL/glut.h>
8+
69
using namespace std;
710

811
void on_opengl( void* param ) {

example_09-10.cpp

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
//Example 9-10. The WxMoviePlayer object source file WxMoviePlayer.cpp
2-
//This file is incomplete
2+
//
33
#include "WxMoviePlayer.hpp"
44
BEGIN_EVENT_TABLE( WxMoviePlayer, wxWindow )
55
EVT_PAINT( WxMoviePlayer::OnPaint )
66
EVT_TIMER( TIMER_ID, WxMoviePlayer::OnTimer )
77
EVT_CHAR( WxMoviePlayer::OnKey )
88
END_EVENT_TABLE()
9-
The first thing we do is to set up the callbacks that will be associated with individual
10-
events. We do this through macros provided by the wxWidgets framework.17
9+
10+
// The first thing we do is to set up the callbacks that will be associated with individual
11+
// events. We do this through macros provided by the wxWidgets framework.
1112
WxMoviePlayer::WxMoviePlayer(
1213
wxWindow* parent,
1314
const wxPoint& pos,
@@ -16,3 +17,69 @@ WxMoviePlayer::WxMoviePlayer(
1617
m_timer = NULL;
1718
m_parent = parent;
1819
}
20+
21+
//We will need to
22+
//know which frame is the parent when it comes time to close the application in
23+
//response to the Esc key.
24+
void WxMoviePlayer::OnPaint( wxPaintEvent& event ) {
25+
wxPaintDC dc( this );
26+
if( !dc.Ok() ) return;
27+
int x,y,w,h;
28+
dc.BeginDrawing();
29+
dc.GetClippingBox( &x, &y, &w, &h );
30+
dc.DrawBitmap( m_wx_bmp, x, y );
31+
dc.EndDrawing();
32+
return;
33+
}
34+
35+
//The WxMoviePlayer::_copyImage() method will get called whenever a new image is
36+
//read from the cv::VideoCapture object.
37+
void WxMoviePlayer::_copyImage( void ) {
38+
m_wx_bmp = wxBitmap( m_wx_img );
39+
Refresh( FALSE ); // indicate that the object is dirty
40+
Update();
41+
}
42+
43+
//The WxMoviePlayer::open() method also does several important things. The first is
44+
//to actually open the cv::VideoCapture object, but there is a lot more to be done.
45+
//Next, an image is read off of the player and is used to create a wxImage object that
46+
//“points at” the OpenCV cv::Mat image. This is the opposite philosophy to the one
47+
//we used in the Qt example: in this case, it turns out to be a little more convenient to
48+
//create the cv::Mat first and have it own the data, and then to create the GUI toolkit’s
49+
//image object
50+
51+
bool WxMoviePlayer::open( wxString file ) {
52+
53+
if( !m_cap.open( std::string( file.mb_str() ) )) {
54+
return false;
55+
}
56+
57+
// If we opened the file, set up everything now:
58+
//
59+
m_cap.read( m_cv_img );
60+
m_wx_img = wxImage(
61+
m_cv_img.cols,
62+
m_cv_img.rows,
63+
m_cv_img.data,
64+
TRUE // static data, do not free on delete()
65+
);
66+
_copyImage();
67+
m_timer = new wxTimer( this, TIMER_ID );
68+
m_timer->Start( 1000. / m_cap.get( cv::CAP_PROP_FPS ) );
69+
return true;
70+
}
71+
72+
//The following handler doesn’t do too much; primarily it just reads a new frame from the video,
73+
//converts that frame from BGR to RGB for display, and then calls our WxMovie
74+
//Player::_copyImage() , which makes the next bitmap for us.
75+
void WxMoviePlayer::OnTimer( wxTimerEvent& event ) {
76+
if( !m_cap.isOpened() ) return;
77+
m_cap.read( m_cv_img );
78+
cv::cvtColor( m_cv_img, m_cv_img, cv::BGR2RGB );
79+
_copyImage();
80+
}
81+
82+
//Handler for keypresses
83+
void WxMoviePlayer::OnKey( wxKeyEvent& e ) {
84+
if( e.GetKeyCode() == WXK_ESCAPE ) m_parent->Close();
85+
}

example_15-02.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using namespace std;
1010
//
1111
// Float, 3-channel images
1212
//
13-
cv::Mat image; //, rawImage;
13+
cv::Mat image;
1414
cv::Mat IavgF, IdiffF, IprevF, IhiF, IlowF;
1515
cv::Mat tmp, tmp2, mask;
1616

@@ -173,8 +173,8 @@ int main( int argc, char** argv) {
173173
while(1) {
174174
cout << "frame#: " << frame_count << endl;
175175
cap >> image;
176-
if(frame_count == 0) { AllocateImages(image);}
177176
if( !image.data ) exit(1); // Something went wrong, abort
177+
if(frame_count == 0) { AllocateImages(image);}
178178
accumulateBackground( image );
179179
cv::imshow( argv[0], image );
180180
frame_count++;

example_15-03.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//Example 15-3. Computing the on and off-diagonal elements of a variance/covariance model
2+
3+
vector<cv::Mat> planes(3);
4+
vector<cv::Mat> sums(3);
5+
vector<cv::Mat> xysums(6);
6+
int image_count = 0;
7+
8+
//A function to accumulate
9+
// the information we need for our variance computation:
10+
void accumulateVariance(
11+
cv::Mat& I
12+
) {
13+
if( sum.empty ) {
14+
sum = cv::Mat::zeros( I.size(), CV_32FC(I.channels()) );
15+
sqsum = cv::Mat::zeros( I.size(), CV_32FC(I.channels()) );
16+
}
17+
cv::accumulate( I, sum );
18+
cv::accumulateSquare( I, sqsum );
19+
image_count++;
20+
}
21+
22+
//The associated variance computation function would then be:
23+
// (note that 'variance' is sigma^2)
24+
//
25+
void computeVariance(
26+
cv::Mat& variance
27+
) {
28+
double one_by_N = 1.0 / image_count;
29+
variance = one_by_N * sqsum – (one_by_N * one_by_N) * sum.mul(sum);
30+
}
31+
32+
33+
34+
35+
void accumulateCovariance(
36+
cv::Mat& I
37+
) {
38+
int i, j, n;
39+
40+
if( sum.empty ) {
41+
for( i=0; i<3; i++ ) {
42+
// the r, g, and b sums
43+
sums[i]
44+
= cv::Mat::zeros( I.size(), CV::F32C1 );
45+
}
46+
for( n=0; n<6; n++ ) {
47+
// the rr, rg, rb, gg, gb, and bb elements
48+
xysums[n] = cv::Mat::zeros( I.size(), CV::F32C1 ) );
49+
}
50+
}
51+
cv::split( I, planes );
52+
for( i=0; i<3; i++ ) {
53+
cv::accumulate( planes[i], sums[i] );
54+
}
55+
n = 0;
56+
for( i=0; i<3; i++ ) {
57+
// "row" of Sigma
58+
for( j=i; j<3; j++ ) {
59+
// "column" of Sigma
60+
n++;
61+
cv::accumulateProduct( planes[i], planes[j], xysums[n] );
62+
}
63+
}
64+
image_count++;
65+
}
66+
67+
//The corresponding compute function is also just a slight extension of the compute
68+
//function for the variances we saw earlier.
69+
// note that 'variance' is sigma^2
70+
//
71+
void computeVariance(
72+
cv::Mat& covariance
73+
// a six-channel array, channels are the
74+
// rr, rg, rb, gg, gb, and bb elements of Sigma_xy
75+
) {
76+
double one_by_N = 1.0 / image_count;
77+
78+
// reuse the xysum arrays as storage for individual entries
79+
//
80+
int n = 0;
81+
for( int i=0; i<3; i++ ) {
82+
// "row" of Sigma
83+
for( int j=i; j<3; j++ ) {
84+
// "column" of Sigma
85+
n++;
86+
xysums[n] = one_by_N * xysums[n]
87+
– (one_by_N * one_by_N) * sums[i].mul(sums[j]);
88+
}
89+
}
90+
91+
// reassemble the six individual elements into a six-channel array
92+
//
93+
cv::merge( xysums, covariance );
94+
}

0 commit comments

Comments
 (0)