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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -10
app.py CHANGED
@@ -15,7 +15,8 @@ def background_subtraction(image1, image2):
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)
@@ -26,7 +27,8 @@ def optical_flow(image1, image2):
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()
@@ -36,17 +38,17 @@ def feature_matching(image1, image2):
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)
@@ -70,8 +72,9 @@ def compare_images(image1, image2, blur_value, technique, threshold_value, metho
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
 
76
  def update_threshold_visibility(technique):
77
  return gr.update(visible=(technique == "Simple Binary"))
@@ -83,7 +86,7 @@ with gr.Blocks() as demo:
83
  img1 = gr.Image(type="numpy", label="Image Without Object")
84
  img2 = gr.Image(type="numpy", label="Image With Object")
85
 
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)
@@ -94,7 +97,10 @@ with gr.Blocks() as demo:
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()
 
15
  fgmask1 = subtractor.apply(image1)
16
  fgmask2 = subtractor.apply(image2)
17
  diff = cv2.absdiff(fgmask1, fgmask2)
18
+ unchanged = cv2.bitwise_and(image1, image1, mask=255 - diff)
19
+ return cv2.cvtColor(diff, cv2.COLOR_GRAY2BGR), unchanged
20
 
21
  def optical_flow(image1, image2):
22
  gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
 
27
  hsv[..., 1] = 255
28
  hsv[..., 0] = ang * 180 / np.pi / 2
29
  hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
30
+ unchanged = cv2.bitwise_and(image1, image1, mask=255 - hsv[..., 2].astype(np.uint8))
31
+ return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR), unchanged
32
 
33
  def feature_matching(image1, image2):
34
  orb = cv2.ORB_create()
 
38
  matches = bf.match(des1, des2)
39
  matches = sorted(matches, key=lambda x: x.distance)
40
  result = cv2.drawMatches(image1, kp1, image2, kp2, matches[:20], None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
41
+ unchanged = cv2.addWeighted(image1, 0.7, image2, 0.3, 0)
42
+ return result, unchanged
43
 
44
  def compare_images(image1, image2, blur_value, technique, threshold_value, method):
45
  if method == "Background Subtraction":
46
+ return background_subtraction(image1, image2)
47
  elif method == "Optical Flow":
48
+ return optical_flow(image1, image2)
49
  elif method == "Feature Matching":
50
+ return feature_matching(image1, image2)
51
 
 
52
  gray1 = preprocess_image(image1, blur_value)
53
  gray2 = preprocess_image(image2, blur_value)
54
  score, diff = ssim(gray1, gray2, full=True)
 
72
  diff_colored[:, :, 1] = 0
73
  diff_colored[:, :, 2] = thresh
74
  overlayed = cv2.addWeighted(image1, 0.7, diff_colored, 0.6, 0)
75
+ unchanged = cv2.bitwise_and(image1, image1, mask=255 - thresh)
76
 
77
+ return highlighted, overlayed, unchanged
78
 
79
  def update_threshold_visibility(technique):
80
  return gr.update(visible=(technique == "Simple Binary"))
 
86
  img1 = gr.Image(type="numpy", label="Image Without Object")
87
  img2 = gr.Image(type="numpy", label="Image With Object")
88
 
89
+ blur_slider = gr.Slider(minimum=1, maximum=15, step=1, value=5, label="Gaussian Blur")
90
  technique_dropdown = gr.Dropdown(["Adaptive Threshold", "Otsu's Threshold", "Simple Binary"], label="Thresholding Technique", value="Adaptive Threshold", interactive=True)
91
  threshold_slider = gr.Slider(minimum=0, maximum=255, step=1, value=50, label="Threshold Value", visible=False)
92
  method_dropdown = gr.Dropdown(["SSIM", "Background Subtraction", "Optical Flow", "Feature Matching"], label="Comparison Method", value="SSIM", interactive=True)
 
97
  output1 = gr.Image(type="numpy", label="Highlighted Differences")
98
  output2 = gr.Image(type="numpy", label="Raw Difference Overlay (Magenta)")
99
 
100
+ with gr.Row():
101
+ output3 = gr.Image(type="numpy", label="Unchanged Areas Blended")
102
+
103
  btn = gr.Button("Process")
104
+ btn.click(compare_images, inputs=[img1, img2, blur_slider, technique_dropdown, threshold_slider, method_dropdown], outputs=[output1, output2, output3])
105
 
106
  demo.launch()