MFC对话框和Opencv结合显示图像,有时候图像的大小要大于对话框的大小,图像显示不完全,可以在对话框上添加水平和垂直方向的滑动块,方便查看整个图像。
(1)OnInitDialog()函数,实现图像读取和创建opencv窗口,并且与MFC对话框结合使用
// TODO: 在此添加额外的初始化代码
BOOL CShowImageByScrollbarDlg::OnInitDialog(){
const char* file="C:/Users/dell/Downloads/1.jpg";
src_img=cv::imread(file,1);
namedWindow("IPCamera", WINDOW_AUTOSIZE);
HWND hWnd_CAM1 = (HWND)cvGetWindowHandle("IPCamera");
HWND hParent_CAM1 = ::GetParent(hWnd_CAM1);
::SetParent(hWnd_CAM1, AfxGetMainWnd()->m_hWnd);
::ShowWindow(hParent_CAM1, 0); //隐藏运行程序框
AfxGetMainWnd()->ShowWindow(1);
myShowImageScroll() ;
return TRUE;
}
(2)myShowImageScroll()函数完成滑动块的初始化设置,显示图像
void CShowImageByScrollbarDlg::myShowImageScroll()
{
if(!src_img.empty())
{
imageWidth=src_img.cols;
imageHeight=src_img.rows;
}
CRect WinRect;
this->GetClientRect(&WinRect);
WindowWidth=WinRect.Width();
WindowHeight=WinRect.Height();
double scale_w = (double)imageWidth / (double)WindowWidth; // 源图像与窗口的宽度比值 用以判断是否超出显示范围
double scale_h = (double)imageHeight / (double)WindowHeight; // 源图像与窗口的高度比值 用以判断是否超出显示范围
LPSCROLLINFO lpsi;
lpsi = new SCROLLINFO;
m_nHorzMaxSize = imageWidth;
m_nVertMaxSize = imageHeight;
m_nHorzPageSize =WindowWidth;
m_nVertPageSize =WindowHeight;
m_nOneStep = 1;
m_nHorzPos = 0;
m_nVertPos = 0;
this->SetScrollRange( SB_HORZ,0,m_nHorzMaxSize);
this->SetScrollRange( SB_VERT,0,m_nVertMaxSize);
this->GetScrollInfo(SB_HORZ,lpsi);
lpsi->nPage = m_nHorzPageSize;
this->SetScrollInfo(SB_HORZ,lpsi);
this->GetScrollInfo(SB_VERT,lpsi);
lpsi->nPage = m_nVertPageSize;
this->SetScrollInfo(SB_VERT,lpsi);
//DarwPic();
imshow("IPCamera",src_img);
}
(3)OnVScroll垂直对话框和OnHScroll水平对话框的响应函数,在拖动滑动块时会自动调用对应的函数
void CShowImageByScrollbarDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
switch(nSBCode)
{
case SB_LEFT:
m_nHorzPos = 0;
this->SetScrollPos(SB_HORZ,m_nHorzPos);
DarwPic();
break;
case SB_LINELEFT:
case SB_PAGELEFT:
m_nHorzPos -= m_nOneStep;
if(m_nHorzPos<0)
{
m_nHorzPos = 0;
}
this->SetScrollPos(SB_HORZ,m_nHorzPos);
DarwPic();
break;
case SB_LINERIGHT:
case SB_PAGERIGHT:
m_nHorzPos += m_nOneStep;
if(m_nHorzPos>m_nHorzMaxSize)
{
m_nHorzPos = m_nHorzMaxSize;
}
this->SetScrollPos(SB_HORZ,m_nHorzPos);
DarwPic();
break;
case SB_RIGHT:
m_nHorzPos = m_nHorzMaxSize;
this->SetScrollPos(SB_HORZ,m_nHorzPos);
DarwPic();
break;
case SB_THUMBPOSITION:
case SB_THUMBTRACK:
m_nHorzPos = nPos;
this->SetScrollPos(SB_HORZ,m_nHorzPos);
DarwPic();
break;
default:
break;
}
CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}
void CShowImageByScrollbarDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
switch(nSBCode)
{
case SB_LEFT:
m_nVertPos = 0;
this->SetScrollPos(SB_VERT,m_nVertPos);
DarwPic();
break;
case SB_LINELEFT:
case SB_PAGELEFT:
m_nVertPos -= m_nOneStep;
if(m_nVertPos<0)
{
m_nVertPos = 0;
}
this->SetScrollPos(SB_VERT,m_nVertPos);
DarwPic();
break;
case SB_LINERIGHT:
case SB_PAGERIGHT:
m_nVertPos += m_nOneStep;
if(m_nVertPos>m_nVertMaxSize)
{
m_nVertPos = m_nVertMaxSize;
}
this->SetScrollPos(SB_VERT,m_nVertPos);
DarwPic();
break;
case SB_RIGHT:
m_nVertPos = m_nVertMaxSize;
this->SetScrollPos(SB_VERT,m_nVertPos);
DarwPic();
break;
case SB_THUMBPOSITION:
case SB_THUMBTRACK:
m_nVertPos = nPos;
this->SetScrollPos(SB_VERT,m_nVertPos);
DarwPic();
break;
default:
break;
}
CDialogEx::OnVScroll(nSBCode, nPos, pScrollBar);
}
void CShowImageByScrollbarDlg::DarwPic()
{
// 设置窗口显示区的 ROI
rect_dst = Rect(0, 0, WindowWidth,WindowHeight);
int src_x = min(m_nHorzPos, imageWidth-WindowWidth);
int src_y = min(m_nVertPos, imageHeight-WindowHeight);
rect_src = Rect(src_x ,src_y, WindowWidth,WindowHeight);
src_img(rect_src).convertTo(dstROI, src_img.type());
imshow("IPCamera", dstROI);
}