我有一個PyQT4應用程序,它在Matplotlib圖中顯示中等大小的圖像。我正在顯示的測試圖像大約是5Mb(2809 x 1241像素)。順便說一下,我使用GDAL讀取數據。圖像被讀入一個數組中,其中無數值被掩蓋。然後用標準化的值和指定的顏色表顯示Matplotlib中過多的內存使用imshow
它似乎使用過多的內存來顯示一個5MB文件。我所看到的是,需要140mb的內存來顯示以全分辨率讀入的圖像。 (使用imshow的應用程序註釋掉了60mb的內存,vs206)隨着圖像以多個圖形顯示,每個圖像使用額外的200m內存,問題變得更加嚴重。在大約3或4個數字顯示的應用程序開始陷入內存使用進入700-900 mb範圍內。
我瞭解matplotlib必須存儲所有像素,即使它只顯示縮減採樣子集以匹配屏幕分辨率。我可能最終會寫程序,只讀取一定數量的像素以匹配數字大小。但由於這個應用程序將在8個獨立的屏幕上顯示多達8個地圖,我擔心它仍然使用過多的內存。
所以我的問題是:
1)這看起來像是用來顯示簡單的彩色地圖圖像的過多內存嗎?它對我有用。
2)有什麼我可以做的,以減少這種內存使用情況?例如使用整數數據類型,釋放內存等。
3)我應該使用其他什麼策略來處理這種內存使用情況?例如採樣(可能不以全屏幕分辨率1900x1200的非常有效),切換到64位架構等
感謝, 代碼如下
import sys, os, random
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import matplotlib
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
from matplotlib.figure import Figure
import matplotlib.colors as colors
import numpy as np
from osgeo import gdal, gdalconst
gridfile = r"i:\vistrails\workingfiles\secondseason\secondseason_workfile_2012_02_28b\brt_1\brt_prob_map.tif"
class AppForm(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.create_main_frame()
ds = gdal.Open(gridfile, gdal.GA_ReadOnly)
ary = ds.GetRasterBand(1).ReadAsArray(buf_ysize=500, buf_xsize=300)
ndval = ds.GetRasterBand(1).GetNoDataValue()
rasterdata = np.ma.masked_array(ary, mask=(ary==ndval))
del ary
self.axes.imshow(rasterdataint, cmap=matplotlib.cm.jet)
del rasterdata
def create_main_frame(self):
self.main_frame = QWidget()
# Create the mpl Figure and FigCanvas objects.
# 5x4 inches, 100 dots-per-inch
#
self.dpi = 100
self.fig = Figure((5.0, 4.0), dpi=self.dpi)
self.canvas = FigureCanvas(self.fig)
self.canvas.setParent(self.main_frame)
self.axes = self.fig.add_subplot(111)
self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame)
vbox = QVBoxLayout()
vbox.addWidget(self.canvas)
vbox.addWidget(self.mpl_toolbar)
self.main_frame.setLayout(vbox)
self.setCentralWidget(self.main_frame)
def main():
app = QApplication(sys.argv)
form = AppForm()
form.show()
app.exec_()
if __name__ == "__main__":
main()
我有一個類似的問題,我通過使用'draw()'而不是'show()'和刪除imshow對象來解決它。例如:'self.im = imshow(your_data)','draw()','del self.im'。我不知道這是你的情況 – maupertius 2012-04-24 15:25:53