Spaces:
Sleeping
Sleeping
Commit ·
1601fa5
1
Parent(s): 9e2fa2e
updated magenta images
Browse files
app.py
CHANGED
|
@@ -10,6 +10,24 @@ def preprocess_image(image, blur_value):
|
|
| 10 |
blurred = cv2.GaussianBlur(gray, (blur_value, blur_value), 0)
|
| 11 |
return blurred
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def background_subtraction(image1, image2):
|
| 14 |
subtractor = cv2.createBackgroundSubtractorMOG2()
|
| 15 |
fgmask1 = subtractor.apply(image1)
|
|
@@ -22,11 +40,8 @@ def background_subtraction(image1, image2):
|
|
| 22 |
# Create highlighted differences
|
| 23 |
highlighted = cv2.bitwise_and(image2, image2, mask=mask)
|
| 24 |
|
| 25 |
-
# Create raw difference overlay
|
| 26 |
-
|
| 27 |
-
diff_colored[:, :, 0] = 0 # Remove blue
|
| 28 |
-
diff_colored[:, :, 1] = 0 # Remove green
|
| 29 |
-
overlay = cv2.addWeighted(image1, 0.6, diff_colored, 0.6, 0)
|
| 30 |
|
| 31 |
# Create a blended image
|
| 32 |
blended = cv2.addWeighted(image1, 0.5, image2, 0.5, 0)
|
|
@@ -35,13 +50,10 @@ def background_subtraction(image1, image2):
|
|
| 35 |
composite = image1.copy()
|
| 36 |
composite[mask > 0] = image2[mask > 0]
|
| 37 |
|
| 38 |
-
# Create final difference overlay
|
| 39 |
-
|
| 40 |
-
composite_diff[:, :, 0] = 0 # Remove blue
|
| 41 |
-
composite_diff[:, :, 1] = 0 # Remove green
|
| 42 |
-
final_overlay = cv2.addWeighted(image1, 0.6, composite_diff, 0.6, 0)
|
| 43 |
|
| 44 |
-
return blended,
|
| 45 |
|
| 46 |
def optical_flow(image1, image2):
|
| 47 |
gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
|
|
@@ -59,11 +71,8 @@ def optical_flow(image1, image2):
|
|
| 59 |
# Create highlighted differences
|
| 60 |
highlighted = cv2.bitwise_and(image2, image2, mask=mask)
|
| 61 |
|
| 62 |
-
# Create raw difference overlay
|
| 63 |
-
|
| 64 |
-
diff_colored[:, :, 0] = 0 # Remove blue
|
| 65 |
-
diff_colored[:, :, 1] = 0 # Remove green
|
| 66 |
-
overlay = cv2.addWeighted(image1, 0.6, diff_colored, 0.6, 0)
|
| 67 |
|
| 68 |
# Create a blended image
|
| 69 |
blended = cv2.addWeighted(image1, 0.5, image2, 0.5, 0)
|
|
@@ -72,13 +81,10 @@ def optical_flow(image1, image2):
|
|
| 72 |
composite = image1.copy()
|
| 73 |
composite[mask > 0] = image2[mask > 0]
|
| 74 |
|
| 75 |
-
# Create final difference overlay
|
| 76 |
-
|
| 77 |
-
composite_diff[:, :, 0] = 0 # Remove blue
|
| 78 |
-
composite_diff[:, :, 1] = 0 # Remove green
|
| 79 |
-
final_overlay = cv2.addWeighted(image1, 0.6, composite_diff, 0.6, 0)
|
| 80 |
|
| 81 |
-
return blended,
|
| 82 |
|
| 83 |
def feature_matching(image1, image2):
|
| 84 |
# Use SSIM as a fallback for feature matching since the original implementation doesn't give us a good mask
|
|
@@ -105,11 +111,8 @@ def compare_ssim(image1, image2, blur_value, technique, threshold_value):
|
|
| 105 |
# Create highlighted differences
|
| 106 |
highlighted = cv2.bitwise_and(image2, image2, mask=mask)
|
| 107 |
|
| 108 |
-
# Create raw difference overlay
|
| 109 |
-
|
| 110 |
-
diff_colored[:, :, 0] = 0 # Remove blue
|
| 111 |
-
diff_colored[:, :, 1] = 0 # Remove green
|
| 112 |
-
overlay = cv2.addWeighted(image1, 0.6, diff_colored, 0.6, 0)
|
| 113 |
|
| 114 |
# Create a blended image
|
| 115 |
blended = cv2.addWeighted(image1, 0.5, image2, 0.5, 0)
|
|
@@ -121,13 +124,10 @@ def compare_ssim(image1, image2, blur_value, technique, threshold_value):
|
|
| 121 |
masked_bg = cv2.bitwise_and(image1, cv2.bitwise_not(mask_3channel))
|
| 122 |
composite = cv2.add(masked_bg, masked_obj)
|
| 123 |
|
| 124 |
-
# Create final difference overlay
|
| 125 |
-
|
| 126 |
-
composite_diff[:, :, 0] = 0 # Remove blue
|
| 127 |
-
composite_diff[:, :, 1] = 0 # Remove green
|
| 128 |
-
final_overlay = cv2.addWeighted(image1, 0.6, composite_diff, 0.6, 0)
|
| 129 |
|
| 130 |
-
return blended,
|
| 131 |
|
| 132 |
def compare_images(image1, image2, blur_value, technique, threshold_value, method):
|
| 133 |
if method == "Background Subtraction":
|
|
@@ -149,7 +149,7 @@ with gr.Blocks() as demo:
|
|
| 149 |
img1 = gr.Image(type="numpy", label="Image Without Object (Scene)")
|
| 150 |
img2 = gr.Image(type="numpy", label="Image With Object")
|
| 151 |
|
| 152 |
-
blur_slider = gr.Slider(minimum=1, maximum=15, step=
|
| 153 |
technique_dropdown = gr.Dropdown(["Adaptive Threshold", "Otsu's Threshold", "Simple Binary"], label="Thresholding Technique", value="Adaptive Threshold", interactive=True)
|
| 154 |
threshold_slider = gr.Slider(minimum=0, maximum=255, step=1, value=50, label="Threshold Value", visible=False)
|
| 155 |
method_dropdown = gr.Dropdown(["SSIM", "Background Subtraction", "Optical Flow", "Feature Matching"], label="Comparison Method", value="SSIM", interactive=True)
|
|
|
|
| 10 |
blurred = cv2.GaussianBlur(gray, (blur_value, blur_value), 0)
|
| 11 |
return blurred
|
| 12 |
|
| 13 |
+
def create_dramatic_magenta(image1, diff):
|
| 14 |
+
"""Create a more dramatic magenta overlay to highlight differences"""
|
| 15 |
+
# Create a more intense magenta by boosting the red and blue channels
|
| 16 |
+
diff_colored = cv2.absdiff(image1, diff)
|
| 17 |
+
|
| 18 |
+
# Normalize to enhance contrast
|
| 19 |
+
diff_normalized = cv2.normalize(diff_colored, None, 0, 255, cv2.NORM_MINMAX)
|
| 20 |
+
|
| 21 |
+
# Amplify the red channel for more dramatic magenta
|
| 22 |
+
diff_normalized[:, :, 0] = 0 # Remove blue
|
| 23 |
+
diff_normalized[:, :, 1] = 0 # Remove green
|
| 24 |
+
diff_normalized[:, :, 2] = np.clip(diff_normalized[:, :, 2] * 2, 0, 255) # Boost red
|
| 25 |
+
|
| 26 |
+
# Create more dramatic overlay with higher contrast
|
| 27 |
+
overlay = cv2.addWeighted(image1, 0.5, diff_normalized, 0.8, 0)
|
| 28 |
+
|
| 29 |
+
return overlay
|
| 30 |
+
|
| 31 |
def background_subtraction(image1, image2):
|
| 32 |
subtractor = cv2.createBackgroundSubtractorMOG2()
|
| 33 |
fgmask1 = subtractor.apply(image1)
|
|
|
|
| 40 |
# Create highlighted differences
|
| 41 |
highlighted = cv2.bitwise_and(image2, image2, mask=mask)
|
| 42 |
|
| 43 |
+
# Create raw difference overlay with dramatic magenta
|
| 44 |
+
raw_overlay = create_dramatic_magenta(image1, image2)
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# Create a blended image
|
| 47 |
blended = cv2.addWeighted(image1, 0.5, image2, 0.5, 0)
|
|
|
|
| 50 |
composite = image1.copy()
|
| 51 |
composite[mask > 0] = image2[mask > 0]
|
| 52 |
|
| 53 |
+
# Create final difference overlay with dramatic magenta
|
| 54 |
+
final_overlay = create_dramatic_magenta(image1, composite)
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
return blended, raw_overlay, highlighted, mask, composite, final_overlay
|
| 57 |
|
| 58 |
def optical_flow(image1, image2):
|
| 59 |
gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
|
|
|
|
| 71 |
# Create highlighted differences
|
| 72 |
highlighted = cv2.bitwise_and(image2, image2, mask=mask)
|
| 73 |
|
| 74 |
+
# Create raw difference overlay with dramatic magenta
|
| 75 |
+
raw_overlay = create_dramatic_magenta(image1, image2)
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
# Create a blended image
|
| 78 |
blended = cv2.addWeighted(image1, 0.5, image2, 0.5, 0)
|
|
|
|
| 81 |
composite = image1.copy()
|
| 82 |
composite[mask > 0] = image2[mask > 0]
|
| 83 |
|
| 84 |
+
# Create final difference overlay with dramatic magenta
|
| 85 |
+
final_overlay = create_dramatic_magenta(image1, composite)
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
return blended, raw_overlay, highlighted, mask, composite, final_overlay
|
| 88 |
|
| 89 |
def feature_matching(image1, image2):
|
| 90 |
# Use SSIM as a fallback for feature matching since the original implementation doesn't give us a good mask
|
|
|
|
| 111 |
# Create highlighted differences
|
| 112 |
highlighted = cv2.bitwise_and(image2, image2, mask=mask)
|
| 113 |
|
| 114 |
+
# Create raw difference overlay with dramatic magenta
|
| 115 |
+
raw_overlay = create_dramatic_magenta(image1, image2)
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
# Create a blended image
|
| 118 |
blended = cv2.addWeighted(image1, 0.5, image2, 0.5, 0)
|
|
|
|
| 124 |
masked_bg = cv2.bitwise_and(image1, cv2.bitwise_not(mask_3channel))
|
| 125 |
composite = cv2.add(masked_bg, masked_obj)
|
| 126 |
|
| 127 |
+
# Create final difference overlay with dramatic magenta
|
| 128 |
+
final_overlay = create_dramatic_magenta(image1, composite)
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
+
return blended, raw_overlay, highlighted, mask, composite, final_overlay
|
| 131 |
|
| 132 |
def compare_images(image1, image2, blur_value, technique, threshold_value, method):
|
| 133 |
if method == "Background Subtraction":
|
|
|
|
| 149 |
img1 = gr.Image(type="numpy", label="Image Without Object (Scene)")
|
| 150 |
img2 = gr.Image(type="numpy", label="Image With Object")
|
| 151 |
|
| 152 |
+
blur_slider = gr.Slider(minimum=1, maximum=15, step=2, value=5, label="Gaussian Blur")
|
| 153 |
technique_dropdown = gr.Dropdown(["Adaptive Threshold", "Otsu's Threshold", "Simple Binary"], label="Thresholding Technique", value="Adaptive Threshold", interactive=True)
|
| 154 |
threshold_slider = gr.Slider(minimum=0, maximum=255, step=1, value=50, label="Threshold Value", visible=False)
|
| 155 |
method_dropdown = gr.Dropdown(["SSIM", "Background Subtraction", "Optical Flow", "Feature Matching"], label="Comparison Method", value="SSIM", interactive=True)
|