简体   繁体   English

如何使用鼠标滚轮放大和缩小opencv python中的图像?

[英]how to zoom in and zoom out on image in opencv python with mouse wheel?

I have this python code我有这个 python 代码

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我可以通过搜索在python中找到一种用鼠标放大普通图像窗口的方法,但它不能在opencv窗口上工作

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我在互联网上搜索了 2 天,但找不到正确的方法(我找到了旧 opencv(不是 cv2)的方法),但它不能与新的 opencv 一起使用

any help is welcomed thanks欢迎任何帮助谢谢

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 (vs 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 mygithub ,checking OnMouseWheel () and RefreshSrcView () in Fastest_Image_Pattern_Matching/ELCVMatchTool/ELCVMatchToolDlg.cpp may give what you want.在我的github中,检查 Fastest_Image_Pattern_Matching/ELCVMatchTool/ELCVMatchToolDlg.cpp 中的 OnMouseWheel() 和 RefreshSrcView() 可能会得到你想要的。

Although it's c++ code, only imshow () and resize () are used, and it is easy for you to find python version.虽然是c++代码,但只用到了imshow()和resize(),很容易找到python版本。

Focusing on how I change the scale and how the new rectangle to be draw in scaled image will be enough.专注于我如何更改比例以及如何在缩放图像中绘制新矩形就足够了。

Effect:影响: ![在此处输入图像描述 ![在此处输入图像描述

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);    
# 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM