Module brevettiai.tests.test_polygon_extraction
Expand source code
import unittest
import numpy as np
import cv2
from brevettiai.utils.polygon_utils import cv2_contour_to_shapely, simplify_polygon
def plot_mask_and_contours(mask, contours):
import matplotlib.pyplot as plt
plt.imshow(mask)
for cnt in contours:
try:
plt.plot(*cnt.xy, 'b-x')
except NotImplementedError:
try:
plt.plot(*cnt.boundary.xy, 'r-x')
except NotImplementedError:
for b in cnt.boundary:
plt.plot(*b.xy, 'g-x')
plt.xlim(plt.xlim() + np.array([-1, 1]))
plt.ylim(plt.ylim() + np.array([1, -1]))
plt.show()
class TestPolygonExtraction(unittest.TestCase):
def test_polygon_validity(self):
mask = np.array([
[0, 1, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 0, 0, 1],
[1, 0, 0, 1, 0, 0, 1],
[0, 1, 0, 0, 1, 0, 0],
[0, 1, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0, 0],
[0, 0, 0, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 0, 1, 1, 1],
[0, 1, 1, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[1, 1, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[1, 1, 0, 1, 1, 0, 0],
[0, 1, 0, 1, 1, 0, 0],
[0, 0, 0, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 1, 1, 0],
[0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 1, 1, 0],
[1, 0, 0, 0, 1, 0, 0],
[1, 0, 0, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 1, 0, 1],
[1, 0, 1, 0, 0, 1, 1],
[1, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
], dtype=np.uint8)
num_labels, labels = cv2.connectedComponents(mask)
# https://docs.opencv.org/master/d9/d8b/tutorial_py_contours_hierarchy.html
contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
polygons = [cv2_contour_to_shapely(x, resolution=2) for x in contours]
#plot_mask_and_contours(labels, polygons)
self.assertTrue(all(p.is_valid for p in polygons), "All polygons must be valid")
def test_polygon_validity_random_field(self):
mask = cv2.resize(np.random.randn(10, 10), (500, 500), interpolation=cv2.INTER_LANCZOS4)
mask += cv2.resize(np.random.randn(51, 51), (500, 500), interpolation=cv2.INTER_LANCZOS4)
mask += cv2.resize(np.random.randn(167, 167), (500, 500), interpolation=cv2.INTER_LANCZOS4)
mask += 0.5*np.random.randn(500, 500)
mask = (mask > 0.9).astype(np.uint8)
num_labels, labels = cv2.connectedComponents(mask)
# https://docs.opencv.org/master/d9/d8b/tutorial_py_contours_hierarchy.html
contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
parent_info = hierarchy[0][:, 3]
is_hole = parent_info != -1
for ix, hole in enumerate(is_hole):
if hole:
is_hole[ix] = not is_hole[parent_info[ix]]
polygons = [simplify_polygon(cv2_contour_to_shapely(cnt, hole=hole, resolution=2))
for cnt, hole in zip(contours, is_hole)]
#plot_mask_and_contours(labels, polygons)
self.assertTrue(all(p.is_valid for p in polygons), "All polygons must be valid")
Functions
def plot_mask_and_contours(mask, contours)
-
Expand source code
def plot_mask_and_contours(mask, contours): import matplotlib.pyplot as plt plt.imshow(mask) for cnt in contours: try: plt.plot(*cnt.xy, 'b-x') except NotImplementedError: try: plt.plot(*cnt.boundary.xy, 'r-x') except NotImplementedError: for b in cnt.boundary: plt.plot(*b.xy, 'g-x') plt.xlim(plt.xlim() + np.array([-1, 1])) plt.ylim(plt.ylim() + np.array([1, -1])) plt.show()
Classes
class TestPolygonExtraction (methodName='runTest')
-
A class whose instances are single test cases.
By default, the test code itself should be placed in a method named 'runTest'.
If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.
Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.
If it is necessary to override the init method, the base class init method must always be called. It is important that subclasses should not change the signature of their init method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.
When subclassing TestCase, you can set these attributes: * failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'. * longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed. * maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.
Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.
Expand source code
class TestPolygonExtraction(unittest.TestCase): def test_polygon_validity(self): mask = np.array([ [0, 1, 0, 0, 0, 0, 1], [0, 0, 1, 0, 0, 0, 1], [1, 0, 0, 1, 0, 0, 1], [0, 1, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 1], [0, 1, 1, 0, 1, 1, 1], [0, 1, 1, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0], [1, 1, 0, 1, 1, 0, 0], [0, 1, 0, 1, 1, 0, 0], [0, 0, 0, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 1, 0, 0], [0, 0, 1, 0, 1, 1, 0], [0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], [0, 1, 0, 1, 1, 1, 0], [1, 0, 0, 0, 1, 0, 0], [1, 0, 0, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 1, 0, 1], [1, 0, 1, 0, 0, 1, 1], [1, 0, 0, 0, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1], ], dtype=np.uint8) num_labels, labels = cv2.connectedComponents(mask) # https://docs.opencv.org/master/d9/d8b/tutorial_py_contours_hierarchy.html contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) polygons = [cv2_contour_to_shapely(x, resolution=2) for x in contours] #plot_mask_and_contours(labels, polygons) self.assertTrue(all(p.is_valid for p in polygons), "All polygons must be valid") def test_polygon_validity_random_field(self): mask = cv2.resize(np.random.randn(10, 10), (500, 500), interpolation=cv2.INTER_LANCZOS4) mask += cv2.resize(np.random.randn(51, 51), (500, 500), interpolation=cv2.INTER_LANCZOS4) mask += cv2.resize(np.random.randn(167, 167), (500, 500), interpolation=cv2.INTER_LANCZOS4) mask += 0.5*np.random.randn(500, 500) mask = (mask > 0.9).astype(np.uint8) num_labels, labels = cv2.connectedComponents(mask) # https://docs.opencv.org/master/d9/d8b/tutorial_py_contours_hierarchy.html contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) parent_info = hierarchy[0][:, 3] is_hole = parent_info != -1 for ix, hole in enumerate(is_hole): if hole: is_hole[ix] = not is_hole[parent_info[ix]] polygons = [simplify_polygon(cv2_contour_to_shapely(cnt, hole=hole, resolution=2)) for cnt, hole in zip(contours, is_hole)] #plot_mask_and_contours(labels, polygons) self.assertTrue(all(p.is_valid for p in polygons), "All polygons must be valid")
Ancestors
- unittest.case.TestCase
Methods
def test_polygon_validity(self)
-
Expand source code
def test_polygon_validity(self): mask = np.array([ [0, 1, 0, 0, 0, 0, 1], [0, 0, 1, 0, 0, 0, 1], [1, 0, 0, 1, 0, 0, 1], [0, 1, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 1], [0, 1, 1, 0, 1, 1, 1], [0, 1, 1, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0], [1, 1, 0, 1, 1, 0, 0], [0, 1, 0, 1, 1, 0, 0], [0, 0, 0, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 1, 0, 0], [0, 0, 1, 0, 1, 1, 0], [0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], [0, 1, 0, 1, 1, 1, 0], [1, 0, 0, 0, 1, 0, 0], [1, 0, 0, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 1, 0, 1], [1, 0, 1, 0, 0, 1, 1], [1, 0, 0, 0, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1], ], dtype=np.uint8) num_labels, labels = cv2.connectedComponents(mask) # https://docs.opencv.org/master/d9/d8b/tutorial_py_contours_hierarchy.html contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) polygons = [cv2_contour_to_shapely(x, resolution=2) for x in contours] #plot_mask_and_contours(labels, polygons) self.assertTrue(all(p.is_valid for p in polygons), "All polygons must be valid")
def test_polygon_validity_random_field(self)
-
Expand source code
def test_polygon_validity_random_field(self): mask = cv2.resize(np.random.randn(10, 10), (500, 500), interpolation=cv2.INTER_LANCZOS4) mask += cv2.resize(np.random.randn(51, 51), (500, 500), interpolation=cv2.INTER_LANCZOS4) mask += cv2.resize(np.random.randn(167, 167), (500, 500), interpolation=cv2.INTER_LANCZOS4) mask += 0.5*np.random.randn(500, 500) mask = (mask > 0.9).astype(np.uint8) num_labels, labels = cv2.connectedComponents(mask) # https://docs.opencv.org/master/d9/d8b/tutorial_py_contours_hierarchy.html contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) parent_info = hierarchy[0][:, 3] is_hole = parent_info != -1 for ix, hole in enumerate(is_hole): if hole: is_hole[ix] = not is_hole[parent_info[ix]] polygons = [simplify_polygon(cv2_contour_to_shapely(cnt, hole=hole, resolution=2)) for cnt, hole in zip(contours, is_hole)] #plot_mask_and_contours(labels, polygons) self.assertTrue(all(p.is_valid for p in polygons), "All polygons must be valid")