From 841da2b3f88b47737cf43f7c98d7689ff86d86fe Mon Sep 17 00:00:00 2001 From: Gustav Larsson Date: Mon, 20 Apr 2015 09:39:38 -0500 Subject: [PATCH] Fixed crashing bug in slic. The enforce connectivity step of slic runs a BFS and puts the results in coord_list. This coord_list has room for max_size elements, which is easily overrun if there is a large connected element. This change limits the BFS to max_size, so that if a connected area is bigger than max_size, it will be split up instead of the program crashing. --- skimage/segmentation/_slic.pyx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/skimage/segmentation/_slic.pyx b/skimage/segmentation/_slic.pyx index 1418e902..0935550c 100644 --- a/skimage/segmentation/_slic.pyx +++ b/skimage/segmentation/_slic.pyx @@ -255,7 +255,7 @@ def _enforce_label_connectivity_cython(Py_ssize_t[:, :, ::1] segments, #perform a breadth first search to find # the size of the connected component - while bfs_visited != current_segment_size: + while bfs_visited < current_segment_size < max_size: for i in range(6): zz = coord_list[bfs_visited, 0] + ddz[i] yy = coord_list[bfs_visited, 1] + ddy[i] @@ -271,6 +271,8 @@ def _enforce_label_connectivity_cython(Py_ssize_t[:, :, ::1] segments, coord_list[current_segment_size, 1] = yy coord_list[current_segment_size, 2] = xx current_segment_size += 1 + if current_segment_size >= max_size: + break elif (connected_segments[zz, yy, xx] >= 0 and connected_segments[zz, yy, xx] != current_new_label): adjacent = connected_segments[zz, yy, xx]