当前位置:   article > 正文

使用opencv(cv2)来计算两张有位移的图片间的区别_opencv两张图变化检测

opencv两张图变化检测

背景:两张图片间有微小移位需要先对齐再计算,若无偏移可直接使用 cv2.subtract() 计算差异。

main.py目录创建一个img文件夹存放图片。

 直接上脚本:

  1. import cv2
  2. import numpy as np
  3. def align_images(img1, img2, max_features=5000, good_match_percent=0.15):
  4. # Convert images to grayscale
  5. img1_gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
  6. img2_gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
  7. # Detect ORB features and compute descriptors
  8. orb = cv2.ORB_create(max_features)
  9. keypoints1, descriptors1 = orb.detectAndCompute(img1_gray, None)
  10. keypoints2, descriptors2 = orb.detectAndCompute(img2_gray, None)
  11. # Match features
  12. matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING)
  13. matches = matcher.match(descriptors1, descriptors2, None)
  14. # Sort matches by score
  15. # matches.sort(key=lambda x: x.distance, reverse=False)
  16. matches = tuple(sorted(list(matches), key=lambda x: x.distance, reverse=False))
  17. # Remove not so good matches
  18. num_good_matches = int(len(matches) * good_match_percent)
  19. matches = matches[:num_good_matches]
  20. # Extract location of good matches
  21. points1 = np.zeros((len(matches), 2), dtype=np.float32)
  22. points2 = np.zeros((len(matches), 2), dtype=np.float32)
  23. for i, match in enumerate(matches):
  24. points1[i, :] = keypoints1[match.queryIdx].pt
  25. points2[i, :] = keypoints2[match.trainIdx].pt
  26. # Find homography
  27. h, mask = cv2.findHomography(points1, points2, cv2.RANSAC)
  28. # Use homography to align images
  29. height, width, channels = img2.shape
  30. img1_aligned = cv2.warpPerspective(img1, h, (width, height))
  31. return img1_aligned, img2
  32. def find_differences(img1, img2, threshold=30):
  33. # Convert images to grayscale
  34. img1_gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
  35. img2_gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
  36. # Compute absolute difference between the two images
  37. diff = cv2.absdiff(img1_gray, img2_gray)
  38. # Threshold the difference image
  39. _, thresh = cv2.threshold(diff, threshold, 255, cv2.THRESH_BINARY)
  40. return thresh
  41. if __name__ == '__main__':
  42. # Load images
  43. img1 = cv2.imread('img/img01.jpg')
  44. img2 = cv2.imread('img/img02.jpg')
  45. # Align the images
  46. img1_aligned, img2 = align_images(img1, img2)
  47. # Find differences between the aligned images
  48. differences = find_differences(img1_aligned, img2)
  49. # Save the result
  50. cv2.imwrite('img/differences.jpg', differences)
  51. # Show the result
  52. cv2.imshow('Differences', differences)
  53. cv2.waitKey(0)
  54. cv2.destroyAllWindows()

这个脚本首先使用ORB特征检测器来检测两张图片的关键点,并计算描述符。然后,匹配特征点并使用RANSAC算法找到一个透视变换矩阵,以便将第一张图片与第二张图片对齐。最后,计算两张图片之间的差异,并将结果保存为一个新的图片。你也可以更改阈值参数以获得更好的差异检测结果。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/287715
推荐阅读
相关标签
  

闽ICP备14008679号