Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,24 +3,34 @@ import cv2
|
|
3 |
import numpy as np
|
4 |
from skimage.metrics import structural_similarity as ssim
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
def compare_images(image1, image2):
|
7 |
-
#
|
8 |
-
gray1 =
|
9 |
-
gray2 =
|
10 |
|
11 |
# Compute SSIM between the two images
|
12 |
score, diff = ssim(gray1, gray2, full=True)
|
13 |
diff = (diff * 255).astype("uint8")
|
14 |
|
15 |
-
#
|
16 |
-
_, thresh = cv2.threshold(diff,
|
17 |
|
18 |
# Find contours of differences
|
19 |
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
20 |
|
|
|
|
|
|
|
21 |
# Create a mask to isolate only the significant added object
|
22 |
mask = np.zeros_like(image1)
|
23 |
-
cv2.drawContours(mask,
|
24 |
|
25 |
# Apply the mask to highlight the object added in the second image
|
26 |
highlighted = cv2.bitwise_and(image2, mask)
|
|
|
3 |
import numpy as np
|
4 |
from skimage.metrics import structural_similarity as ssim
|
5 |
|
6 |
+
def preprocess_image(image):
|
7 |
+
# Convert to grayscale
|
8 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
9 |
+
# Apply Gaussian blur to reduce noise
|
10 |
+
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
|
11 |
+
return blurred
|
12 |
+
|
13 |
def compare_images(image1, image2):
|
14 |
+
# Preprocess images
|
15 |
+
gray1 = preprocess_image(image1)
|
16 |
+
gray2 = preprocess_image(image2)
|
17 |
|
18 |
# Compute SSIM between the two images
|
19 |
score, diff = ssim(gray1, gray2, full=True)
|
20 |
diff = (diff * 255).astype("uint8")
|
21 |
|
22 |
+
# Apply adaptive thresholding for better object isolation
|
23 |
+
_, thresh = cv2.threshold(diff, 30, 255, cv2.THRESH_BINARY_INV)
|
24 |
|
25 |
# Find contours of differences
|
26 |
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
27 |
|
28 |
+
# Filter out small noise using contour area threshold
|
29 |
+
filtered_contours = [cnt for cnt in contours if cv2.contourArea(cnt) > 500]
|
30 |
+
|
31 |
# Create a mask to isolate only the significant added object
|
32 |
mask = np.zeros_like(image1)
|
33 |
+
cv2.drawContours(mask, filtered_contours, -1, (255, 255, 255), thickness=cv2.FILLED)
|
34 |
|
35 |
# Apply the mask to highlight the object added in the second image
|
36 |
highlighted = cv2.bitwise_and(image2, mask)
|