carlosriverat commited on
Commit
0b7c7f6
·
verified ·
1 Parent(s): ae2249b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -23
app.py CHANGED
@@ -10,12 +10,45 @@ def preprocess_image(image, blur_value):
10
  blurred = cv2.GaussianBlur(gray, (blur_value, blur_value), 0)
11
  return blurred
12
 
13
- def compare_images(image1, image2, blur_value, technique, threshold_value):
14
- # Preprocess images
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  gray1 = preprocess_image(image1, blur_value)
16
  gray2 = preprocess_image(image2, blur_value)
17
-
18
- # Compute SSIM between the two images
19
  score, diff = ssim(gray1, gray2, full=True)
20
  diff = (diff * 255).astype("uint8")
21
 
@@ -23,30 +56,20 @@ def compare_images(image1, image2, blur_value, technique, threshold_value):
23
  _, thresh = cv2.threshold(diff, 30, 255, cv2.THRESH_BINARY_INV)
24
  elif technique == "Otsu's Threshold":
25
  _, thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
26
- else: # Simple Binary
27
  _, thresh = cv2.threshold(diff, threshold_value, 255, cv2.THRESH_BINARY)
28
 
29
- # Find contours of differences
30
  contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
31
-
32
- # Filter out small noise using contour area threshold
33
  filtered_contours = [cnt for cnt in contours if cv2.contourArea(cnt) > 500]
34
-
35
- # Create a mask to isolate only the significant added object
36
  mask = np.zeros_like(image1, dtype=np.uint8)
37
  cv2.drawContours(mask, filtered_contours, -1, (255, 255, 255), thickness=cv2.FILLED)
38
-
39
- # Apply the mask to highlight the object added in the second image
40
  highlighted = cv2.bitwise_and(image2, mask)
41
 
42
- # Create a magenta overlay where changes occurred
43
  diff_colored = np.zeros_like(image1, dtype=np.uint8)
44
- diff_colored[:, :, 0] = thresh # Set blue channel to zero
45
- diff_colored[:, :, 1] = 0 # Set green channel to zero
46
- diff_colored[:, :, 2] = thresh # Set red channel to highlight in magenta
47
-
48
- # Combine the original image with the magenta overlay
49
- overlayed = cv2.addWeighted(image1, 0.7, diff_colored, 0.3, 0)
50
 
51
  return highlighted, overlayed
52
 
@@ -63,13 +86,15 @@ with gr.Blocks() as demo:
63
  blur_slider = gr.Slider(minimum=1, maximum=15, step=2, value=5, label="Gaussian Blur")
64
  technique_dropdown = gr.Dropdown(["Adaptive Threshold", "Otsu's Threshold", "Simple Binary"], label="Thresholding Technique", value="Adaptive Threshold", interactive=True)
65
  threshold_slider = gr.Slider(minimum=0, maximum=255, step=1, value=50, label="Threshold Value", visible=False)
 
66
 
67
  technique_dropdown.change(update_threshold_visibility, inputs=[technique_dropdown], outputs=[threshold_slider])
68
 
69
- output1 = gr.Image(type="numpy", label="Highlighted Differences")
70
- output2 = gr.Image(type="numpy", label="Raw Difference Overlay (Magenta)")
 
71
 
72
  btn = gr.Button("Process")
73
- btn.click(compare_images, inputs=[img1, img2, blur_slider, technique_dropdown, threshold_slider], outputs=[output1, output2])
74
 
75
- demo.launch()
 
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)
16
+ fgmask2 = subtractor.apply(image2)
17
+ diff = cv2.absdiff(fgmask1, fgmask2)
18
+ return cv2.cvtColor(diff, cv2.COLOR_GRAY2BGR)
19
+
20
+ def optical_flow(image1, image2):
21
+ gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
22
+ gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
23
+ flow = cv2.calcOpticalFlowFarneback(gray1, gray2, None, 0.5, 3, 15, 3, 5, 1.2, 0)
24
+ mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
25
+ hsv = np.zeros_like(image1)
26
+ hsv[..., 1] = 255
27
+ hsv[..., 0] = ang * 180 / np.pi / 2
28
+ hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
29
+ return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
30
+
31
+ def feature_matching(image1, image2):
32
+ orb = cv2.ORB_create()
33
+ kp1, des1 = orb.detectAndCompute(image1, None)
34
+ kp2, des2 = orb.detectAndCompute(image2, None)
35
+ bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
36
+ matches = bf.match(des1, des2)
37
+ matches = sorted(matches, key=lambda x: x.distance)
38
+ result = cv2.drawMatches(image1, kp1, image2, kp2, matches[:20], None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
39
+ return result
40
+
41
+ def compare_images(image1, image2, blur_value, technique, threshold_value, method):
42
+ if method == "Background Subtraction":
43
+ return background_subtraction(image1, image2), None
44
+ elif method == "Optical Flow":
45
+ return optical_flow(image1, image2), None
46
+ elif method == "Feature Matching":
47
+ return feature_matching(image1, image2), None
48
+
49
+ # Default SSIM comparison
50
  gray1 = preprocess_image(image1, blur_value)
51
  gray2 = preprocess_image(image2, blur_value)
 
 
52
  score, diff = ssim(gray1, gray2, full=True)
53
  diff = (diff * 255).astype("uint8")
54
 
 
56
  _, thresh = cv2.threshold(diff, 30, 255, cv2.THRESH_BINARY_INV)
57
  elif technique == "Otsu's Threshold":
58
  _, thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
59
+ else:
60
  _, thresh = cv2.threshold(diff, threshold_value, 255, cv2.THRESH_BINARY)
61
 
 
62
  contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
 
 
63
  filtered_contours = [cnt for cnt in contours if cv2.contourArea(cnt) > 500]
 
 
64
  mask = np.zeros_like(image1, dtype=np.uint8)
65
  cv2.drawContours(mask, filtered_contours, -1, (255, 255, 255), thickness=cv2.FILLED)
 
 
66
  highlighted = cv2.bitwise_and(image2, mask)
67
 
 
68
  diff_colored = np.zeros_like(image1, dtype=np.uint8)
69
+ diff_colored[:, :, 0] = 0
70
+ diff_colored[:, :, 1] = 0
71
+ diff_colored[:, :, 2] = thresh
72
+ overlayed = cv2.addWeighted(image1, 0.7, diff_colored, 0.6, 0)
 
 
73
 
74
  return highlighted, overlayed
75
 
 
86
  blur_slider = gr.Slider(minimum=1, maximum=15, step=2, value=5, label="Gaussian Blur")
87
  technique_dropdown = gr.Dropdown(["Adaptive Threshold", "Otsu's Threshold", "Simple Binary"], label="Thresholding Technique", value="Adaptive Threshold", interactive=True)
88
  threshold_slider = gr.Slider(minimum=0, maximum=255, step=1, value=50, label="Threshold Value", visible=False)
89
+ method_dropdown = gr.Dropdown(["SSIM", "Background Subtraction", "Optical Flow", "Feature Matching"], label="Comparison Method", value="SSIM", interactive=True)
90
 
91
  technique_dropdown.change(update_threshold_visibility, inputs=[technique_dropdown], outputs=[threshold_slider])
92
 
93
+ with gr.Row():
94
+ output1 = gr.Image(type="numpy", label="Highlighted Differences")
95
+ output2 = gr.Image(type="numpy", label="Raw Difference Overlay (Magenta)")
96
 
97
  btn = gr.Button("Process")
98
+ btn.click(compare_images, inputs=[img1, img2, blur_slider, technique_dropdown, threshold_slider, method_dropdown], outputs=[output1, output2])
99
 
100
+ demo.launch()