@@ -137,13 +137,16 @@ def grayscale(self):
137137 data = cv2 .cvtColor (self ._data , cv2 .COLOR_BGR2GRAY )
138138 return Image (data )
139139
140+ def blackwhite (self ):
141+ data = cv2 .threshold (self ._data , 128 , 255 , cv2 .THRESH_BINARY | cv2 .THRESH_OTSU )
142+ return Image (data )
143+
140144 def invert (self ):
141145 data = cv2 .bitwise_not (self ._data )
142146 return Image (data )
143147
144148 def binarize (self , threshold = - 1 ):
145149 data = cv2 .cvtColor (self ._data , cv2 .COLOR_BGR2GRAY )
146- #data = cv2.adaptiveThreshold(data, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 11, 0)
147150 if threshold < 0 :
148151 data = cv2 .adaptiveThreshold (data , 255 , cv2 .ADAPTIVE_THRESH_MEAN_C , cv2 .THRESH_BINARY_INV , max ((self ._kernel .shape [0 ]/ 2 * 2 )+ 1 , 3 ), 3 )
149152 else :
@@ -161,10 +164,7 @@ def get_average(self):
161164 def find_blobs (self , minsize = 0 , maxsize = 10000000 ):
162165 blobs = []
163166 image = contours = hyerarchy = None
164- if "2.4" in cv2 .__version__ :
165- contours , hyerarchy = cv2 .findContours (self ._data , cv2 .RETR_LIST , cv2 .CHAIN_APPROX_SIMPLE )
166- else :
167- image , contours , hyerarchy = cv2 .findContours (self ._data , cv2 .RETR_LIST , cv2 .CHAIN_APPROX_SIMPLE )
167+ image , contours , hyerarchy = cv2 .findContours (self ._data , cv2 .RETR_LIST , cv2 .CHAIN_APPROX_SIMPLE )
168168
169169 for c in contours :
170170 area = cv2 .contourArea (c )
@@ -280,6 +280,12 @@ def find_ar_code(self):
280280 (rect [0 ][1 ]+ rect [1 ][1 ]+ rect [2 ][1 ]+ rect [3 ][1 ])/ 4 ])
281281 return {"codes" : codes , "positions" : positions }
282282
283+ def draw_blob (self , blob ):
284+ cv2 .drawContours (self ._data , blob .contour (), - 1 , (0 ,255 ,0 ))
285+
286+ def draw_rect (self , x1 , y1 , x2 , y2 , color , thickness ):
287+ cv2 .rectangle (self ._data , (x1 ,y1 ), (x2 ,y2 ), color , thickness )
288+
283289 def to_jpeg (self ):
284290 ret , jpeg_array = cv2 .imencode ('.jpeg' , self ._data )
285291 return np .array (jpeg_array ).tostring ()
0 commit comments