Skip to content

Commit 44a39cc

Browse files
committed
add code from c9
Signed-off-by: jsxyhelu <jsxyhelu@foxmail.com>
1 parent d5d6162 commit 44a39cc

11 files changed

+369
-0
lines changed

example_09-01.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <opencv2/opencv.hpp>
2+
3+
using namespace std;
4+
5+
int main( int argc, char** argv ) {
6+
// Create a named window with the name of the file
7+
//
8+
cv::namedWindow( argv[1], 1 );
9+
// Load the image from the given filename
10+
//
11+
cv::Mat = cv::imread( argv[1] );
12+
// Show the image in the named window
13+
//
14+
cv::imshow( argv[1], img );
15+
// Idle until the user hits the Esc key
16+
//
17+
while( true ) {
18+
if( cv::waitKey( 100 /* milliseconds */ ) == 27 ) break;
19+
}
20+
// Clean up and don't be piggies
21+
//
22+
cv::destroyWindow( argv[1] );
23+
exit(0);
24+
}

example_09-02.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <opencv2/opencv.hpp>
2+
// Define our callback which we will install for
3+
// mouse events
4+
//
5+
void my_mouse_callback(
6+
int event, int x, int y, int flags, void* param
7+
);
8+
Rect box;
9+
bool drawing_box = false;
10+
// A little subroutine to draw a box onto an image
11+
//
12+
void draw_box( cv::Mat& img, cv::Rect box ) {
13+
cv::rectangle(
14+
img,
15+
box.tl(),
16+
box.br(),
17+
cv::Scalar(0x00,0x00,0xff) /* red */
18+
);
19+
}
20+
void help() {
21+
std::cout << "Call: ./ch4_ex4_1\n" <<
22+
" shows how to use a mouse to draw regions in an image." << std::endl;
23+
}
24+
int main( int argc, char** argv ) {
25+
help();
26+
box = cv::Rect(-1,-1,0,0);
27+
cv::Mat image(200, 200, CV_8UC3), temp;
28+
image.copyTo(temp);
29+
box = cv::Rect(-1,-1,0,0);
30+
image = cv::Scalar::all(0);
31+
cv::namedWindow( "Box Example" );
32+
// Here is the crucial moment where we actually install
33+
// the callback. Note that we set the value of 'params' to
34+
// be the image we are working with so that the callback
35+
// will have the image to edit.
36+
//
37+
cv::setMouseCallback(
38+
"Box Example",
39+
my_mouse_callback,
40+
(void*)&image
41+
);
42+
// The main program loop. Here we copy the working image
43+
// to the temp image, and if the user is drawing, then
44+
// put the currently contemplated box onto that temp image.
45+
// Display the temp image, and wait 15ms for a keystroke,
46+
// then repeat.
47+
//
48+
for(;;) {
49+
image.copyTo(temp);
50+
if( drawing_box ) draw_box( temp, box );
51+
cv::imshow( "Box Example", temp );
52+
if( cv::waitKey( 15 ) == 27 ) break;
53+
}
54+
return 0;
55+
}
56+
// This is our mouse callback. If the user
57+
// presses the left button, we start a box.
58+
// When the user releases that button, then we
59+
// add the box to the current image. When the
60+
// mouse is dragged (with the button down) we
61+
// resize the box.
62+
//
63+
void my_mouse_callback(
64+
int event, int x, int y, int flags, void* param)
65+
{
66+
cv::Mat& image = *(cv::Mat*) param;
67+
switch( event ) {
68+
case cv::EVENT_MOUSEMOVE: {
69+
if( drawing_box ) {
70+
box.width = x-box.x;
71+
box.height = y-box.y;
72+
}
73+
}
74+
break;
75+
case cv::EVENT_LBUTTONDOWN: {
76+
drawing_box = true;
77+
box = cv::Rect( x, y, 0, 0 );
78+
}
79+
break;
80+
case cv::EVENT_LBUTTONUP: {
81+
drawing_box = false;
82+
if( box.width < 0 ) {
83+
box.x += box.width;
84+
box.width *= -1;
85+
}
86+
if( box.height < 0 ) {
87+
box.y += box.height;
88+
box.height *= -1;
89+
}
90+
draw_box( image, box );
91+
}
92+
break;
93+
}
94+
}

example_09-03.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// An example program in which the user can draw boxes on the screen.
2+
//
3+
#include <opencv2/opencv.hpp>
4+
#include <iostream>
5+
using namespace std;
6+
//
7+
// Using a trackbar to create a "switch" that the user can turn on and off.
8+
// We make this value global so everyone can see it.
9+
//
10+
int g_switch_value = 1;
11+
void switch_off_function() { cout << "Pause\n"; }; //YOU COULD DO MORE
12+
void switch_on_function() { cout << "Run\n"; };
13+
// This will be the callback that we give to the trackbar.
14+
//
15+
void switch_callback( int position, void* ) {
16+
if( position == 0 ) {
17+
switch_off_function();
18+
} else {
19+
switch_on_function();
20+
}
21+
}
22+
void help() {
23+
cout << "Call: my.avi" << endl;
24+
cout << "Shows putting a pause button in a video." << endl;
25+
}
26+
int main( int argc, char** argv ) {
27+
cv::Mat frame; // To hold movie images
28+
cv::VideoCapture g_capture;
29+
help();
30+
if( argc < 2 || !g_capture.open( argv[1] ) ){
31+
cout << "Failed to open " << argv[1] << " video file\n" << endl;
32+
return -1;
33+
}
34+
// Name the main window
35+
//
36+
cv::namedWindow( "Example", 1 );
37+
// Create the trackbar. We give it a name,
38+
// and tell it the name of the parent window.
39+
//
40+
cv::createTrackbar(
41+
"Switch",
42+
"Example",
43+
&g_switch_value,
44+
1,
45+
switch_callback
46+
);
47+
// This will cause OpenCV to idle until
48+
// someone hits the Esc key.
49+
//
50+
for(;;) {
51+
if( g_switch_value ) {
52+
g_capture >> frame;
53+
if( frame.empty() ) break;
54+
cv::imshow( "Example", frame);
55+
}
56+
if( cv::waitKey(10)==27 ) break;
57+
}
58+
return 0;
59+
}

example_09-04.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <opencv2/opencv.hpp>
2+
using namespace std;
3+
4+
void on_opengl( void* param ) {
5+
glMatrixModel( GL_MODELVIEW );
6+
glLoadIdentity();
7+
glTranslated( 0.0, 0.0, -1.0 );
8+
glRotatef( rotx, 1, 0, 0 );
9+
glRotatef( roty, 0, 1, 0 );
10+
glRotatef( 0, 0, 0, 1 );
11+
static const int coords[6][4][3] = {
12+
{ { +1, -1, -1 }, { -1, -1, -1 }, { -1, +1, -1 }, { +1, +1, -1 } },
13+
{ { +1, +1, -1 }, { -1, +1, -1 }, { -1, +1, +1 }, { +1, +1, +1 } },
14+
{ { +1, -1, +1 }, { +1, -1, -1 }, { +1, +1, -1 }, { +1, +1, +1 } },
15+
{ { -1, -1, -1 }, { -1, -1, +1 }, { -1, +1, +1 }, { -1, +1, -1 } },
16+
{ { +1, -1, +1 }, { -1, -1, +1 }, { -1, -1, -1 }, { +1, -1, -1 } },
17+
{ { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } }
18+
};
19+
for (int i = 0; i < 6; ++i) {
20+
glColor3ub( i*20, 100+i*10, i*42 );
21+
glBegin(GL_QUADS);
22+
for (int j = 0; j < 4; ++j) {
23+
glVertex3d(
24+
0.2 * coords[i][j][0],
25+
0.2 * coords[i][j][1],
26+
0.2 * coords[i][j][2]
27+
);
28+
}
29+
glEnd();
30+
}
31+
}

example_09-05.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <QApplication>
2+
#include <QLabel>
3+
#include <QMoviePlayer.hpp>
4+
int main( int argc, char* argv[] ) {
5+
QApplication app( argc, argv );
6+
QMoviePlayer mp;
7+
mp.open( argv[1] );
8+
mp.show();
9+
return app.exec();
10+
}

example_09-06.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "ui_QMoviePlayer.h"
2+
#include <opencv2/opencv.hpp>
3+
#include <string>
4+
using namespace std;
5+
class QMoviePlayer : public QWidget {
6+
Q_OBJECT;
7+
public:
8+
QMoviePlayer( QWidget *parent = NULL );
9+
virtual ~QMoviePlayer() {;}
10+
bool open( string file );
11+
private:
12+
Ui::QMoviePlayer ui;
13+
cv::VideoCapture m_cap;
14+
QImage m_qt_img;
15+
cv::Mat m_cv_img;
16+
QTimer* m_timer;
17+
void paintEvent( QPaintEvent* q );
18+
void _copyImage( void );
19+
public slots:
20+
void nextFrame();
21+
};

example_09-07.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "QMoviePlayer.hpp"
2+
#include <QTimer>
3+
#include <QPainter>
4+
QMoviePlayer::QMoviePlayer( QWidget *parent )
5+
: QWidget( parent )
6+
{
7+
ui.setupUi( this );
8+
}

example_09-08.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "wx/wx.h"
2+
#include "WxMoviePlayer.hpp"
3+
// Application class, the top level object in wxWidgets
4+
//
5+
class MyApp : public wxApp {
6+
public:
7+
virtual bool OnInit();
8+
};
9+
// Behind the scenes stuff to create a main() function and attach MyApp
10+
//
11+
DECLARE_APP( MyApp );
12+
IMPLEMENT_APP( MyApp );
13+
// When MyApp is initialized, do these things.
14+
//
15+
bool MyApp::OnInit() {
16+
wxFrame* frame = new wxFrame( NULL, wxID_ANY, wxT("ch4_wx") );
17+
frame->Show( true );
18+
WxMoviePlayer* mp = new WxMoviePlayer(
19+
frame,
20+
wxPoint( -1, -1 ),
21+
wxSize( 640, 480 )
22+
);
23+
mp->open( wxString(argv[1]) );
24+
mp->Show( true );
25+
return true;
26+
}

example_09-09.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "opencv2/opencv.hpp"
2+
#include "wx/wx.h"
3+
#include <string>
4+
#define TIMER_ID 0
5+
using namespace std;
6+
class WxMoviePlayer : public wxWindow {
7+
public:
8+
WxMoviePlayer(
9+
wxWindow* parent,
10+
const wxPoint& pos,
11+
const wxSize& size
12+
);
13+
virtual ~WxMoviePlayer() {};
14+
bool open( wxString file );
15+
private:
16+
cv::VideoCapture m_cap;
17+
cv::Mat m_cv_img;
18+
wxImage m_wx_img;
19+
wxBitmap m_wx_bmp;
20+
wxTimer* m_timer;
21+
wxWindow* m_parent;
22+
void _copyImage( void );
23+
void OnPaint( wxPaintEvent& e );
24+
void OnTimer( wxTimerEvent& e );
25+
void OnKey( wxKeyEvent& e );
26+
protected:
27+
DECLARE_EVENT_TABLE();
28+
};

example_09-10.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "WxMoviePlayer.hpp"
2+
BEGIN_EVENT_TABLE( WxMoviePlayer, wxWindow )
3+
EVT_PAINT( WxMoviePlayer::OnPaint )
4+
EVT_TIMER( TIMER_ID, WxMoviePlayer::OnTimer )
5+
EVT_CHAR( WxMoviePlayer::OnKey )
6+
END_EVENT_TABLE()
7+
The first thing we do is to set up the callbacks that will be associated with individual
8+
events. We do this through macros provided by the wxWidgets framework.17
9+
WxMoviePlayer::WxMoviePlayer(
10+
wxWindow* parent,
11+
const wxPoint& pos,
12+
const wxSize& size
13+
) : wxWindow( parent, -1, pos, size, wxSIMPLE_BORDER ) {
14+
m_timer = NULL;
15+
m_parent = parent;
16+
}

0 commit comments

Comments
 (0)