检查给定图像是否是另一个更大图像的子集

问题描述 投票:0回答:1

我有一些图片。其中一些图像是裁剪版本。 就像这里是原始图像大图 和裁剪后的图像小图像

请注意,图像的形状(分辨率)不相同。

我有几双这样的。原始图像保存在一个文件夹中,裁剪后的图像保存在另一个文件夹中。

最终我想从这些图像中找到原始图像和裁剪图像对。

所以我想迭代两个文件夹中的图像,并检查裁剪后的图像是否是更大图像的一部分。

但是我找不到任何算法可以用不同形状(分辨率)的图像给出这样的结果。

我已经尝试过

cv2.matchTemplate
skimage.metrics.structural_similarity
但它们仅适用于形状(分辨率)相似的图像。

python opencv machine-learning image-processing computer-vision
1个回答
0
投票

使用 SIFT 查找两个图像之间的相似性。检索匹配良好的点并使用这些点来识别原始图像中的剪切区域

import cv2
import numpy as np


image1 = cv2.imread('img1.jpg')
image2 = cv2.imread('img2.jpg')

sift = cv2.SIFT_create()
kp1, des1 = sift.detectAndCompute(image1, None)
kp2, des2 = sift.detectAndCompute(image2, None)
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)
good_matches = [m for m, n in matches if m.distance < 0.75 * n.distance]

if len(good_matches) > 50:

    src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1, 1, 2)
    dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1, 1, 2)
    
    M, _ = cv2.findHomography(dst_pts, src_pts, cv2.RANSAC, 5.0)
    
    h, w = image2.shape[:2]
    pts = np.float32([[0, 0], [0, h-1], [w-1, h-1], [w-1, 0]]).reshape(-1, 1, 2)
    
    dst = cv2.perspectiveTransform(pts, M)
    image1 = cv2.polylines(image1, [np.int32(dst)], True, (0, 255, 0), 3)
    
    cv2.imshow('Result', cv2.resize(image1, (0, 0), fx=0.5, fy=0.5))
    cv2.waitKey(0)
    cv2.destroyAllWindows()
© www.soinside.com 2019 - 2024. All rights reserved.