Optofidelity: Screen Calibration fixes

When calibrating a screen with PWM, even the non flashing parts
of the screen can look like they are flashing. This cl makes some small
adjustments to account for that.

BUG=None
TEST=covered in regression tests

Change-Id: Ice3848d193dfff71b3529ae54e12e8908e731519
Reviewed-on: https://chromium-review.googlesource.com/336538
Commit-Ready: Dennis Kempin <[email protected]>
Tested-by: Dennis Kempin <[email protected]>
Reviewed-by: Dennis Kempin <[email protected]>
diff --git a/optofidelity/optofidelity/detection/screen_calibration.py b/optofidelity/optofidelity/detection/screen_calibration.py
index bfc5a4c..cf821fb 100644
--- a/optofidelity/optofidelity/detection/screen_calibration.py
+++ b/optofidelity/optofidelity/detection/screen_calibration.py
@@ -12,6 +12,7 @@
 import numpy as np
 import skimage.morphology as morphology
 import skimage.transform as transform
+import skimage.filters as filters
 
 from optofidelity.util import nputil
 from optofidelity.videoproc import DebugView, Filter, Shape
@@ -29,11 +30,11 @@
   """The test area is reduced in size by this margin to account for potential
      camera shake."""
 
-  DIFF_THRESHOLD = 0.2
+  DIFF_THRESHOLD = 0.4
   """Threshold used on the difference between the on and off image to find
      the test area."""
 
-  CLOSING_KERNEL = morphology.disk(2)
+  CLOSING_KERNEL = morphology.disk(8)
   """Size of the disk shaped kernel used for the closing operation to close
      any holes in the detected shapes that might be caused by noise."""
 
@@ -480,6 +481,7 @@
     :returns Shape: shape object of the detected screen
     """
     delta = Filter.Truncate(white_frame - black_frame)
+    delta = filters.gaussian_filter(delta, 5)
     margin_kernel = morphology.disk(self.SHAPE_MARGIN)
     binary = delta > self.DIFF_THRESHOLD
     binary = morphology.binary_closing(binary, self.CLOSING_KERNEL)
diff --git a/optofidelity/optofidelity/videoproc/shape.py b/optofidelity/optofidelity/videoproc/shape.py
index e379caa..1cc0a1b 100644
--- a/optofidelity/optofidelity/videoproc/shape.py
+++ b/optofidelity/optofidelity/videoproc/shape.py
@@ -82,7 +82,7 @@
 
     # First pass of approximation, this will calculate a simplified polygon
     # with an undefined number of verticies.
-    verticies = measure.approximate_polygon(contour, tolerance=10)
+    verticies = measure.approximate_polygon(contour, tolerance=50)
 
     # The last coordinate is often a duplicate of the first.
     if (np.abs(np.linalg.norm(verticies[0, :] - verticies[-1, :]))
diff --git a/optofidelity/regression_tests.py b/optofidelity/regression_tests.py
index e32dbcc..8b60523 100644
--- a/optofidelity/regression_tests.py
+++ b/optofidelity/regression_tests.py
@@ -39,6 +39,8 @@
       Test("20150915_2230_003", {}),
   "led.inconsistent.s6edge":
       Test("20150928_1805_000", {}),
+  "led.poor_calibration.s6edge":
+      Test("20151003_0456_001", {}),
 
   "line.brightness_changed.nexus6":
       Test("20150908_1624_000", {}),