Skip to content

Commit

Permalink
adding example_09-04.cpp example_09-10.cpp example_15-02.cpp example_…
Browse files Browse the repository at this point in the history
…15-03.cpp example_15-04.cpp
  • Loading branch information
garybradski committed May 31, 2017
1 parent 83b8ac4 commit 68f6043
Show file tree
Hide file tree
Showing 6 changed files with 629 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ add_executable( example_08-03 example_08-03.cpp )
add_executable( example_09-01 example_09-01.cpp )
add_executable( example_09-02 example_09-02.cpp )
add_executable( example_09-03 example_09-03.cpp )
#add_library(examples_09 example_09-04.cpp)
add_executable( example_10-01 example_10-01.cpp )
add_executable( example_10-02 example_10-02.cpp )
add_executable( example_10-03 example_10-03.cpp )
Expand All @@ -47,6 +48,7 @@ add_executable( example_14-03 example_14-03.cpp )
add_executable( example_14-04 example_14-04.cpp )
add_executable( example_15-01 example_15-01.cpp )
add_executable( example_15-02 example_15-02.cpp )
add_executable( example_15-04 example_15-04.cpp )
#...
add_executable( example_16-01 example_16-01.cpp )

Expand Down Expand Up @@ -92,5 +94,6 @@ target_link_libraries( example_14-03 ${OpenCV_LIBS} )
target_link_libraries( example_14-04 ${OpenCV_LIBS} )
target_link_libraries( example_15-01 ${OpenCV_LIBS} )
target_link_libraries( example_15-02 ${OpenCV_LIBS} )
target_link_libraries( example_15-04 ${OpenCV_LIBS} )
#...
target_link_libraries( example_16-01 ${OpenCV_LIBS} )
3 changes: 3 additions & 0 deletions example_09-04.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
//connected to the sliders in Figure 9-6

#include <opencv2/opencv.hpp>
#include <GL/gl.h>
#include <GL/glut.h>

using namespace std;

void on_opengl( void* param ) {
Expand Down
73 changes: 70 additions & 3 deletions example_09-10.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//Example 9-10. The WxMoviePlayer object source file WxMoviePlayer.cpp
//This file is incomplete
//
#include "WxMoviePlayer.hpp"
BEGIN_EVENT_TABLE( WxMoviePlayer, wxWindow )
EVT_PAINT( WxMoviePlayer::OnPaint )
EVT_TIMER( TIMER_ID, WxMoviePlayer::OnTimer )
EVT_CHAR( WxMoviePlayer::OnKey )
END_EVENT_TABLE()
The first thing we do is to set up the callbacks that will be associated with individual
events. We do this through macros provided by the wxWidgets framework.17

// The first thing we do is to set up the callbacks that will be associated with individual
// events. We do this through macros provided by the wxWidgets framework.
WxMoviePlayer::WxMoviePlayer(
wxWindow* parent,
const wxPoint& pos,
Expand All @@ -16,3 +17,69 @@ WxMoviePlayer::WxMoviePlayer(
m_timer = NULL;
m_parent = parent;
}

//We will need to
//know which frame is the parent when it comes time to close the application in
//response to the Esc key.
void WxMoviePlayer::OnPaint( wxPaintEvent& event ) {
wxPaintDC dc( this );
if( !dc.Ok() ) return;
int x,y,w,h;
dc.BeginDrawing();
dc.GetClippingBox( &x, &y, &w, &h );
dc.DrawBitmap( m_wx_bmp, x, y );
dc.EndDrawing();
return;
}

//The WxMoviePlayer::_copyImage() method will get called whenever a new image is
//read from the cv::VideoCapture object.
void WxMoviePlayer::_copyImage( void ) {
m_wx_bmp = wxBitmap( m_wx_img );
Refresh( FALSE ); // indicate that the object is dirty
Update();
}

//The WxMoviePlayer::open() method also does several important things. The first is
//to actually open the cv::VideoCapture object, but there is a lot more to be done.
//Next, an image is read off of the player and is used to create a wxImage object that
//“points at” the OpenCV cv::Mat image. This is the opposite philosophy to the one
//we used in the Qt example: in this case, it turns out to be a little more convenient to
//create the cv::Mat first and have it own the data, and then to create the GUI toolkit’s
//image object

bool WxMoviePlayer::open( wxString file ) {

if( !m_cap.open( std::string( file.mb_str() ) )) {
return false;
}

// If we opened the file, set up everything now:
//
m_cap.read( m_cv_img );
m_wx_img = wxImage(
m_cv_img.cols,
m_cv_img.rows,
m_cv_img.data,
TRUE // static data, do not free on delete()
);
_copyImage();
m_timer = new wxTimer( this, TIMER_ID );
m_timer->Start( 1000. / m_cap.get( cv::CAP_PROP_FPS ) );
return true;
}

//The following handler doesn’t do too much; primarily it just reads a new frame from the video,
//converts that frame from BGR to RGB for display, and then calls our WxMovie
//Player::_copyImage() , which makes the next bitmap for us.
void WxMoviePlayer::OnTimer( wxTimerEvent& event ) {
if( !m_cap.isOpened() ) return;
m_cap.read( m_cv_img );
cv::cvtColor( m_cv_img, m_cv_img, cv::BGR2RGB );
_copyImage();
}

//Handler for keypresses
void WxMoviePlayer::OnKey( wxKeyEvent& e ) {
if( e.GetKeyCode() == WXK_ESCAPE ) m_parent->Close();
}
4 changes: 2 additions & 2 deletions example_15-02.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using namespace std;
//
// Float, 3-channel images
//
cv::Mat image; //, rawImage;
cv::Mat image;
cv::Mat IavgF, IdiffF, IprevF, IhiF, IlowF;
cv::Mat tmp, tmp2, mask;

Expand Down Expand Up @@ -173,8 +173,8 @@ int main( int argc, char** argv) {
while(1) {
cout << "frame#: " << frame_count << endl;
cap >> image;
if(frame_count == 0) { AllocateImages(image);}
if( !image.data ) exit(1); // Something went wrong, abort
if(frame_count == 0) { AllocateImages(image);}
accumulateBackground( image );
cv::imshow( argv[0], image );
frame_count++;
Expand Down
94 changes: 94 additions & 0 deletions example_15-03.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//Example 15-3. Computing the on and off-diagonal elements of a variance/covariance model

vector<cv::Mat> planes(3);
vector<cv::Mat> sums(3);
vector<cv::Mat> xysums(6);
int image_count = 0;

//A function to accumulate
// the information we need for our variance computation:
void accumulateVariance(
cv::Mat& I
) {
if( sum.empty ) {
sum = cv::Mat::zeros( I.size(), CV_32FC(I.channels()) );
sqsum = cv::Mat::zeros( I.size(), CV_32FC(I.channels()) );
}
cv::accumulate( I, sum );
cv::accumulateSquare( I, sqsum );
image_count++;
}

//The associated variance computation function would then be:
// (note that 'variance' is sigma^2)
//
void computeVariance(
cv::Mat& variance
) {
double one_by_N = 1.0 / image_count;
variance = one_by_N * sqsum – (one_by_N * one_by_N) * sum.mul(sum);
}




void accumulateCovariance(
cv::Mat& I
) {
int i, j, n;

if( sum.empty ) {
for( i=0; i<3; i++ ) {
// the r, g, and b sums
sums[i]
= cv::Mat::zeros( I.size(), CV::F32C1 );
}
for( n=0; n<6; n++ ) {
// the rr, rg, rb, gg, gb, and bb elements
xysums[n] = cv::Mat::zeros( I.size(), CV::F32C1 ) );
}
}
cv::split( I, planes );
for( i=0; i<3; i++ ) {
cv::accumulate( planes[i], sums[i] );
}
n = 0;
for( i=0; i<3; i++ ) {
// "row" of Sigma
for( j=i; j<3; j++ ) {
// "column" of Sigma
n++;
cv::accumulateProduct( planes[i], planes[j], xysums[n] );
}
}
image_count++;
}

//The corresponding compute function is also just a slight extension of the compute
//function for the variances we saw earlier.
// note that 'variance' is sigma^2
//
void computeVariance(
cv::Mat& covariance
// a six-channel array, channels are the
// rr, rg, rb, gg, gb, and bb elements of Sigma_xy
) {
double one_by_N = 1.0 / image_count;

// reuse the xysum arrays as storage for individual entries
//
int n = 0;
for( int i=0; i<3; i++ ) {
// "row" of Sigma
for( int j=i; j<3; j++ ) {
// "column" of Sigma
n++;
xysums[n] = one_by_N * xysums[n]
– (one_by_N * one_by_N) * sums[i].mul(sums[j]);
}
}

// reassemble the six individual elements into a six-channel array
//
cv::merge( xysums, covariance );
}
Loading

0 comments on commit 68f6043

Please sign in to comment.