我正在繪製PGM圖像: 以下是我正在使用的data。在imshow中關閉一個錯誤?
問題是一些顯示的像素是錯誤的。例如:
- 靠近圖像的頂部的三個灰色框是的值11(因此它們應該是紅色,而不是紅色)
- 頂行中兩個黃色像素 - 它們是值8,所以他們應該是黃綠色,不是黃色
任何人都可以解釋這些差異,以及如何解決它們?
這裏是我的源:
from pylab import *
import numpy
LABELS = range(13)
NUM_MODES = len(LABELS)
def read_ascii_pgm(fname):
"""
Very fragile PGM reader. It's OK since this is only for reading files
output by my own app.
"""
lines = open(fname).read().strip().split('\n')
assert lines[0] == 'P2'
width, height = map(int, lines[1].split(' '))
assert lines[2] == '13'
pgm = numpy.zeros((height, width), dtype=numpy.uint8)
for i in range(height):
cols = lines[3+i].split(' ')
for j in range(width):
pgm[i,j] = int(cols[j])
return pgm
def main():
import sys
assert len(sys.argv) > 1
fname = sys.argv[1]
pgm = read_ascii_pgm(fname)
# EDIT: HACK!
pgm[0,0] = 12
cmap = cm.get_cmap('spectral', NUM_MODES)
imshow(pgm, cmap=cmap, interpolation='nearest')
edit = True
if edit:
cb = colorbar()
else:
ticks = [ (i*11./NUM_MODES + 6./NUM_MODES) for i in range(NUM_MODES) ]
cb = colorbar(ticks=ticks)
cb.ax.set_yticklabels(map(str, LABELS))
savefig('imshow.png')
if __name__ == '__main__':
main()
編輯
我現在看到這裏發生了什麼。基本上,imshow
似乎是這樣:
- 確定所述動態範圍(如
[ min(image), max(image) ]
- 表示的在彩色圖中指定的顏色(13色)
我想什麼此使用數字它做的是:
- 使用我創建色彩映射時指定的動態範圍(13)
- 表示此使用13種顏色的彩色映射
我可以通過強制的圖像的動態範圍驗證這是13(見線標記HACK
)。有一個更好的方法嗎?
這裏有一個更新的圖像:
+1 - 這似乎是一個問題,很多人可能會遇到,讓「幾乎正確「的結果。會討厭這個__以後出版。 – samplebias 2011-05-25 17:37:56