Skip to content

Commit

Permalink
Added a new aggresive cut function
Browse files Browse the repository at this point in the history
  • Loading branch information
saisurbehera committed Jul 4, 2024
1 parent 299bdc5 commit 86571bf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
21 changes: 21 additions & 0 deletions random/understand_p1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,27 @@
" return ans"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[<image.Image at 0x79231c61bc20>]"
]
},
"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,
Expand Down
25 changes: 25 additions & 0 deletions src/arclang/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


Point = namedtuple("Point", ["x", "y"])
from scipy.ndimage import label, generate_binary_structure


class Image:
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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]])
Expand Down

0 comments on commit 86571bf

Please sign in to comment.