Найдите различия между двумя изображениямиPython

Программы на Python
Anonymous
Найдите различия между двумя изображениями

Сообщение Anonymous »


Помогите мне найти разницу между этими двумя изображениямиL
изображением 1 и 2:
Изображение
,
Изображение

Мне нужно сравнить изображения ниже, а затем нарисовать рамки там, где есть различия.
Я использовал следующий код для сравнения, но, похоже, он не сработал. .
Не удалось точно обнаружить различия в изображении.
Вывод кода ниже:
Изображение
).
Мне нужно выходное изображение.
вот так:
Изображение

Я впервые задаю вопрос, если есть ошибки, надеюсь, вы поймете.

Код: Выделить всё

import cv2
import imutils
import numpy as np

# Load the two images
img1 = cv2.imread('images1.jpg')
img2 = cv2.imread("images2.jpg")
# Resize images if necessary
img1 = cv2.resize(img1, (600,360))
img2 = cv2.resize(img2, (600,360))

img_height = img1.shape[0]

# Grayscale
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

# Find the difference between the two images
# Calculate absolute difference between two arrays
diff = cv2.absdiff(gray1, gray2)
cv2.imshow("diff(img1, img2)", diff)

# Apply threshold. Apply both THRESH_BINARY and THRESH_OTSU
thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
cv2.imshow("Threshold", thresh)

# Dilation
kernel = np.ones((5,5), np.uint8)
dilate = cv2.dilate(thresh, kernel, iterations=2)
cv2.imshow("Dilate", dilate)

# Calculate contours
contours = cv2.findContours(dilate.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)

for contour in contours:
if cv2.contourArea(contour) > 100:
# Calculate bounding box around contour
x, y, w, h = cv2.boundingRect(contour)
# Draw rectangle - bounding box on both images
cv2.rectangle(img1, (x, y), (x+w, y+h), (0,0,255), 2)
cv2.rectangle(img2, (x, y), (x+w, y+h), (0,0,255), 2)

# Show images with rectangles on differences
x = np.zeros((img_height,10,3), np.uint8)
result = np.hstack((img1, x, img2))
cv2.imshow("Differences", result)

cv2.waitKey(0)
cv2.destroyAllWindows()


Источник: https://stackoverflow.com/questions/781 ... two-images

Вернуться в «Python»