Search code examples
pythonopencvwebmouseeventvirtual

I want to invert my mouse movement using OpenCV and python


while I'm moving my pointer to left the mouse is moving towards right and while moving to right its moving towards left... I want my mouse to move exactly the same I move the pointer..

where my screen size is 1440*900

if len(cnts) > 0:
        c = max(cnts, key=cv2.contourArea)  #Finding the largest contour in the mask
        ((x, y), radius) = cv2.minEnclosingCircle(c)  #Finding the minimum enclosing circle
        M = cv2.moments(c)  #Calculating image moment(center of mass)
        center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) #Centroid of the minimum enclosing circle
        bint = int(cv2.contourArea(c))
        if radius > 5:
            cv2.circle(frame, (int(x), int(y)), int(radius),(255,255,255), 2)  #Drawing a circle of thickness 2
            cv2.circle(frame, center, 5, (226, 43, 138), -1) #Drawing centroid on the frame
            mouse.release(Button.left)
            mouse.position=(1440-(int(x)*1440)/1440,900-(int(y)*900)/900)

Solution

  • If just the X axis is inverted, you can try this -

    mouse.position=(1440-(1440-(int(x)*1440)/1440),900-(int(y)*900)/900)
    

    If both the X and Y axes are inverted -

    mouse.position=(1440-(1440-(int(x)*1440)/1440),900-(900-(int(y)*900)/900))