如何比较两幅图像的差异并返回点的坐标

2024-04-24 20:47:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我想找到CAPTCHA图片滑块的默认位置,并获得默认位置的坐标,我已经处理了这两幅图像。 之后的图像 https://i.loli.net/2019/07/31/5d412d6c7d95e29689.pnghttps://i.loli.net/2019/07/31/5d412d6c7e25770645.png

Python3 PIL lib

im1 = Image.open("frame.png")
im2 = Image.open("frame1.png")
diff = ImageChops.difference(im2, im1).getbbox()

(29,65,289,151)不是结果。你知道吗


Tags: https图像imagenetpilpnglib图片
1条回答
网友
1楼 · 发布于 2024-04-24 20:47:52

试试这个python代码。图像位置将写入imagePositions.txt文件地址:

#!/usr/bin/env python
import cv2 
import imutils
import numpy as np  

import argparse
import os, shutil

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-b", " beforeimage", required=True,
    help="path to first image")
ap.add_argument("-i", " image", required=True,
    help="path to 2nd image")
args = vars(ap.parse_args())
# load the example image and convert it to grayscale
image = cv2.imread(args["image"])
originalImage = image #save copy to use later
beforeimage = cv2.imread(args["beforeimage"])

image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
beforeimage = cv2.cvtColor(beforeimage, cv2.COLOR_BGR2GRAY)

res = cv2.bitwise_xor(image,beforeimage, mask= beforeimage) 

if os.path.exists("imageDiff.png"):
    os.remove("imageDiff.png")

if os.path.exists("imagePositions.txt"):
    os.remove('imagePositions.txt')

cv2.imwrite('imageDiff.png', res)

image = res

image = (255-image) #invert black and white

# threshold the image (convert it to pure black and white)
thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

# find the contours (continuous blobs of pixels) the image
contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Hack for compatibility with different OpenCV versions
contours = contours[0] if imutils.is_cv2() else contours[1]

with open('imagePositions.txt', 'w+') as out:
    # Now we can loop through each of the four contours and extract the letter
    # inside of each one
    cntr = 1
    for contour in contours:
        # Get the rectangle that contains the contour
        (x, y, w, h) = cv2.boundingRect(contour)
        #check for needed size here
        if (w > 3 and h > 3 ):
            cv2.rectangle(image, (x,y), (x + w, y + h), (0, 0, 255), 2)
            #add some surrounding area we require
            y1 = y -20
            if y1 < 0:
                y1 = 0
            h1 = h + 40
            x1 = x - 30
            if x1 < 0:
                x1 = 0
            w1 = w + 60     
            crop_img = originalImage[y1:y1+h1, x1:x1+w1]
            #We have a folder to store cropped images
            cv2.imwrite('ImageDifferences/' + str(cntr) + '.png', crop_img)
            out.write(str(cntr) + '.png : ' + str(x1+w1/2) + ',' + str(y1+h1/2) + '\n')
            cntr = cntr + 1

相关问题 更多 >