使用matplotlib绘制图像颜色直方图 [英] Plot image color histogram using matplotlib

查看:1932
本文介绍了使用matplotlib绘制图像颜色直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成图像的颜色直方图。我正在使用PIL读取图像文件,并试图通过matplotlib绘制相同的图形。

I am trying to generate a color histogram of an image. I am using PIL for reading image files and trying to plot the same through 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,也需要花费很长时间才能消耗大量内存)

Update: After some trial and error this code plots the histogram, but not the colors! (Takes laboriously long consumes ton loads of memory even for a 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;我终止执行的帖子。我怎样才能解决问题?

On execution, the program begins to consume infinite memory and no display is provided. OS memory usage went from < 380 MB to > 2.5 GB in matter of couple of minutes; post which I terminated the execution. How can I get solve the problem?

以下是具有显性红色调的图像颜色直方图示例:

Here is an example of a color histogram of image with dominant Red shades:

推荐答案

我累了你的更新代码,它工作正常。这正是我正在尝试的:

I tired your update code and it worked fine. Here is exactly what I am trying:

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试图在每个酒吧周围放置一个黑色寄宿生?如果有很多酒吧,酒吧要薄到有颜色?如果您有工具栏,则可以放大绘图并查看条形图确实具有颜色。因此,如果您设置边缘颜色:

I think matplotlib is trying to put a black boarder around every bar? If there are to many bars, the bar is to thin to have color? If you have the toolbar, you can zoom in on the plot and see that the bars do indeed have color. So, if you set the edge color by:

for idx, c in enumerate(colors):
        plt.bar(idx, c[0], color=hexencode(c[1]),edgecolor=hexencode(c[1]))

它有效!

要处理的图像:

Image to be processed:

结果:

性能分析

已排序by tottime:

    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上。如果你想加快速度,你可以找到一个不同的绘图工具,减少条形图的数量,尝试用画布上的矩形自己做?

It seems that all the time is spent in matplotlib. If you want to speed it up, you can either find a different plotting tool, reduce the number of 'bars', try doing it yourself with rectangle on a canvas?

时间:


  1. 上面的代码:75s

  2. 为每一个画一条线,即plt.plot ([n,n],[0,count]等。):95s

这篇关于使用matplotlib绘制图像颜色直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆