'OpenCV zooming in on Mat image without the window border changing c++

I have a window Mat gestures containing an image, I want to zoom in every pixel in the window but keep the border the same size. I have tried resize() but it's resizing the border as well.

Image

For better explanation, I don't want the border that is in the green box to be resized as well as the whole border, but I need the image inside the border to be resized. How can I achieve this?



Solution 1:[1]

Set a ROI of the image excluding the border. If you already know the thickness, simply assign a new img from it. Then you can resize and draw cv::rectangle with the thickness of original image.

Following code snippet may not compile since I don't see a reproducible code.

cv::Mat img = cv::imread(...);

const int thick = 3;
const cv::Rect roi(thick, thick, img.width()-2*thick, img.height()-2*thick);
cv::Mat img_roi = img(roi);

cv::resize(...);  // resize img_roi
cv::rectangle(...);  // draw new border on img_roi, you need to pass a cv::Scalar value from img.at(0, 0) for the color of it.

However, I'm expecting a better idea from someone else.

Solution 2:[2]

The basic idea is deciding the scale changed every time on mouse wheel. After you get the current scale (v.s. origin image), you then can get the position and length of rectangle on scaled image.

In my github?checking OnMouseWheel () and RefreshSrcView () in Fastest_Image_Pattern_Matching/ELCVMatchTool/ELCVMatchToolDlg.cpp may give what you want.

Besides, if you only want to use opencv window without MFC framework or other frameworks, check this (pure OpenCV version)

Effect: enter image description here enter image description here

Part of the code:

BOOL CELCVMatchToolDlg::OnMouseWheel (UINT nFlags, short zDelta, CPoint pt)
{
    POINT pointCursor;
    GetCursorPos (&pointCursor);
    ScreenToClient (&pointCursor);
    // TODO: ???????????????? (?) ?????
    if (zDelta > 0)
    {
        if (m_iScaleTimes == MAX_SCALE_TIMES)
            return TRUE;
        else
            m_iScaleTimes++;
    }
    if (zDelta < 0)
    {
        if (m_iScaleTimes == MIN_SCALE_TIMES)
            return TRUE;
        else
            m_iScaleTimes--;
    }
    CRect rect;
    //GetWindowRect (rect);
    GetDlgItem (IDC_STATIC_SRC_VIEW)->GetWindowRect (rect);//??

    if (m_iScaleTimes == 0)
        g_dCompensationX = g_dCompensationY = 0;

    int iMouseOffsetX = pt.x - (rect.left + 1);
    int iMouseOffsetY = pt.y - (rect.top + 1);

    double dPixelX = (m_hScrollBar.GetScrollPos () + iMouseOffsetX + g_dCompensationX) / m_dNewScale;
    double dPixelY = (m_vScrollBar.GetScrollPos () + iMouseOffsetY + g_dCompensationY) / m_dNewScale;


    m_dNewScale = m_dSrcScale * pow (SCALE_RATIO, m_iScaleTimes);

    if (m_iScaleTimes != 0)
    {
        int iWidth = m_matSrc.cols;
        int iHeight = m_matSrc.rows;

        
        m_hScrollBar.SetScrollRange (0, int (m_dNewScale * iWidth - m_dSrcScale * iWidth) - 1 + BAR_SIZE);
        m_vScrollBar.SetScrollRange (0, int (m_dNewScale * iHeight - m_dSrcScale * iHeight) - 1 + BAR_SIZE);
        int iBarPosX = int (dPixelX * m_dNewScale - iMouseOffsetX + 0.5);
        m_hScrollBar.SetScrollPos (iBarPosX);
        m_hScrollBar.ShowWindow (SW_SHOW);
        g_dCompensationX = -iBarPosX + (dPixelX * m_dNewScale - iMouseOffsetX);

        int iBarPosY = int (dPixelY * m_dNewScale - iMouseOffsetY + 0.5);
        m_vScrollBar.SetScrollPos (iBarPosY);
        m_vScrollBar.ShowWindow (SW_SHOW);
        g_dCompensationY = -iBarPosY + (dPixelY * m_dNewScale - iMouseOffsetY);

        //????
        SCROLLINFO infoH;
        infoH.cbSize = sizeof (SCROLLINFO);
        infoH.fMask = SIF_PAGE;
        infoH.nPage = BAR_SIZE;
        m_hScrollBar.SetScrollInfo (&infoH);

        SCROLLINFO infoV;
        infoV.cbSize = sizeof (SCROLLINFO);
        infoV.fMask = SIF_PAGE;
        infoV.nPage = BAR_SIZE;
        m_vScrollBar.SetScrollInfo (&infoV);
        //????

    }
    else
    {
        m_hScrollBar.SetScrollPos (0);
        m_hScrollBar.ShowWindow (SW_HIDE);
        m_vScrollBar.SetScrollPos (0);
        m_vScrollBar.ShowWindow (SW_HIDE);
    }
    RefreshSrcView ();
    return CDialogEx::OnMouseWheel (nFlags, zDelta, pt);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John Park
Solution 2