【发布时间】:2021-01-31 10:31:17
【问题描述】:
所以,我有一张具有透明背景和尺寸(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,Y, new_Width , 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)
image_resized = cv2.resize(image,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,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,Y, new_Width , new_High]
match_number += 1
high += 1
print(ocurrencies)
cv2.waitKey(0)
return ocurrencies
我不知道是否有更好的方法来做到这一点,如果有,请告诉我,我很乐意改进代码(必须尽可能快),但主要问题是:它无法匹配具有透明度的模板,以及透明度渐变背景。
所以我有这张图片:
并且程序无法找到图像并获取它的坐标。
*编辑:
所以我可以拍下这张照片:
但是当我尝试拍摄具有透明背景的 .png 图片时,它不起作用:
(模板)并尝试在这个上找到匹配项 (screen1) 或这个(screen2)。
您可以注意到第一个模板的尺寸与 screen1 相同,但背景为深灰色,而 screen2 的背景也不同且较小(但保持原来的Width/High比例)比原来的。
所以我想要的是能够使用 template 在屏幕的某个位置找到 screen1 或 screen2。
【问题讨论】:
标签: python python-3.x image image-processing pyautogui