From 86571bf070441c4902c74a5af53fb00cbaa663d7 Mon Sep 17 00:00:00 2001 From: sai Date: Wed, 3 Jul 2024 21:59:19 -0400 Subject: [PATCH] Added a new aggresive cut function --- random/understand_p1.ipynb | 21 +++++++++++++++++++++ src/arclang/image.py | 25 +++++++++++++++++++++++++ tests/test_image.py | 4 ++-- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/random/understand_p1.ipynb b/random/understand_p1.ipynb index c080a58..ff76732 100644 --- a/random/understand_p1.ipynb +++ b/random/understand_p1.ipynb @@ -139,6 +139,27 @@ " return ans" ] }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "img = Image(0, 0, 3, 3, [[1, 0, 1], [0, 1, 0], [1, 0, 1]])\n", + "img.list_components()" + ] + }, { "cell_type": "code", "execution_count": 6, diff --git a/src/arclang/image.py b/src/arclang/image.py index b89c60b..b1c35bf 100644 --- a/src/arclang/image.py +++ b/src/arclang/image.py @@ -8,6 +8,7 @@ Point = namedtuple("Point", ["x", "y"]) +from scipy.ndimage import label, generate_binary_structure class Image: @@ -138,7 +139,31 @@ def list_components(self) -> List["Image"]: def count_components(self) -> int: return len(self.list_components()) + + def count_components_col(self) -> int: + return len(self.list_same_color_components()) + + def aggressive_connected_components(self, connectivity=2) -> List["Image"]: + """ + Find connected components with a more aggressive connectivity. + + :param connectivity: 1 for 4-connectivity, 2 for 8-connectivity, + can be increased for more aggressive connectivity + :return: List of Image objects, each representing a component + """ + # Create a structure for the given connectivity + struct = generate_binary_structure(2, connectivity) + + # Label the connected components + labeled_array, num_features = label(self.mask > 0, structure=struct) + + components = [] + for i in range(1, num_features + 1): + component = np.where(labeled_array == i, self.mask, 0) + components.append(Image(self.x, self.y, self.w, self.h, component)) + + return components def majority_col(self, include0: int = 0) -> int: diff --git a/tests/test_image.py b/tests/test_image.py index 39a1129..ef049e6 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -2,9 +2,9 @@ import numpy as np -from arclang.image import Image # Assuming your classes are in 'image_module.py' from arclang.image import Piece from arclang.image import Point +from arclang.image import Image # Assuming your classes are in 'image_module.py' class TestImageAdditional(unittest.TestCase): @@ -39,7 +39,7 @@ def test_is_rectangle(self): def test_count_components(self): img = Image(0, 0, 3, 3, [[1, 0, 1], [0, 1, 0], [1, 0, 1]]) - self.assertEqual(img.count_components(), 5) + self.assertEqual(img.count_components_col(), 5) def test_majority_col(self): img = Image(0, 0, 3, 3, [[1, 2, 1], [2, 1, 2], [1, 2, 1]])