基本参数:
- opencv获取 mat图片
- Picture Control的ID标识 IDC_STATIC2
方法1:文件读入
CStatic* pwnd = (CStatic*)GetDlgItem(IDC_STATIC2);
CRect rect;
pwnd->GetWindowRect(rect);
pwnd->ModifyStyle(0xf, SS_BITMAP | SS_CENTERIMAGE);
pwnd->SetBitmap((HBITMAP)::LoadImage(NULL, _T("F:\\biancheng\\VS2019\\mymfc\\My_MFCApp\\pic\\2.BMP"),
IMAGE_BITMAP,
rect.Width(),
rect.Height(),
LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE));
方法2:摄像头输入
using namespace cv;
cv::VideoCapture cam(0);
cv::Mat frame;
cam >> frame;
CRect rect;
GetDlgItem(IDC_STATIC)->GetClientRect(&rect);
switch (frame.channels())
{
case 1:
cv::cvtColor(frame, frame, CV_GRAY2BGRA);
break;
case 3:
cv::cvtColor(frame, frame, CV_BGR2BGRA);
break;
default:
break;
}
int pixelBytes = frame.channels() * (frame.depth() + 1);
BITMAPINFO bitInfo;
bitInfo.bmiHeader.biBitCount = 8 * pixelBytes;
bitInfo.bmiHeader.biWidth = frame.cols;
bitInfo.bmiHeader.biHeight = -frame.rows;
bitInfo.bmiHeader.biPlanes = 1;
bitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitInfo.bmiHeader.biCompression = BI_RGB;
bitInfo.bmiHeader.biClrImportant = 0;
bitInfo.bmiHeader.biClrUsed = 0;
bitInfo.bmiHeader.biSizeImage = 0;
bitInfo.bmiHeader.biXPelsPerMeter = 0;
bitInfo.bmiHeader.biYPelsPerMeter = 0;
CDC* pDC = GetDlgItem(IDC_STATIC2)->GetDC();
CStatic* pwnd = (CStatic*)GetDlgItem(IDC_STATIC2);
pwnd->GetWindowRect(rect);
::StretchDIBits(
pDC->GetSafeHdc(),
0, 0, rect.Width(), rect.Height(),
0, 0, frame.cols, frame.rows,
frame.data,
&bitInfo,
DIB_RGB_COLORS,
SRCCOPY
);
ReleaseDC(pDC);
方法3:窗口覆盖
cv::namedWindow("view", cv::WINDOW_AUTOSIZE);
HWND hWnd = (HWND)cvGetWindowHandle("view");
HWND hParent = ::GetParent(hWnd);
::SetParent(hWnd, GetDlgItem(IDC_STATIC2)->m_hWnd);
::ShowWindow(hParent, SW_HIDE);
using namespace cv;
cv::VideoCapture cam(0);
cv::Mat frame;
cam >> frame;
CRect rect;
GetDlgItem(IDC_STATIC2)->GetClientRect(&rect);
Rect dst(rect.left, rect.top, rect.right, rect.bottom);
resize(frame, frame, cv::Size(rect.Width(), rect.Height()));
imshow("view", frame);