![](/img/trans.png)
[英]In matplotlib, plotting the histogram using plot and the returns of hist
[英]Plot image color histogram using matplotlib
我正在嘗試生成圖像的顏色直方圖。 我正在使用 PIL 讀取圖像文件並嘗試通過 matplotlib 繪制相同的圖像。
im = Image.open(sys.argv[1])
w, h = im.size
colors = im.getcolors(w*h) #Returns a list [(pixel_count, (R, G, B))]
更新:經過反復試驗,此代碼繪制了直方圖,而不是顏色! (即使對於 320x480 jpeg,也需要花費大量時間消耗大量內存)
for idx, c in enumerate(colors):
plt.bar(idx, c[0], color=hexencode(c[1]))
plt.show()
在哪里,
def hexencode(rgb):
return '#%02x%02x%02x' % rgb
在執行時,程序開始消耗無限內存並且不提供任何顯示。 幾分鍾內,操作系統內存使用量從 < 380 MB 增加到 > 2.5 GB; 我終止執行的帖子。 我怎樣才能解決問題?
以下是具有主要紅色陰影的圖像顏色直方圖示例:
我試過你的更新代碼,效果很好。 這正是我正在嘗試的:
import PIL
from PIL import Image
from matplotlib import pyplot as plt
im = Image.open('./color_gradient.png')
w, h = im.size
colors = im.getcolors(w*h)
def hexencode(rgb):
r=rgb[0]
g=rgb[1]
b=rgb[2]
return '#%02x%02x%02x' % (r,g,b)
for idx, c in enumerate(colors):
plt.bar(idx, c[0], color=hexencode(c[1]))
plt.show()
更新:
我認為 matplotlib 試圖在每個條形周圍放置一個黑色邊框。 如果條形太多,則條形太薄而無法着色。 如果您有工具欄,您可以放大繪圖並查看條形圖確實有顏色。 因此,如果您通過以下方式設置邊緣顏色:
for idx, c in enumerate(colors):
plt.bar(idx, c[0], color=hexencode(c[1]),edgecolor=hexencode(c[1]))
有用!
要處理的圖像:
結果:
剖析
按時間排序:
ncalls tottime percall cumtime percall filename:lineno(function)
1 23.424 23.424 24.672 24.672 {built-in method mainloop}
460645 8.626 0.000 8.626 0.000 {numpy.core.multiarray.array}
22941 7.909 0.000 18.447 0.001 C:\Python27\lib\site-packages\matplotlib\artist.py:805(get_aliases)
6814123 3.900 0.000 3.900 0.000 {method 'startswith' of 'str' objects}
22941 2.244 0.000 2.244 0.000 {dir}
276714 2.140 0.000 2.140 0.000 C:\Python27\lib\weakref.py:243(__init__)
4336835 2.029 0.000 2.029 0.000 {getattr}
1927044 1.962 0.000 3.027 0.000 C:\Python27\lib\site-packages\matplotlib\artist.py:886(is_alias)
114811 1.852 0.000 3.883 0.000 C:\Python27\lib\site-packages\matplotlib\colors.py:317(to_rgba)
69559 1.653 0.000 2.841 0.000 C:\Python27\lib\site-packages\matplotlib\path.py:86(__init__)
68869 1.425 0.000 11.700 0.000 C:\Python27\lib\site-packages\matplotlib\patches.py:533(_update_patch_transform)
161205 1.316 0.000 1.618 0.000 C:\Python27\lib\site-packages\matplotlib\cbook.py:381(is_string_like)
1 1.232 1.232 1.232 1.232 {gc.collect}
344698 1.116 0.000 1.513 0.000 C:\Python27\lib\site-packages\matplotlib\cbook.py:372(iterable)
22947 1.111 0.000 3.768 0.000 {built-in method draw_path}
276692 1.024 0.000 3.164 0.000 C:\Python27\lib\site-packages\matplotlib\transforms.py:80(__init__)
2 1.021 0.510 1.801 0.900 C:\Python27\lib\site-packages\matplotlib\colors.py:355(to_rgba_array)
22947 0.818 0.000 14.677 0.001 C:\Python27\lib\site-packages\matplotlib\patches.py:371(draw)
183546/183539 0.793 0.000 2.030 0.000 C:\Python27\lib\site-packages\matplotlib\units.py:117(get_converter)
138006 0.756 0.000 1.267 0.000 C:\Python27\lib\site-packages\matplotlib\transforms.py:126(set_children)
按累計時間排序
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.001 0.001 84.923 84.923 C:\Python27\test.py:23(imageProcess)
1 0.013 0.013 44.079 44.079 C:\Python27\lib\site-packages\matplotlib\pyplot.py:2080(bar)
1 0.286 0.286 43.825 43.825 C:\Python27\lib\site-packages\matplotlib\axes.py:4556(bar)
1 0.000 0.000 40.533 40.533 C:\Python27\lib\site-packages\matplotlib\pyplot.py:123(show)
1 0.000 0.000 40.533 40.533 C:\Python27\lib\site-packages\matplotlib\backend_bases.py:69(__call__)
22943 0.171 0.000 24.964 0.001 C:\Python27\lib\site-packages\matplotlib\patches.py:508(__init__)
1 0.000 0.000 24.672 24.672 C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py:68(mainloop)
1 0.000 0.000 24.672 24.672 C:\Python27\lib\lib-tk\Tkinter.py:323(mainloop)
1 23.424 23.424 24.672 24.672 {built-in method mainloop}
22947 0.499 0.000 24.654 0.001 C:\Python27\lib\site-packages\matplotlib\patches.py:55(__init__)
22941 0.492 0.000 20.180 0.001 C:\Python27\lib\site-packages\matplotlib\artist.py:1136(setp)
22941 0.135 0.000 18.730 0.001 C:\Python27\lib\site-packages\matplotlib\artist.py:788(__init__)
22941 7.909 0.000 18.447 0.001 C:\Python27\lib\site-packages\matplotlib\artist.py:805(get_aliases)
72/65 0.071 0.001 17.118 0.263 {built-in method call}
24/12 0.000 0.000 17.095 1.425 C:\Python27\lib\lib-tk\Tkinter.py:1405(__call__)
22941 0.188 0.000 16.647 0.001 C:\Python27\lib\site-packages\matplotlib\axes.py:1476(add_patch)
1 0.000 0.000 15.861 15.861 C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py:429(show)
1 0.000 0.000 15.861 15.861 C:\Python27\lib\lib-tk\Tkinter.py:909(update)
1 0.000 0.000 15.846 15.846 C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py:219(resize)
1 0.000 0.000 15.503 15.503 C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py:238(draw)
似乎所有的時間都花在了 matplotlib 上。 如果您想加快速度,您可以找到不同的繪圖工具或減少“條”的數量。 嘗試在畫布上用矩形自己做。
定時:
我一直在研究類似的問題,並在 5 年之后遇到了這個線程。 對於較大的圖像,接受的答案花費的時間太長,無法簡單地“生成圖像的顏色直方圖”。
我使用 openCV 而不是 PIL 來生成直方圖,而且速度要快得多。
import cv2
import numpy as np
from matplotlib import pyplot as plt
file0 = 'image.jpg'
img = cv2.imread(file0)
color = ('b','g','r')
plt.figure()
for i,col in enumerate(color):
histr = cv2.calcHist([img],[i],None,[256],[0,256])
plt.plot(histr,color = col)
plt.xlim([0,256])
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.