【绘图】将plt.imshow转换为cv2.imshow代码
程序员文章站
2022-05-19 13:32:43
...
1.本质来说就是一个画框函数的变换,改改就好了,另外注意的是plt.rectangle的坐标绘制依次为左上顶点坐标(x,y),w,h
cv2.rectangle的坐标为左上顶点坐标和右下顶点坐标。
2.其次plt是rgb方式读取。im = im[:, :, (2, 1, 0)] 对应着红,绿,蓝
cv2 imread读取图像之后,imshow是bgr通道顺序来进行显示的,如果使用pltshow去显示就会不正常。
也就是
bgr 0 1 2 opencv
rgb 2 1 0 plt
两段代码应用场景均是使用opencv去读取图像/视频帧。使用imread得到的图片帧(即im)通道为bgr
tf-faster-RCNN的代码
def vis_detections(im, class_name, dets, thresh=0.5):
"""Draw detected bounding boxes."""
inds = np.where(dets[:, -1] >= thresh)[0]
if len(inds) == 0:
return
im = im[:, :, (2, 1, 0)] #因为用imread读入图像默认通道顺序为bgr,如果想要plt去显示,要转成rgb通道顺序
fig, ax = plt.subplots(figsize=(12, 12))
ax.imshow(im, aspect='equal')
for i in inds:
bbox = dets[i, :4]
score = dets[i, -1]
ax.add_patch(
plt.Rectangle((bbox[0], bbox[1]),
bbox[2] - bbox[0],
bbox[3] - bbox[1], fill=False,
edgecolor='red', linewidth=3.5)
)
ax.text(bbox[0], bbox[1] - 2,
'{:s} {:.3f}'.format(class_name, score),
bbox=dict(facecolor='blue', alpha=0.5),
fontsize=14, color='white')
ax.set_title(('{} detections with '
'p({} | box) >= {:.1f}').format(class_name, class_name,
thresh),
fontsize=14)
plt.axis('off')
plt.tight_layout()
plt.draw()
修改为opencv显示的代码:
#这里注意的是,opencv imshow默认三通道顺序为bgr (imread读入的通道就是bgr,所以对应imshow也是bgr)
def vis_detections(im, class_name, dets, thresh=0.5):
"""Draw detected bounding boxes."""
inds = np.where(dets[:, -1] >= thresh)[0]
if len(inds) == 0:
return
# im = im[:, :, (2, 1, 0)]
for i in inds:
bbox = dets[i, :4]
score = dets[i, -1]
cv2.rectangle(im,(bbox[0], bbox[1]),( bbox[2],bbox[3]),(0,255,255), 2)
text='{:s} {:.3f}'.format(class_name, score)
cv2.putText(im, text, (int(bbox[0]), int(bbox[1] - 2)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
对通道数不懂得看这: https://blog.csdn.net/liu13364876993/article/details/79867061
上一篇: Python数据交换
下一篇: JavaSE高级应用编程——工具类练习题