如何解决如何在屏幕上查找具有透明背景的图像?
因此,我有一幅具有透明背景和尺寸(Width_1
,High_1
)的图像,而我想做的就是找到该图像(但不必使用相同的尺寸)在屏幕上以尺寸(Width_2
,High_2
)来表示,因此我谨记以下代码:
def resize_untill_find(image,bigger_or_smaller,min_size = 50,max_size = 150,one_match_per_point = 'on'): #(imagem,diminuir ou aumentar,min_size = reduce the image until min_size%,max_size = expand the image until max_size%)
print('Press CTRL+ALT to start:')
while True:
if keyboard.is_pressed('control+alt'):
break
image_info = Ferramentas.get_image_first_info(image)# get width,high,and pixels number off the image
image = cv2.imread(image)
image_rate = image_info['whidth'] / image_info['high'] #proportion Width/High
ocurrencies = {} # [x,y,w,h]
match_number = 1 # number of the ocurrency
high = image_info['high']
if bigger_or_smaller == 'smaller':
while int((high/image_info['high'])*100) >= int(min_size): # while the resize are not under min_size
print('{}%'.format(round((high/image_info['high'])*100)))
width = round(image_info['whidth']-((image_info['high'] - high) * image_rate))
dimension = (width,high) # new dimension of the image
print('Dimension:',dimension)
image_resized = cv2.resize(image,dimension) #resize the image
matches_position = pyautogui.locateallOnScreen(image_resized,confidence = 0.9)
for region in matches_position:
region = list(region)
ocurrencies['Match_{}'.format(match_number)] = region # region = [X,Y,new_Width,new_High]
match_number += 1
if one_match_per_point == 'on':
match_position = pyautogui.locateOnScreen(image_resized,confidence = 0.9)
if match_position != None:
match_position = list(match_position)
ocurrencies['Match_{}'.format(match_number)] = match_position # region = [X,new_High]
match_number += 1
high -= 1
if bigger_or_smaller == 'bigger':
while int((high/image_info['high'])*100) <= int(max_size):
print('{}%'.format(round((high/image_info['high'])*100)))
width = round(image_info['whidth']+((high - image_info['high']) * image_rate))
dimension = (width,high) # image new dimension
print('Dimension:',dimension) #resize the imge with new dimensions
if one_match_per_point == 'off':
matches_position = pyautogui.locateallOnScreen(image_resized,confidence = 0.9) # lista todos os matches 90% iguais ao da imagem redimensionada
for region in matches_position:
region = list(region)
ocurrencies['Match_{}'.format(match_number)] = region # region = [X,new_High]
match_number += 1
if one_match_per_point == 'on':
match_position = pyautogui.locateOnScreen(image_resized,new_High]
match_number += 1
high += 1
print(ocurrencies)
cv2.waitKey(0)
return ocurrencies
我不知道是否有更好的方法来执行此操作,如果有,请告诉我,我将很高兴改进代码(它必须尽可能快),但是主要问题是:它无法匹配具有透明度和透明度渐变背景的模板。
,程序无法找到图像并获取坐标。
* 编辑:
所以我可以拍摄这张照片:
但是当我尝试用透明背景拍摄.png图片时,它不起作用:
(模板),然后尝试在此模板上找到匹配项 ( screen1 )或此( screen2 )。
您会注意到,第一个模板的尺寸与 screen1 相同,但是背景为深灰色,而 screen2 的背景也不同,并且较小(但保持原始的“宽/高”比例)。
所以我想要的是能够使用 template 在屏幕的某个位置找到 screen1 或 screen2 。 / p>
解决方法
def search(screen,img):
sx,sy = screen.size
ix,iy = img.size
for xstart in range(sx - ix):
for ystart in range(sy - iy):
#search for the pixel on the screen that equals the pixel at img[0:0]
if img.getpixel((0,0)) == screen.getpixel((xstart,ystart)):
match = 1 #temporary
for x in range(ix): #check if first row of img is on this coords
if img.getpixel((x,0)) <> screen.getpixel((xstart+x,ystart)):
match = 0 #if there's any difference,exit the loop
break
if match == 1: #otherwise,if this coords matches the first row of img
for x in range(ix):
for y in range(iy):
#check every pixel of the img
if img.getpixel((x,y)) <> screen.getpixel((xstart+x,ystart+y)):
match = 0 #any difference - break
break
if match == 1: return (xstart,ystart) #return top-left corner coordinates
return (-1,-1) #or this,if not found
#i found this somewhere else
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。