'how to zoom in and zoom out on image in opencv python with mouse wheel?
I have this python code
I need to can zoom in and zoom out on this image with mouse wheel
I am sure this is question of many people - solve this problem can solve their problem too
import cv2
if __name__ == '__main__' :
im_dst = cv2.imread('C:/11.jpg')
cv2.imshow('Image',im_dst)
I could find a way for zoom on normal image window with mouse in python by search , but it could not work on opencv window
i searched 2days on internet but i could not find a correct way (i found a way for old opencv (not cv2)) but it could not work with new opencv
any help is welcomed thanks
Solution 1:[1]
First, find the mouse event which can receive mouse wheel event. Yet, this part should be rely on yourself.
Second, the basic idea is deciding the scale changed every time on mouse wheel. After you get the current scale (v.s. origin image) and the right subregion of image you want to show on screen, you can get the position and length of rectangle on scaled image. So you can draw this rectangle on a scaled image.
In my github?checking OnMouseWheel () and RefreshSrcView () in Fastest_Image_Pattern_Matching/ELCVMatchTool/ELCVMatchToolDlg.cpp may give what you want.
Although it's c++ code, only imshow () and resize () are used, and it is easy for you to find python version.
Focusing on how I change the scale and how the new rectangle to be draw in scaled image will be enough.
Part of the code:
POINT pointCursor;
GetCursorPos (&pointCursor);
ScreenToClient (&pointCursor);
//check the wheel is forward or backward
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);
//compensationXY is value for slight error in every calculating process
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;
//every time calculating a new scale, change the horizontal&vertical bars range
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);
//recorde the slight error, and compensate it next time
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);
//scroll bar size
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);
//scroll bar size
}
else
{
m_hScrollBar.SetScrollPos (0);
m_hScrollBar.ShowWindow (SW_HIDE);
m_vScrollBar.SetScrollPos (0);
m_vScrollBar.ShowWindow (SW_HIDE);
}
//because scale is changed, refresh image
RefreshSrcView ();
return CDialogEx::OnMouseWheel (nFlags, zDelta, pt);
Solution 2:[2]
# in terminal: pip install mouse
import mouse
if mouse.wheel(1) # Zoom in / mouse wheel up
# change the image size
if mouse.wheel(-1) # Zoom out / mouse wheel down
# change the image size
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 | |
| Solution 2 | Sepehr Beheshti |


