VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > 简明python教程 >
  • 爬虫滑块计算图片之间的距离更加精确

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

作者:小小咸鱼YwY

出处:https://www.cnblogs.com/pythonywy/p/12983952.html



相关教程