drov0 / python-imagesearch

A wrapper around opencv2 and pyautogui to do image searching easily.
MIT License
278 stars 99 forks source link

imagesearch returns wrong coordinates #8

Open ricvillagrana opened 4 years ago

ricvillagrana commented 4 years ago

I searched for the position of an image, as your example shows, and if it is on the top-left corner the position is ok, but as it gets far from there, it will increase the coordinates even doubling them.

I divided them by 2 and it is close to the actual position but not enough:

from imagesearch import *

pos = imagesearch('notif.jpg')

if pos[0] != -1:
    print("position : ", pos[0], pos[1])
    pyautogui.moveTo(pos[0]/2, pos[1]/2)
    pyautogui.click(button="left")
else:
    print("image not found")

I am using Mac OS, also I use 2 monitors, but the problem is still there if I only use one.

drov0 commented 4 years ago

Hello, I've tested your code and it runs fine on my end (ubuntu 18, double monitors as well). Since you have a mac I assume that the problem might be related to your high resolution. I have never tested it with resolutions higher than 1920×1080.

I've had a guy in the past with a retina display that had errors and here's his comment :

"However I had troubles with retina display and the screenshot function that was returning a double resolution image. Here is the code with simple retina support, tested only on Mac OS : https://github.com/anthonyivol/python-imagesearch/blob/master/imagesearch.py "

drov0 commented 4 years ago

I've actually updated the lib with retina support, tell me how that goes for you :)

ricvillagrana commented 4 years ago

Thank you so much for your quick response. I rebased master but now the imagesearch does not find the image, it prints image not found.

I also commented the is_retina validation and it seems not to have changes in the result.

drov0 commented 4 years ago

Huh, that's odd, can you save the screencap and see if your searched image is in it ? (uncomment im.save('testarea.png'))

ricvillagrana commented 4 years ago

Yes it is. These are the images, the testarea, and the image to search. testarea img

drov0 commented 4 years ago

The size of the logo you're trying to find must match the size of the logo on the screencap. This is not a fancy machine learning image searching that will search things that resemble the input image. What opencv does is basically take the input image and then look for something of the same size and more or less the same colored pixels (this is what the tolerance parameter does btw). and if your input image is bigger than the one that's displayed, then it won't find it.

here's an example of a screencap + logo that works :

image

image

eagleEggs commented 4 years ago

Here is an option: https://www.pyimagesearch.com/2015/01/26/multi-scale-template-matching-using-python-opencv/

But if you can click by a unique value (pixel) then you save a lot of cycles. You could even do a branching pixel check from a color to rule out other colors on the screen which may be the same.