From 09d53e146136d57c0b737a666a7930f8991a9c32 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Al=C3=A1n=20Mu=C3=B1oz?= <amuoz@ed.ac.uk>
Date: Thu, 3 Mar 2022 15:57:44 +0000
Subject: [PATCH] add additional trap identification method

---
 aliby/tile/traps.py | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/aliby/tile/traps.py b/aliby/tile/traps.py
index 08b54847..5d3e25fe 100644
--- a/aliby/tile/traps.py
+++ b/aliby/tile/traps.py
@@ -91,10 +91,28 @@ def segment_traps(image, tile_size, downscale=0.4):
         x - tile_size // 2 : x + tile_size // 2, y - tile_size // 2 : y + tile_size // 2
     ]
 
+    # add template as mean of found traps
+    mean_template = (
+        np.dstack(
+            [
+                image[
+                    x - tile_size // 2 : x + tile_size // 2,
+                    y - tile_size // 2 : y + tile_size // 2,
+                ]
+                for x, y in traps
+            ]
+        )
+        .astype(int)
+        .mean(axis=-1)
+    )
+
     traps = identify_trap_locations(image, template)
+    mean_traps = identify_trap_locations(image, mean_template)
+
+    traps = traps if len(traps) > len(mean_traps) else mean_traps
 
-    if len(traps) < 10 and downscale != 1:
-        print("Trying again.")
+    if len(traps) < 30 and downscale != 1:
+        print("Tiler:TrapIdentification: Trying again.")
         return segment_traps(image, tile_size, downscale=1)
 
     return traps
-- 
GitLab