@@ -171,7 +171,7 @@ def matchTemplates(listTemplates, image, method=cv2.TM_CCOEFF_NORMED, N_object=f
171
171
return bestHits
172
172
173
173
174
- def drawBoxes (image , listHit , boxThickness = 2 , boxColor = (255 , 255 , 00 ), showLabel = False , labelColor = (255 , 255 , 0 ) ):
174
+ def drawBoxesOnRGB (image , listHit , boxThickness = 2 , boxColor = (255 , 255 , 00 ), showLabel = False , labelColor = (255 , 255 , 0 ), labelScale = 0.5 ):
175
175
'''
176
176
Return a copy of the image with predicted template locations as bounding boxes overlaid on the image
177
177
The name of the template can also be displayed on top of the bounding box with showLabel=True
@@ -200,11 +200,45 @@ def drawBoxes(image, listHit, boxThickness=2, boxColor=(255, 255, 00), showLabel
200
200
for hit in listHit :
201
201
x ,y ,w ,h = hit ['BBox' ]
202
202
cv2 .rectangle (outImage , (x , y ), (x + w , y + h ), color = boxColor , thickness = boxThickness )
203
- if showLabel : cv2 .putText (outImage , text = hit ['TemplateName' ], org = (x , y ), fontFace = cv2 .FONT_HERSHEY_SIMPLEX , fontScale = 0.5 , color = labelColor , lineType = cv2 .LINE_AA )
203
+ if showLabel : cv2 .putText (outImage , text = hit ['TemplateName' ], org = (x , y ), fontFace = cv2 .FONT_HERSHEY_SIMPLEX , fontScale = labelScale , color = labelColor , lineType = cv2 .LINE_AA )
204
204
205
205
return outImage
206
206
207
207
208
+ def drawBoxesOnGray (image , listHit , boxThickness = 2 , boxColor = 255 , showLabel = False , labelColor = 255 , labelScale = 0.5 ):
209
+ '''
210
+ Same as drawBoxesOnRGB but with Graylevel.
211
+ If a RGB image is provided, the output image will be a grayscale image
212
+ Parameters
213
+ ----------
214
+ - image : image in which the search was performed
215
+ - listHit: list of hit as returned by matchTemplates or findMatches
216
+ - boxThickness: int
217
+ thickness of bounding box contour in pixels
218
+ - boxColor: int
219
+ Gray level for the bounding box
220
+ - showLabel: Boolean
221
+ Display label of the bounding box (field TemplateName)
222
+ - labelColor: int
223
+ Gray level for the label
224
+
225
+ Returns
226
+ -------
227
+ outImage: Single channel grayscale image
228
+ original image with predicted template locations depicted as bounding boxes
229
+ '''
230
+ # Convert RGB to grayscale
231
+ if image .ndim == 3 : outImage = cv2 .cvtColor (image , cv2 .COLOR_RGB2GRAY ) # convert to RGB to be able to show detections as color box on grayscale image
232
+ else : outImage = image .copy ()
233
+
234
+ for hit in listHit :
235
+ x ,y ,w ,h = hit ['BBox' ]
236
+ cv2 .rectangle (outImage , (x , y ), (x + w , y + h ), color = boxColor , thickness = boxThickness )
237
+ if showLabel : cv2 .putText (outImage , text = hit ['TemplateName' ], org = (x , y ), fontFace = cv2 .FONT_HERSHEY_SIMPLEX , fontScale = labelScale , color = labelColor , lineType = cv2 .LINE_AA )
238
+
239
+ return outImage
240
+
241
+
208
242
if __name__ == '__main__' :
209
243
210
244
from skimage .data import coins
0 commit comments