我已经编写了此代码以检查对象包围框,但是当我给
axes
添加title
时,它没有显示出来(我想将文件号作为标题显示)。#!/home/ckim/anaconda2/bin/python
#%pylab
import os.path as osp
import sys
import cv2
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as patches
def add_path(path):
if path not in sys.path:
sys.path.insert(0, path)
def move_figure(f, x, y):
backend = matplotlib.get_backend()
f.canvas.manager.window.move(x,y)
plt.show()
# Add lib to PYTHONPATH
lib_path = osp.join('/home/ckim/Neuro/py-faster-rcnn/', 'lib')
add_path(lib_path)
import datasets
import datasets.pascal_voc as pv
#plt.ion()
fig, ax = plt.subplots(figsize=(8,8))
im = cv2.imread(osp.join('/home/ckim/Neuro/py-faster-rcnn/data/VOCdevkit2007/VOC2007/JPEGImages/', '{0:06d}'.format(eval(sys.argv[1])) + '.jpg'))
#im = cv2.imread(osp.join('000005.jpg'))
im = im[:, :, (2, 1, 0)]
ax.imshow(im, aspect='equal')
#res = pv._load_pascal_annotation(sys.argv[1])
d = datasets.pascal_voc('trainval', '2007')
res = d._load_pascal_annotation('{0:06d}'.format(eval(sys.argv[1])))
# return {'boxes' : boxes,
# 'gt_classes': gt_classes,
# 'gt_overlaps' : overlaps,
# 'flipped' : False}
for i in range(len(res['boxes'])):
x1 = res['boxes'][i][0]
y1 = res['boxes'][i][1]
x2 = res['boxes'][i][2]
y2 = res['boxes'][i][3]
ax.add_patch(patches.Rectangle((x1,y1), x2-x1, y2-y1, fill=False, edgecolor='red', linewidth=1.5))
ax.text(x1, y1 - 5, '{:s}'.format(d._classes[res['gt_classes'][i]]), \
bbox=dict(facecolor='blue', alpha=0.5), fontsize=14, color='white')
#thismanager = get_current_fig_manager()
#thismanager.window.SetPosition((500, 0))
#thismanager.window.wm_geometry("+500+0")
move_figure(fig, 500, 500)
#fig.show()
#fig.suptitle("Title x")
ax.set_title("Title x")
plt.pause(0)
为了测试问题,我将代码简化如下,但这个简化版本对于图形绘制(情况1)和图像显示(情况2)都有效。 我找不到与上述代码的区别。 有人能告诉我上述代码出了什么问题吗? (关于标题未显示)
#!/home/ckim/anaconda2/bin/python
import cv2
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(8,8))
# case 1 : plot
#ax.plot([1,2,3,4],[1,4,9,16])
# case 2 : image display
im = cv2.imread('000005.jpg')
im = im[:, :, (2, 1, 0)]
ax.imshow(im, aspect='equal')
ax.set_title("Title x")
plt.pause(0)
move_figure
在调用ax.set_title
之前没有调用plt.show
吗?解决方法是将ax.set_title("Title x")
移动到move_figure(...)
的前面。如果没有这么做,可能会将标题覆盖在画布上... - Ed Smith