-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource code.
61 lines (44 loc) · 1.79 KB
/
Source code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import cv2
import tkinter as tk
from tkinter import filedialog
import numpy as np
# Create a tkinter root window
root = tk.Tk()
root.withdraw()
# Open a file dialog to select the first image
image1_path = filedialog.askopenfilename()
# Open a file dialog to select the second image
image2_path = filedialog.askopenfilename()
# Load the two images
image1 = cv2.imread(image1_path)
image2 = cv2.imread(image2_path)
# Convert the images to grayscale
gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
# Blur the images to reduce noise
gray1 = cv2.GaussianBlur(gray1, (5, 5), 0)
gray2 = cv2.GaussianBlur(gray2, (5, 5), 0)
# Get the shape of the first image
height1, width1 = gray1.shape[:2]
# Get the shape of the second image
height2, width2 = gray2.shape[:2]
# If the shapes are not the same, resize the second image to match the first image
if (height1, width1) != (height2, width2):
gray2 = cv2.resize(gray2, (width1, height1))
# Compute the absolute difference between the two images
difference = cv2.absdiff(gray1, gray2)
# Use a threshold to create a black and white image
threshold = cv2.threshold(difference, 25, 255, cv2.THRESH_BINARY)[1]
# Use dilate to "grow" the white pixels
dilated = cv2.dilate(threshold, None, iterations=3)
# Find the contours of the white regions
contours, _ = cv2.findContours(dilated, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_TC89_L1)
# Create an empty image that is the same size as the input images
combined = np.zeros((image1.shape[0], image1.shape[1], 3), dtype=np.uint8)
# Iterate through the contours and draw them on the combined image
for c in contours:
cv2.drawContours(combined, [c], -1, (0, 255, 0), 2)
# Display the combined image
cv2.imshow("Combined", combined)
# Wait for a key press to close the window
cv2.waitKey(0)