Commit 26502eb7 authored by Felix Hamann's avatar Felix Hamann

first rudimentary detection works!

parent 687a6274
......@@ -12,7 +12,7 @@ import skimage.transform as skt
from . import tmeasure
from . import logger
log = logger(__name__)
log.propagate = False
# ---
......@@ -222,7 +222,6 @@ class Erode(Morph):
return tgt
# Currently unused...
class Fill(Module):
def __init__(self, name: str):
......@@ -230,6 +229,9 @@ class Fill(Module):
def execute(self) -> np.ndarray:
src = self.pipeline[-1].arr
return self.apply(src)
def apply(self, src: np.ndarray) -> np.ndarray:
labeled, labels = scnd.label(np.invert(src.astype(np.bool)))
max_count = 0
......@@ -253,6 +255,9 @@ class Edger(Module):
def execute(self) -> np.ndarray:
src = self.pipeline[-1].arr
return self.apply(src)
def apply(self, src: np.ndarray) -> np.ndarray:
tgt = np.zeros(src.shape)
tgt[scnd.binary_dilation(src)] = 255
return (src.astype(np.bool) ^ tgt.astype(np.bool)) * 255
......@@ -264,6 +269,14 @@ class Hough(Module):
def src(self) -> np.ndarray:
return self._src
@property
def binarized(self) -> np.ndarray:
return self._binarized
@binarized.setter
def binarized(self, binarized: np.ndarray):
self._binarized = binarized
# ---
@property
......@@ -363,7 +376,6 @@ class Hough(Module):
(int -> int -> tuple(3))
"""
binarized = self.pipeline['pre_dilate'].arr
off = self.red_detection // 2
for i, j, coords in self._iter_intersections(h_coords):
......@@ -371,7 +383,7 @@ class Hough(Module):
# abort if there's no red to be found on
# the point of intersection
if not np.any(binarized[y-off:y+off, x-off:x+off]):
if not np.any(self.binarized[y-off:y+off, x-off:x+off]):
fmt = 'discarding %s because it lacks red'
log.debug(fmt, str(coords))
continue
......@@ -467,7 +479,7 @@ class Hough(Module):
x0, x1 = np.min(xcoords), np.max(xcoords)
h, w = y1 - y0, x1 - x0
img_h, img_w = self._src.shape
img_h, img_w = self.src.shape
# abort if the sign size does not fit
if img_h/2 < h or h < 10 or img_w/2 < w or w < 10:
......@@ -528,10 +540,14 @@ class Hough(Module):
self.patmatch_threshold = 0.2
def execute(self) -> np.ndarray:
self._src = self.pipeline[-1].arr
tgt = self.pipeline[0].arr
src = self.pipeline[-1].arr
self.binarized = self.pipeline['pre_dilate'].arr
self.apply(src)
return self.pipeline[0].arr
h, w = self._src.shape
def apply(self, src: np.ndarray) -> np.ndarray:
self._src = src
h, w = self.src.shape
# reset
self._pois = defaultdict(dict) # points of intersections
......@@ -556,7 +572,7 @@ class Hough(Module):
done(len(angles))
if len(angles) == 0:
return tgt
return
self._angles = angles
self._dists = dists
......@@ -607,5 +623,3 @@ class Hough(Module):
# populate self.barycenter by calculating
# and filtering triangles
self._calc_triangles()
return tgt
......@@ -15,6 +15,7 @@ import argparse
import numpy as np
import skvideo.io as skvio
import skimage.draw as skd
from src import pipeline as pl
......@@ -46,6 +47,8 @@ def save(fname: str, vid: np.ndarray):
def start(vid: np.ndarray):
log.info('initializing modules')
# preprocessing
mod_binarize = pl.Binarize('binarize')
mod_binarize.threshold = 90
mod_binarize.reference_color = (150, 20, 20)
......@@ -56,23 +59,59 @@ def start(vid: np.ndarray):
mod_erode = pl.Erode('erode')
mod_erode.iterations = 4
# edging
mod_fill = pl.Fill('fill')
mod_edger = pl.Edger('edger')
# detecting
mod_hough = pl.Hough('hough')
# start
log.info('start processing')
n, h, w, _ = vid.shape
binary = np.zeros((n, h, w))
done = tmeasure(log.info, 'took %sms')
for frame in range(n):
binary[frame] = mod_binarize.apply(vid[frame].astype(np.int64))
binary[frame] = mod_dilate.apply(binary[frame])
binary[frame] = mod_erode.apply(binary[frame])
binary[frame] = mod_fill.apply(binary[frame])
binary[frame] = mod_edger.apply(binary[frame])
vid[frame] = vid[frame] / 3
mod_hough.binarized = binary[frame]
mod_hough.apply(binary[frame])
for (p0, p1, p2), (y, x) in mod_hough.barycenter.items():
pois = (p0, p1), (p0, p2), (p1, p2)
py, px = zip(*[mod_hough.pois[a][b] for a, b in pois])
rr, cc = skd.polygon_perimeter(
py + (py[0], ),
px + (px[0], ),
shape=vid[frame].shape)
vid[frame, rr, cc] = [255, 255, 255]
r = (np.max(py) - np.min(py)) * 2
rr, cc = skd.circle_perimeter(
y, x, r, shape=vid[frame].shape)
vid[frame, rr, cc, 0] = 255
sys.stdout.write('.')
sys.stdout.flush()
print('')
done()
return binary
return vid
#
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment