Skip to content

Commit

Permalink
Update example_09-11.cpp
Browse files Browse the repository at this point in the history
Comments up front, extensive changes in the back
  • Loading branch information
garybradski authored May 25, 2017
1 parent 28c90b2 commit ad5aee5
Showing 1 changed file with 125 additions and 1 deletion.
126 changes: 125 additions & 1 deletion example_09-11.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Example 9-11. An example header file for our custom View class
//
class COpenCVTestView : public CWindowImpl<COpenCVTestView> {
public:
DECLARE_WND_CLASS(NULL)
Expand Down Expand Up @@ -49,4 +51,126 @@ class COpenCVTestView : public CWindowImpl<COpenCVTestView> {
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;
}

0 comments on commit ad5aee5

Please sign in to comment.