def autoCompareResImgHashValue(handle,fileName): imgPath=path.getResDirPath()+fileName tmp=fileName.split(".") fSplit=tmp[0].split("_") fLen=len(fSplit) targetImgPerLeftX=int(fSplit[fLen-4]) targetImgPerLeftY=int(fSplit[fLen-3]) targetImgPerRightX=int(fSplit[fLen-2]) targetImgPerRightY=int(fSplit[fLen-1]) targetImg=Image.open(imgPath) targetImgWidth=targetImg.size[0] targetImgHeigth=targetImg.size[1] perSize=(targetImgPerRightX-targetImgPerLeftX)*0.01 resWinWidth=int(targetImgWidth/perSize) perSize=(targetImgPerRightY-targetImgPerLeftY)*0.01 resWinHeight=int(targetImgHeigth/perSize) imgScreen=getScreenRectPerImg(handle,targetImgPerLeftX,targetImgPerLeftY,targetImgPerRightX,targetImgPerRightY) imgScreen.resize((resWinWidth,resWinHeight),Image.ANTIALIAS) hashCode1=imgHash(imgScreen,hashSize,highfreq_factor) hashCode2=imgHash(targetImg,hashSize,highfreq_factor) targetImg.close() imgScreen.close() return alikeHashValue(hashCode2,hashCode1)
def matchResImgInWindow(handle,imgName,threshold=0.8,mult=True): #获取目标图片 tmp=imgName.split(".") fSplit=tmp[0].split("_") fLen=len(fSplit) targetImgPerLeftX=int(fSplit[fLen-4]) targetImgPerLeftY=int(fSplit[fLen-3]) targetImgPerRightX=int(fSplit[fLen-2]) targetImgPerRightY=int(fSplit[fLen-1]) imgPath=path.getResDirPath()+imgName if not os.path.exists(path.getProjectPath()): os.makedirs(path.getProjectPath()) targetImg=Image.open(imgPath) targetImgWidth=targetImg.size[0] targetImgHeigth=targetImg.size[1] perSize=(targetImgPerRightX-targetImgPerLeftX)*0.01 resWinWidth=int(targetImgWidth/perSize) perSize=(targetImgPerRightY-targetImgPerLeftY)*0.01 resWinHeight=int(targetImgHeigth/perSize) #模板图片 temImg=cv2.cvtColor(numpy.asarray(targetImg),cv2.COLOR_RGB2BGR) targetImg.close() wLeft, wTop, wRight, wBottom = appGetWindowRect(handle) winImg = ImageGrab.grab(bbox=(wLeft, wTop, wRight, wBottom)) #对截图缩放,适配资源图片 toMatchWinImgSrc=cv2.cvtColor(numpy.asarray(winImg),cv2.COLOR_RGB2BGR) toMatchWinImg=cv2.resize(toMatchWinImgSrc, (resWinWidth,resWinHeight),interpolation=cv2.INTER_AREA) winImg.close() res = cv2.matchTemplate(toMatchWinImg,temImg,cv2.TM_CCOEFF_NORMED) xyList=[] if mult==True : loc = numpy.where(res>=threshold) for pt in zip(*loc[::-1]): xyList.append((wLeft+pt[0]+(targetImgWidth>>1),wTop+pt[1]+(targetImgHeigth>>1))) else: #单个很不准确 min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) top_left = min_loc #左上角的位置 xyList.append((wLeft+top_left[0]+(targetImgWidth>>1),wTop+top_left[1]+(targetImgHeigth>>1))) # print(xyList) return xyList
def getResImgHashAndSize(fileName): imgPath=path.getResDirPath()+fileName if not os.path.exists(path.getProjectPath()): os.makedirs(path.getProjectPath()) img=Image.open(imgPath) xSize=img.size[0] ySize=img.size[1] hashCode=imgHash(img,hashSize,highfreq_factor) img.close() return hashCode,xSize,ySize
def featResImgInWindow(handle, imgName,distance=0.75,threshold=8): imgPath = path.getResDirPath()+imgName if not os.path.exists(path.getProjectPath()): os.makedirs(path.getProjectPath()) targetImg = Image.open(imgPath) wLeft, wTop, wRight, wBottom = appGetWindowRect(handle) winImg = ImageGrab.grab(bbox=(wLeft, wTop, wRight, wBottom)) imgTag = cv2.cvtColor(numpy.asarray(targetImg), cv2.COLOR_RGB2BGR) imgWin = cv2.cvtColor(numpy.asarray(winImg), cv2.COLOR_RGB2BGR) # imgTag = cv2.cvtColor(numpy.asarray(targetImg), cv2.COLOR_RGB2GRAY) # imgWin = cv2.cvtColor(numpy.asarray(winImg), cv2.COLOR_RGB2GRAY) targetImg.close() winImg.close() # fast = cv2.FastFeatureDetector_create(threshold) # keypointTag = fast.detect(imgTag,None) # keypointWin = fast.detect(imgWin,None) # tmpimg=cv2.drawKeypoints(imgTag,keypointTag,outImage=numpy.array([]),color=(0,0,255)) # tmpimg=cv2.drawKeypoints(imgWin,keypointWin,outImage=numpy.array([]),color=(0,0,255)) # cv2.imshow("show key points",tmpimg) # cv2.waitKey(0) orb = cv2.ORB_create(nfeatures = 1000,edgeThreshold =threshold, nlevels = 1, patchSize = threshold) orb.setFastThreshold(0) keypointTag, desTag = orb.detectAndCompute(imgTag, None) keypointWin, desWin = orb.detectAndCompute(imgWin, None) # tmpimg=cv2.drawKeypoints(imgWin,keypointWin,outImage=numpy.array([]),color=(0,0,255)) # tmpimg = cv2.drawKeypoints( # imgTag, keypointTag, outImage=numpy.array([]), color=(0, 0, 255)) # cv2.imshow("show key points", tmpimg) # cv2.waitKey(0) # print(keypointWin,desWin) # bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) # matches = bf.match(desTag, desWin) # matches = sorted(matches, key=lambda x: x.distance) bf = cv2.BFMatcher() matches =bf.knnMatch(desTag, desWin, k=2) good = [] for m,n in matches: if m.distance < distance*n.distance: good.append([m]) print(good) img3= cv2.drawMatchesKnn(imgTag, keypointTag, imgWin, keypointWin,good,None,flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS) # img3 = cv2.drawMatches(imgTag, keypointTag, imgWin, keypointWin, matches, None, flags=2, singlePointColor=(255, 0, 0),) cv2.imshow("show key points", img3) cv2.waitKey(0)
def matchResImgInWindowOneSize(handle, imgName, threshold=0.8, mult=True): imgPath = path.getResDirPath()+imgName if not os.path.exists(path.getProjectPath()): os.makedirs(path.getProjectPath()) targetImg = Image.open(imgPath) targetImgWidth = targetImg.size[0] targetImgHeigth = targetImg.size[1] # 模板图片 temImg = cv2.cvtColor(numpy.asarray(targetImg), cv2.COLOR_RGB2GRAY) # temImg = cv2.cvtColor(numpy.asarray(targetImg), cv2.COLOR_RGB2GRAY) targetImg.close() wLeft, wTop, wRight, wBottom = appGetWindowRect(handle) winImg = ImageGrab.grab(bbox=(wLeft, wTop, wRight, wBottom)) # 对截图缩放,适配资源图片 toMatchWinImgSrc = cv2.cvtColor(numpy.asarray(winImg), cv2.COLOR_RGB2GRAY) toMatchWinImg = cv2.resize( toMatchWinImgSrc, (396, 698), interpolation=cv2.INTER_AREA) winImg.close() res = cv2.matchTemplate(toMatchWinImg, temImg, cv2.TM_CCOEFF_NORMED) xyList = [] if mult == True: loc = numpy.where(res >= threshold) for pt in zip(*loc[::-1]): x = wLeft+int((pt[0]+(targetImgWidth >> 1))) y = wTop+int((pt[1]+(targetImgHeigth >> 1))) xyList.append((x, y)) else: # 单个很不准确 x = wLeft+int((pt[0]+(targetImgWidth >> 1))) y = wTop+int((pt[1]+(targetImgHeigth >> 1))) xyList.append((x, y)) print(xyList[:10]) return xyList
def getResImgHash(fileName): imgPath=path.getResDirPath()+fileName if not os.path.exists(path.getProjectPath()): os.makedirs(path.getProjectPath()) return imgHash(Image.open(imgPath),hashSize,highfreq_factor)
def matchResImgInWindowPerSize(handle, imgName, threshold=0.8, mult=True): tmp = imgName.split(".") fSplit = tmp[0].split("_") fLen = len(fSplit) targetImgPerLeftX = int(fSplit[fLen-4]) targetImgPerLeftY = int(fSplit[fLen-3]) targetImgPerRightX = int(fSplit[fLen-2]) targetImgPerRightY = int(fSplit[fLen-1]) imgPath = path.getResDirPath()+imgName if not os.path.exists(path.getProjectPath()): os.makedirs(path.getProjectPath()) targetImg = Image.open(imgPath) targetImgWidth = targetImg.size[0] targetImgHeigth = targetImg.size[1] perSizeW = (targetImgPerRightX-targetImgPerLeftX)*0.01 resWinWidth = int(targetImgWidth/perSizeW) perSizeH = (targetImgPerRightY-targetImgPerLeftY)*0.01 resWinHeight = int(targetImgHeigth/perSizeH) # 模板图片 temImg = cv2.cvtColor(numpy.asarray(targetImg), cv2.COLOR_RGB2GRAY) # temImg = cv2.cvtColor(numpy.asarray(targetImg), cv2.COLOR_RGB2GRAY) targetImg.close() wLeft, wTop, wRight, wBottom = appGetWindowRect(handle) winImg = ImageGrab.grab(bbox=(wLeft, wTop, wRight, wBottom)) winNowW = wRight-wLeft winNowH = wBottom-wTop # 对截图缩放,适配资源图片 toMatchWinImgSrc = cv2.cvtColor(numpy.asarray(winImg), cv2.COLOR_RGB2GRAY) toMatchWinImg = cv2.resize( toMatchWinImgSrc, (resWinWidth, resWinHeight), interpolation=cv2.INTER_AREA) winImg.close() scaleValueW = winNowW/resWinWidth scaleValueH = winNowH/resWinHeight res = cv2.matchTemplate(toMatchWinImg, temImg, cv2.TM_CCOEFF_NORMED) xyList = [] if mult == True: loc = numpy.where(res >= threshold) for pt in zip(*loc[::-1]): x = wLeft+int((pt[0]+(targetImgWidth >> 1))*scaleValueW) y = wTop+int((pt[1]+(targetImgHeigth >> 1))*scaleValueW) xyList.append((x, y)) else: # 单个很不准确 x = wLeft+int((pt[0]+(targetImgWidth >> 1))*scaleValueW) y = wTop+int((pt[1]+(targetImgHeigth >> 1))*scaleValueW) xyList.append((x, y)) print(xyList[:10]) return xyList