爬虫滑块计算图片之间的距离更加精确

1.思路

原先图片匹配一般都是缺口匹配全图
优化点:
    1.缺口图片匹配缺口所在图片那一行图片可以提高他识别率
    2.移动后再进行2次匹配计算距离

2.代码

#.缺口图片匹配缺口所在图片那一行图片可以提高他识别率
def get_image_deviation():
    ##读取滑块图
    block = cv.imread("img.png", -1) #完整图片有个缺口
    backimg = cv.imread("bg_img.png") #缺口图片
    # block = cv.resize(block, (240, 480))
    # backimg = cv.resize(block, (240, 480))
    ##灰度化
    gray_backimg = cv.cvtColor(backimg, cv.COLOR_RGB2GRAY)
    blockWidth, blockHeight = block.shape[1], block.shape[0]
    ##识别滑块图前景
    ###由于滑块图为带透明的png,可根据透明通道来判断前景位置
    ##识别物体框,生成blockmask
    left = blockWidth
    right = 0
    top = blockHeight
    bottom = 0
    for i in range(0, blockHeight):
        for j in range(0, blockWidth):
            if block[i, j, 3] > 0:
                if j <= left:
                    left = j
                if j >= right:
                    right = j
                if i <= top:
                    top = i
                if i >= bottom:
                    bottom = i
    blockBox = block[top:bottom, left:right]
    blockBox_width, blockBox_height = blockBox.shape[1], blockBox.shape[0]
    print(blockBox_width)
    blockMask = np.zeros([blockBox_height, blockBox_width], np.uint8)
    for i in range(0, blockBox_height):
        for j in range(0, blockBox_width):
            if blockBox[i, j, 3] > 0:
                blockMask[i, j] = 255
    blockBox = cv.cvtColor(blockBox, cv.COLOR_RGBA2GRAY)
    ##由于边界点存在光照影响,为了避免边界点对匹配的影响,进行腐蚀操作
    kernel = np.ones((3, 3), np.uint8)
    blockMask = cv.erode(blockMask, kernel, iterations=1).astype(np.float32)
    backgroundROI = gray_backimg[top:bottom, :]
    ##将backgroundROI、blockBox都转化成float型
    blockBox = (blockBox * 1.0).astype(np.float32)
    backgroundROI = (backgroundROI * 1.0).astype(np.float32)
    ##使用cv的
    res = cv.matchTemplate(backgroundROI, blockBox, cv.TM_CCORR_NORMED, mask=blockMask)
    loc = cv.minMaxLoc(res)
    print("loc==", loc[3][0])
    locs = (loc[3][0])
    return locs

#移动前获取滑块那部分页面上的图片用selenium截图的形式
driver.find_elements_by_xpath('//*[@class="yidun_bg-img"]')[1].screenshot('0.png')
bg_act = cv.imread('0.png')
bg_act_height, bg_act_width = bg_act.shape[0],bg_act.shape[1]
bg = cv.imread('bg_img.png')
bg_height, bg_width = bg.shape[0],bg.shape[1]
block = cv.imread('img.png', -1)
scale = bg_act_height * 1.0 / bg_height
scale1 = bg_act_width * 1.0 / bg_width
block_act = cv.resize(block, (0,0), fx = scale, fy=scale)

print('scale: ', scale, scale1)
x1,x2 =get_image_deviation(bg, block)
x1 = int(x1*scale)
print("x1x2=", x1, x2)


#部分代码
ActionChains(滑块元素).move_by_offset(xoffset= 移动上面生成的距离, yoffset=0).perform()

#第一次移动后二次识别部分代码

driver.find_elements_by_xpath('//*[@class="yidun_bg-img"]')[1].screenshot('bg1.png')
bg_act1 = cv.imread('bg1.png')
x3,x4=get_image_deviation(bg_act1, block_act)


print("x3x4=", x3, x4)
time.sleep(5)
ActionChains(driver).move_by_offset(xoffset= x1-x3, yoffset=0).perform()

posted @ 2020-05-28 21:12  小小咸鱼YwY  阅读(1318)  评论(0编辑  收藏  举报