opencv와 wxwidgets을 연동하여 웹캠에서 캡처한 영상을 화면에 출력하기OpenCV/OpenCV 강좌2016. 6. 7. 13:03
Table of Contents
반응형
웹캠으로 부터 입력된 영상을 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, 0, 0, false); } 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(50, 50), wxSize(800, 600)); 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
반응형
'OpenCV > OpenCV 강좌' 카테고리의 다른 글
opencv를 이용한 영상 이진화(binarization, thresholding) (12) | 2016.08.31 |
---|---|
opencv 윈도우 상에서 마우스 클릭한 위치 출력하기 (0) | 2016.07.06 |
RANSAC을 이용한 Line fitting (0) | 2016.05.27 |
opencv를 이용하여 wxPython에서 웹캠 영상 띄우기 (0) | 2016.05.23 |
openCV 라벨링 예제 ( connectedComponentsWithStats ) (65) | 2015.11.19 |