11from random import randint
22
3- import cv2
4-
5- import numpy as np
6-
73from .utils import debug_print
84
5+ # Warning: lazy load of cv2 and numpy via local imports
6+
7+ # TODO LH Decide on use of skimage's watershed
98# Breaks pyinstaller build
109# from skimage.morphology import watershed
1110USE_OPENCV_WATERSHED = True
@@ -31,6 +30,8 @@ def _right_sized(contour, image, container_filter=True, size_filter=True):
3130 result : boolean
3231 Object is of correct sizing.
3332 """
33+ import cv2
34+
3435 image_size = image .shape
3536 x , y , w , h = cv2 .boundingRect (contour )
3637 area = image_size [0 ] * image_size [1 ]
@@ -80,6 +81,8 @@ def _process_contours(image, contours, hierarchy, callback, index=0,
8081 size_filter : boolean
8182 Filters large objects.
8283 """
84+ import cv2
85+
8386 result = []
8487 while index >= 0 :
8588 callback ()
@@ -101,6 +104,8 @@ def _process_contours(image, contours, hierarchy, callback, index=0,
101104# alternate process, may be useful if we abandon hierarchical contours later
102105def _process_contours_iterate (image , contours , hierarchy , index = 0 ,
103106 size_filter = True ):
107+ import cv2
108+
104109 result = []
105110 for contour in contours :
106111 if right_sized (contour , image .shape , size_filter = size_filter ):
@@ -124,6 +129,9 @@ def remove_lines(image):
124129 mask : (M, N) array
125130 Mask of image without lines.
126131 """
132+ import cv2
133+ import numpy as np
134+
127135 gray = cv2 .cvtColor (image , cv2 .cv .CV_BGR2GRAY )
128136 v_edges = cv2 .Sobel (gray , cv2 .CV_32F , 1 , 0 , None , 1 )
129137 h_edges = cv2 .Sobel (gray , cv2 .CV_32F , 0 , 1 , None , 1 )
@@ -184,6 +192,9 @@ def segment_edges(image, window=None, threshold=12, lab_based=True,
184192 (rects, display) : list, (M, N, 3) array
185193 Region results and visualization image.
186194 """
195+ import cv2
196+ import numpy as np
197+
187198 if not callback :
188199 def swallow (* args , ** kwargs ):
189200 pass
@@ -291,6 +302,9 @@ def swallow(*args, **kwargs):
291302
292303
293304def segment_intensity (image , window = None ):
305+ import cv2
306+ import numpy as np
307+
294308 if window :
295309 subimage = np .array (image )
296310 x , y , w , h = window
@@ -334,6 +348,9 @@ def segment_grabcut(image, window=None, seeds=[]):
334348 (rects, display) : list, (M, N, 3) array
335349 Region results and visualization image.
336350 """
351+ import cv2
352+ import numpy as np
353+
337354 if window :
338355 subimage = np .array (image )
339356 x , y , w , h = window
@@ -419,6 +436,9 @@ def segment_watershed(image, window=None):
419436 (rects, display) : list, (M, N, 3) array
420437 Region results and visualization image.
421438 """
439+ import cv2
440+ import numpy as np
441+
422442 if window :
423443 subimage = np .array (image )
424444 x , y , w , h = window
@@ -458,6 +478,9 @@ def segment_watershed(image, window=None):
458478
459479
460480if __name__ == "__main__" :
481+ import cv2
482+ import numpy as np
483+
461484 image = cv2 .imread ("../../data/drawer.jpg" )
462485 scaled = 1.0
463486 image = cv2 .resize (image , (int (image .shape [1 ] * scaled ),
0 commit comments