-
Notifications
You must be signed in to change notification settings - Fork 901
/
example_09-11.cpp
176 lines (170 loc) · 3.67 KB
/
example_09-11.cpp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Example 9-11. An example header file for our custom View class
//
class COpenCVTestView : public CWindowImpl<COpenCVTestView> {
public:
DECLARE_WND_CLASS(NULL)
bool OpenFile(std::string file);
void _copyImage();
BOOL PreTranslateMessage(MSG* pMsg);
BEGIN_MSG_MAP(COpenCVTestView)
MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBkgnd)
MESSAGE_HANDLER(WM_PAINT, OnPaint)
MESSAGE_HANDLER(WM_TIMER, OnTimer)
END_MSG_MAP()
// Handler prototypes (uncomment arguments if needed):
// LRESULT MessageHandler(
// UINT /*uMsg*/,
// WPARAM /*wParam*/,
// LPARAM /*lParam*/,
// BOOL& /*bHandled*/
// );
// LRESULT CommandHandler(
// WORD /*wNotifyCode*/,
// WORD /*wID*/,
// HWND /*hWndCtl*/,
// BOOL& /*bHandled*/
// );
// LRESULT NotifyHandler(
// int /*idCtrl*/,
// LPNMHDR /*pnmh*/,
// BOOL& /*bHandled*/
// );
LRESULT OnPaint(
UINT /*uMsg*/,
WPARAM /*wParam*/,
LPARAM /*lParam*/,
BOOL& /*bHandled*/
);
LRESULT OnTimer(
UINT /*uMsg*/,
WPARAM /*wParam*/,
LPARAM /*lParam*/,
BOOL& /*bHandled*/
);
LRESULT OnEraseBkgnd(
UINT /*uMsg*/,
WPARAM /*wParam*/,
LPARAM /*lParam*/,
BOOL& /*bHandled*/
);
private:
cv::VideoCapture m_cap;
cv::Mat m_cv_img;
RGBTRIPLE* m_bitmapBits;
};
LRESULT CMainFrame::OnFileOpen(
WORD /*wNotifyCode*/,
WORD /*wID*/,
HWND /*hWndCtl*/,
BOOL& /*bHandled*/
) {
WTL::CFileDialog dlg(TRUE);
if (IDOK == dlg.DoModal(m_hWnd)) {
m_view.OpenFile(dlg.m_szFileName);
}
return 0;
}
bool COpenCVTestView::OpenFile(std::string file) {
if( !m_cap.open( file ) ) return false;
// If we opened the file, set up everything now:
//
m_cap.read( m_cv_img );
// could create a DIBSection here, but let's just allocate memory for raw bits
//
m_bitmapBits = new RGBTRIPLE[m_cv_img.cols * m_cv_img.rows];
_copyImage();
SetTimer(0, 1000.0f / m_cap.get( cv::CAP_PROP_FPS ) );
return true;
}
void COpenCVTestView::_copyImage() {
// Copy the image data into the bitmap
//
cv::Mat cv_header_to_qt_image(
cv::Size(
m_cv_img.cols,
m_cv_img.rows
),
CV_8UC3,
m_bitmapBits
);
cv::cvtColor( m_cv_img, cv_header_to_qt_image, cv::BGR2RGB );
}
LRESULT COpenCVTestView::OnPaint(
UINT
/* uMsg
*/,
WPARAM /* wParam
*/,
LPARAM /* lParam
*/,
BOOL& /* bHandled */
) {
CPaintDC dc(m_hWnd);
WTL::CRect rect;
GetClientRect(&rect);
if( m_cap.isOpened() ) {
BITMAPINFO bmi = {0};
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biWidth
= m_cv_img.cols;
// note that bitmaps default to bottom-up, use negative height to
// represent top-down
//
bmi.bmiHeader.biHeight = m_cv_img.rows * -1;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
dc.StretchDIBits(
0,
rect.Width(),
0,
bmi.bmiHeader.biWidth,
m_bitmapBits,
&bmi,
DIB_RGB_COLORS,
SRCCOPY
// 32 if you use RGBQUADs for the bits
0,
rect.Height(),
0,
abs(bmi.bmiHeader.biHeight),
Working with Windows
|
245);
} else {
dc.FillRect(rect, COLOR_WINDOW);
}
return 0;
}
RESULT COpenCVTestView::OnTimer(
UINT
/* uMsg
*/,
WPARAM /* wParam
*/,
LPARAM /* lParam
*/,
BOOL& /* bHandled */
) {
// Nothing to do if capture object is not open
//
if( !m_cap.isOpened() ) return 0;
m_cap.read( m_cv_img );
_copyImage();
Invalidate();
return 0;
}
LRESULT COpenCVTestView::OnEraseBkgnd(
UINT
/* uMsg
*/,
WPARAM /* wParam
*/,
LPARAM /* lParam
*/,
BOOL& /* bHandled */
) {
// since we completely paint our window in the OnPaint handler, use
// an empty background handler
return 0;
}