画bounding box的几种方法 利用matplotlib.patches 利用openCV

在做深度学习的图像的localization时,需要画出bounding box和他们的类别名称,这里介绍两种方式

  1. 利用PIL中的ImageDraw.rectangle
  2. 利用matplotlib.patches
  3. 利用openCV
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from PIL import Image, ImageDraw
import numpy as np

def (img, boxes, label, classes):
'''
Args:
img: PIL image
boxes: Nx4
label: [c1, c2 ,c3,....] 1xN
classes: ['bird', 'cat', 'dog', ....]
'''
font = ImageFont.truetype("arial.ttf", 24)
draw = ImageDraw.Draw(img)
for i, box in enumerate(boxes):
draw.rectangle(list(box), outline='red')
draw.text((box[0],box[1]), classes[label[i]-1], font=font)
img.show()


img = Image.open('12.jpg')
label = [1,2]
classes = ['sasiki', 'naruto']
boxes = np.array([[304,70,514,386],[547,55,762,360]], dtype=float)
draw_bbox(img, boxes, label, classes)

Imgur

利用matplotlib.patches

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import matplotlib.pyplot as plt
import cv2
import numpy as np

def (img, boxes, label=None, classes=None):
'''
Args:
img: narrary image
boxes: Nx4 [[xmin, ymin, xmax, ymax],[.. ..]]
label: [c1, c2 ,c3,....] 1xN
classes: ['bird', 'cat', 'dog', ....]
'''
plt.imshow(img)
wh = boxes[:,2:4] - boxes[:,0:2]
for i in range(len(boxes)):
currentAxis=plt.gca()
rect=patches.Rectangle((boxes[i,0], boxes[i,1]),wh[i, 0],wh[i, 1],linewidth=1,edgecolor='r',facecolor='none')
currentAxis.add_patch(rect)
plt.text(boxes[i,0], boxes[i,1], classes[label[i]-1], color='white',fontsize=16)
plt.show()


img = cv2.imread('12.jpg')
boxes = np.array([[304,70,514,386],[547,55,762,360]], dtype=float)
label = [1, 2]
classes = ['sasiki', 'naruto']
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
draw_bbox(img, boxes, label, classes)

Imgur

利用openCV

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import cv2
import numpy as np
from PIL import Image, ImageDraw

def (img, boxes, label, classes):
for i in range(len(boxes)):
pt1 = (int(boxes[i,0]),int(boxes[i,1]))
pt2 = (int(boxes[i,2]),int(boxes[i,3]))
cv2.rectangle(img, pt1, pt2, (0,255,0), 4)

font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, classes[label[i]-1], pt1, font, 2, (255,0,0), 6)


img_new = Image.fromarray(img)
img_new.show()
# using matplotlib.pyplot
plt.imshow(img)
plt.show()


img = cv2.imread('12.jpg')
boxes = np.array([[304,70,514,386],[547,55,762,360]], dtype=float)
label = [1, 2]
classes = ['sasiki', 'naruto']

img = BGR2RGB(img)
draw_bbox(img, boxes, label, classes)

Imgur