matplotlib.axe.draw_artist加快图像及其他如文本、矩形框等的显示
2020-02-12 17:23
525 查看
matplotlib.axe.draw_artist加快图像及其他如文本、矩形框等的显示
Intro
最近在弄一个软件的图像的显示部分,多帧图像在经过处理之后要马上显示出来,开始单纯的使用matplotlib.axe.draw()方法显示图像,总感觉图像的显示很慢,于是想使用各种办法加速图像的显示。
method
这里只讲述使用draw_artist()方法。通过查阅官网给出的信息可以了解到:draw()方法显示图像的时候会画很多重复性的东西(比如轴等),这样导致多帧图像的连续显示变得相对较慢。而使用draw_artist()方法便可以避免每次重画一些不必要的地方。
因此连续多帧图像的显示建议使用draw_artist()方法
此外,我们有时还想在图像上添加各种说明的东西,比如一些矩形框和文本啊。这里需要记下来的是:使用draw_artist()方法,需要对这些添加的元素都是用该方法。代码示例如下:代码复制粘贴于自己构建的一个类,因此带有self参数。看的时候可以将其忽略掉。
self.fig = plt.figure(figsize=(7.2, 5.4)) # 新建图片 self.axes = self.fig.add_subplot(1, 1, 1) # 在图片中新建subplot self.axes.xaxis.set_major_locator(plt.NullLocator()) self.axes.yaxis.set_major_locator(plt.NullLocator()) self.fig.subplots_adjust(top=1, bottom=0, left=0, right=1, hspace=0, wspace=0) self.axes.margins(0, 0) FigureCanvas.__init__(self, self.fig) # 激活Figure窗口 X = 10 * np.random.rand(self.row, self.col) # 初始化时给出一副随机的图像 self.axes.imshow(X, interpolation='nearest', cmap=self.colorStyle) # 颜色风格'jet''bone''rainbow''bone''binary''OrRd''plasma' rect = patches.Rectangle((box[1],box[0]),box[3]-box[1],box[2]-box[0],linewidth = 1,edgecolor='black',fill=False) rect = self.axes.add_patch(rect) t = self.axes.text(box[1],box[0]+1,label,color = 'red') im = self.axes.imshow(figure_data.matrix, interpolation='nearest', cmap=self.colorStyle) print(box,label) # self.draw() # self.draw稍微慢一点 self.axes.draw_artist(im) # 以下四行代码 加快图像的更新 self.axes.draw_artist(rect) self.axes.draw_artist(t) self.blit(self.axes.bbox)
好像其中加速的原因是因为后面还使用了blit()函数,具体不是很清楚,如果哪位知道,还希望不吝赐教。
ps:由于这是我写的第一个博客,有很多地方写的不是很清楚,没有太多的参考价值,更多的可能只是作为自己的一个记录。
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- 为什么matplotlib显示opencv图像不正常
- Numpy、matplotlib实现二维数据到图像的转换,添加colormap,无边距显示
- [python] Kmeans文本聚类算法+PAC降维+Matplotlib显示聚类图像
- UserWarning: Matplotlib is currently using agg,matplotlib图像无法显示问题
- 演示如何实现Matplotlib绘图并保存图像但不显示图形的方法
- 【原】在Matplotlib绘图过程中设置X轴的刻度和显示文本
- 四、Matplotlib系列---pyplot的文本显示
- Pycharm Matplotlib 图像无法显示问题
- matplotlib实现显示伪彩色图像及色度条
- Python中使用matplotlib显示图像色彩失真问题
- 用matplotlib画图时,给图像标题、横纵坐标轴的标签、图例显示中文名字
- python opencv 使用 Matplotlib显示图像
- Pycharm Matplotlib 图像不显示问题
- python实现Kmeans文本聚类,通过PCA降维和Matplotlib显示聚类3d三维图像
- Linux下python matplotlib.pyplot在图像上显示中文的问题
- skimage读取图像——matplotlib显示图像
- wxpython和matplotlib显示图像的一个例子
- PyCharm之Matplotlib不显示图像
- matplotlib使用show方法后不能显示图像的问题
- matplotlib不能正常显示图像