将重叠图像移动到另一个图像上以获得精确的差异

2024-09-27 09:33:34 发布

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

我想得到用相机拍摄的照片的图像差异。在

我尝试了许多使用python库的解决方案:opencv、image magic等

我发现图像比较的解决方案是为了提高精度:

  1. 移动图像:从左到右并寻找最小差异。在
  2. 将图像:从右向左移动并寻找最小差异。在
  3. 移动图像:从上到下并寻找最小差异。在
  4. 移动图像:从下到上并寻找最小差异。在

拍摄图像的条件: 1摄像头永远不会移动(安装在固定支架上)。 2对象被手动放置在白纸上,因此对象永远不会正确对齐。(每次角度略有变化,因为是手动操作)

以下代码使用摄像头拍摄的图像示例:

图像示例1:白点:

Image 1  as original image

图像样本2:作为原始图像

Image 2 with white dotes

图像样本3:黑点

enter image description here

不提供可接受的白点打印输出,但应仅标记差异(缺陷):

Accepted output

目前我正在使用以下图像魔术命令来处理图像差异:

compare -highlight-color black -fuzz 5% -metric AE Image_1.png Image_2.png -compose src diff.png

代码:

差异后的输出不正确,因为比较是逐像素进行的,它不够智能,不能只标记真正的差异:

output

我提到的上述解决方案将工作以获得所需的差异作为输出,但没有库或图像魔术命令可用于此类图像比较。在

有没有任何python代码或Image magic命令可以执行此操作?


Tags: 对象代码标记图像image命令示例png
2条回答

虽然您不希望逐点处理,但下面是一个使用Imagemagick的子图像搜索比较。它在剪掉黑色部分后填充一张图像,然后将较小的图像移动以找到与较大图像最匹配的位置。在

裁剪图像1:

convert image1.jpg -gravity north -chop 0x25 image1c.png


enter image description here

裁剪和填充图像2:

^{pr2}$


enter image description here

执行子图像搜索

compare -metric rmse -subimage-search image2c.png image1c.png null:
1243.41 (0.0189732) @ 22,20

现在移动并得到两个图像之间的差异

convert image2c.png image1c.png -geometry +22+20 -compose difference -composite -shave 22x20 -colorspace gray -auto-level +level-colors white,red diff.png


enter image description here

附加:

如果只想使用compare,那么需要在compare命令中添加-fuzz 15%:

compare -metric rmse -fuzz 15% -subimage-search image2c.png image1c.png diff.png


生成两个图像。差异图像是第一个,所以请看diff-0.png

enter image description here

看起来你在做一些缺陷检测任务。我想到的第一个解决方案是图像配准技术。 首先尝试在相同的条件下拍摄图像(光线、相机角度和…)(您提供的图像之一大于2像素)。在

然后您应该注册两个图像,并将其中一个与另一个匹配,如下图所示

enter image description here

然后利用单应矩阵对其进行包裹,生成一个对齐的图像,在这种情况下,结果如下:

enter image description here

然后将对齐图像与查询图像的差值取阈值,得到:

enter image description here

正如我所说,如果你尝试更精确地拍摄你的帧,注册结果会更好,并导致更准确的性能。在

每个部分的代码:(主要取自here)。在

import cv2
import numpy as np


MAX_FEATURES = 1000
GOOD_MATCH_PERCENT = 0.5


def alignImages(im1, im2):
    # Convert images to grayscale
    im1Gray = cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY)
    im2Gray = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY)

    # Detect ORB features and compute descriptors.
    orb = cv2.ORB_create(MAX_FEATURES)
    keypoints1, descriptors1 = orb.detectAndCompute(im1Gray, None)
    keypoints2, descriptors2 = orb.detectAndCompute(im2Gray, None)

    # Match features.
    matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING)
    matches = matcher.match(descriptors1, descriptors2, None)

    # Sort matches by score
    matches.sort(key=lambda x: x.distance, reverse=False)

    # Remove not so good matches
    numGoodMatches = int(len(matches) * GOOD_MATCH_PERCENT)
    matches = matches[:numGoodMatches]

    # Draw top matches
    imMatches = cv2.drawMatches(im1, keypoints1, im2, keypoints2, matches, None)
    cv2.imwrite("matches.jpg", imMatches)

    # Extract location of good matches
    points1 = np.zeros((len(matches), 2), dtype=np.float32)
    points2 = np.zeros((len(matches), 2), dtype=np.float32)

    for i, match in enumerate(matches):
        points1[i, :] = keypoints1[match.queryIdx].pt
        points2[i, :] = keypoints2[match.trainIdx].pt

    # Find homography
    h, mask = cv2.findHomography(points1, points2, cv2.RANSAC)

    # Use homography
    height, width, channels = im2.shape
    im1Reg = cv2.warpPerspective(im1, h, (width, height))

    return im1Reg 
if __name__ == '__main__':

  # Read reference image
  refFilename = "vv9gFl.jpg" 
  imFilename =  "uP3CYl.jpg" 
  imReference = cv2.imread(refFilename, cv2.IMREAD_COLOR) 
  im = cv2.imread(imFilename, cv2.IMREAD_COLOR) 

  # Registered image will be resotred in imReg. 
  # The estimated homography will be stored in h. 
  imReg = alignImages(im, imReference)

  # Write aligned image to disk. 
  outFilename = "aligned.jpg" 
  cv2.imwrite(outFilename, imReg) 

对于图像差异和阈值: alined=cv2.imread(“对齐.jpg“,0) alined=alined[:,:280]

^{pr2}$

如果你有大量的图像,并想做缺陷检测任务,我建议使用去噪的自动编码器来训练一个深层的人工神经网络。阅读更多here。在

相关问题 更多 >

    热门问题