Skip to content

Commit 8f3d6a2

Browse files
committed
Split function drawBoxesOnRGB and drawBoxesOnGray + label scale
1 parent 9e9ab39 commit 8f3d6a2

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

MTM/__init__.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def matchTemplates(listTemplates, image, method=cv2.TM_CCOEFF_NORMED, N_object=f
171171
return bestHits
172172

173173

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 ):
175175
'''
176176
Return a copy of the image with predicted template locations as bounding boxes overlaid on the image
177177
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
200200
for hit in listHit:
201201
x,y,w,h = hit['BBox']
202202
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)
204204

205205
return outImage
206206

207207

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+
208242
if __name__ == '__main__':
209243

210244
from skimage.data import coins

0 commit comments

Comments
 (0)