回答編集履歴
1
Update
test
CHANGED
@@ -18,3 +18,32 @@
|
|
18
18
|
if w < W-1 and grp[h][w+1] == 0 and select[h][w+1] == 1:
|
19
19
|
determine_next(h, w+1)
|
20
20
|
```
|
21
|
+
|
22
|
+
**余談**
|
23
|
+
|
24
|
+
> またグループ分けの処理をもっと最適化することはできるでしょうか。
|
25
|
+
|
26
|
+
最適化ではありませんが、Scipy パッケージの `scipy.ndimage.label` メソッドで同様の処理を行うことができます。
|
27
|
+
|
28
|
+
[scipy.ndimage.label — SciPy v1.9.1 Manual](https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.label.html)
|
29
|
+
|
30
|
+
```python
|
31
|
+
from scipy.ndimage import label
|
32
|
+
import numpy as np
|
33
|
+
|
34
|
+
arr = np.array([
|
35
|
+
[0, 1, 0, 1],
|
36
|
+
[1, 1, 1, 0],
|
37
|
+
[0, 0, 0, 1],
|
38
|
+
[0, 1, 1, 1],
|
39
|
+
])
|
40
|
+
|
41
|
+
lw, num = label(arr)
|
42
|
+
print(lw)
|
43
|
+
|
44
|
+
#
|
45
|
+
[[0 1 0 2]
|
46
|
+
[1 1 1 0]
|
47
|
+
[0 0 0 3]
|
48
|
+
[0 3 3 3]]
|
49
|
+
```
|