반응형


웹캠으로 부터 입력된 영상을 wxwidgets의 panel에 출력해주는 프로그램을 참고에 있는 소스코드를 참고하여 구현했습니다.

 






컴파일을 하려면..


$ g++ -o main main.cpp ` wx-config --cflags --libs` `pkg-config opencv --libs --cflags`




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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <wx/wx.h>
#include <wx/sizer.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
 
 
#define TIMER_ID 1000
 
 
using namespace cv;
using namespace std;
 
class wxImagePanel : public wxPanel
{
    wxBitmap image;
    wxTimer timer;
    VideoCapture capture;
 
    DECLARE_EVENT_TABLE()
 
public:
    wxImagePanel(wxFrame* parent);
 
    void paintEvent(wxPaintEvent & evt);
    void paintNow();
    void OnTimer( wxTimerEvent& event ); 
    void render(wxDC& dc);
 
};
 
 
BEGIN_EVENT_TABLE(wxImagePanel, wxPanel)
    // catch paint events
    EVT_PAINT(wxImagePanel::paintEvent)
 
    EVT_TIMER(TIMER_ID, wxImagePanel::OnTimer)
END_EVENT_TABLE()
 
 
 
wxImagePanel::wxImagePanel(wxFrame* parent) :
wxPanel(parent),timer(this, TIMER_ID)
{
    capture = VideoCapture(0);
    timer.Start(1000/30);//30 fps
}
 
 
/*
* Called by the system of by wxWidgets when the panel needs
* to be redrawn. You can also trigger this call by
* calling Refresh()/Update().
*/
 
void wxImagePanel::paintEvent(wxPaintEvent & evt)
{
     // depending on your system you may need to look at double-buffered dcs
    wxPaintDC dc(this);
    render(dc);
}
 
 
 
void wxImagePanel::OnTimer(wxTimerEvent& event)
{
    Mat frame;
 
    capture >> frame;
 
    if (frame.channels() == 3)
         cv::cvtColor(frame, frame, CV_BGR2RGB);
 
    wxImage tmp = wxImage(frame.cols, frame.rows, frame.data, TRUE);
    image = wxBitmap(tmp);
 
    paintNow();
}
 
 
 
/*
* Alternatively, you can use a clientDC to paint on the panel
* at any time. Using this generally does not free you from
* catching paint events, since it is possible that e.g. the window
* manager throws away your drawing when the window comes to the
* background, and expects you will redraw it when the window comes
* back (by sending a paint event).
*/
void wxImagePanel::paintNow()
{
    // depending on your system you may need to look at double-buffered dcs
    wxClientDC dc(this);
    render(dc);
}
 
/*
* Here we do the actual rendering. I put it in a separate
* method so that it can work no matter what type of DC
* (e.g. wxPaintDC or wxClientDC) is used.
*/
void wxImagePanel::render(wxDC&  dc)
{
    dc.DrawBitmap(image, 00false);
}
 
 
 
class MyApp : public wxApp
{
 
    wxFrame *frame;
    wxImagePanel * drawPane;
public:
    bool OnInit()
    {
        // make sure to call this first
        wxInitAllImageHandlers();
 
        wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
        frame = new wxFrame(NULL, wxID_ANY, wxT("wxBitmap"), wxPoint(5050), wxSize(800600));
 
        drawPane = new wxImagePanel(frame);
        sizer->Add(drawPane, 1, wxEXPAND);
 
        frame->SetSizer(sizer);
 
        frame->Show();
        return true;
    }
 
};
 
IMPLEMENT_APP(MyApp)
 
cs





참고


http://kimoscode.blogspot.kr/2010/02/opencv-20-%EC%97%90%EC%84%9C-%EC%9B%B9%EC%BA%A0-%EC%98%81%EC%83%81-%EC%BA%A1%EC%B2%98%ED%95%98%EA%B8%B0.html


http://nzkyss.blogspot.kr/2014/07/wxwidgetsopencv.html

반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts