简体   繁体   中英

OpenCV Python: Window Size and Mouse Events Coordinates

I'm trying to use mouse events and store the coordinates over which the cursor browsed. The problem is that my images are very small, 96x96 pixels and OpenCV chooses a window size that has bigger width than my images. So my image only takes the left side of the window. But the coordinates that are recognized by OpenCV correspond to the window size, so if I move the cursor to the middle of the window, only then are coordinates on the image itself marked at the middle. Eg in this image the cursor was placed on the middle of the window and not the image:

标签图像

I tried using the WindowResize function, but for some reason it does not work with images of such a small size, I'm assuming that this is the smallest window size in OpenCV.

Does anybody have any idea of how to make the mouse coordinates actually correspond to the coordinates in the image itself and not the window, or how to make the window size correspond exactly to the size of the image with very small images (96x96)?

I think it can be done by Scaling up your image size. Here is some python code.

scaleFactor = 10 
rows, cols = img.shape[:2] 
img = cv2.resize(img, (scaleFactor*cols, scaleFactor*rows), interpolation=cv2.INTER_LINEAR)

Then get mouse position and scale down. ( pseudo code...)

px, py = getMouseClickPosition()
px /= scaleFactor
py /= scaleFactor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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