图像“黑色墨水水平”的直方图(横轴)

图像“黑色墨水水平”的直方图(横轴),第1张

图像“黑色墨水水平”的直方图(横轴)

我将使用我最喜欢的两个免费实用工具,两个动作给出答案:python和gnuplot。

作为一名(计算)研究生,我的建议是,如果您想免费做事,python是您可以学习使用的最通用的工具之一。

这是做第一部分的python脚本,计算灰度值(从0表示白色到255表示黑色):

#!/usr/bin/pythonimport Image # basic image processing/manipulation, just what we wantim = Image.open('img.png')       # open the image file as a python image objectwith open('data.dat', 'w') as f: # open the data file to be written    for i in range(im.size[0]):  # loop over columns        counter = sum(im.getpixel((i,j)) for j in range(im.size[1]))        f.write(str(i)+'t'+str(counter)+'n')  # write to data file

令人震惊的无痛!现在让gnuplot制作直方图*:

#!/usr/bin/gnuplotset terminal pngcairo size 925,900set output 'plot.png'#set terminal pdfcairo#set output 'plot.pdf'set multiplot## first plotset origin 0,0.025   # this plot will be on the bottomset size 1,0.75      # and fill 3/4 of the whole canvasset title "Black count in XKCD 'Self-Description'"set xlabel 'Column'set ylabel "Blackncount" norotate offset screen 0.0125set lmargin at screen 0.15      # make plot area correct sizeset rmargin at screen 0.95      # width = 740 px = (0.95-0.15)*925 pxset border 0         # these settings are just to make the dataset grid  # stand out and not overlap with the tics, etc.set tics nomirrorset xtics scale 0.5 out set ytics scale 0set xr [0:740]       # x range such that there is one spike/pixel## uncomment if gnuplot version >= 4.6.0## this will autoset the x and y ranges#stats 'data.dat'#set xr [STATS_min_x:STATS_max_x+1]#set yr [STATS_min_y:STATS_may_y]plot 'data.dat' with impulse notitle lc 'black'## second plotset origin 0,0.75    # this plot will be on topset size 1,0.25      # and fill 1/4 of the canvasunset ylabel; unset xlabel      # clean up a bit...unset border; unset grid; unset tics; unset titleset size ratio -1    # ensures image proper ratioplot 'img.png' binary filetype=png with rgbimageunset multiplot         # important to unset multiplot!

运行这些脚本,请将它们与要绘制的图像保存在同一目录中(本例中为XKCD漫画,我将其另存为

img.png
)。使它们可执行。在bash中,这是

$ chmod 755 grayscalecount.py plot.plt

然后(如果全部安装了python + image模块+ gnuplot),则可以运行

$ ./grayscalecount.py$ ./plot.plt

在运行gnuplot 4.4.3的Ubuntu 11.10的计算机上,我得到了下面这个漂亮的图:

*附注:gnuplot可以制作许多不同的直方图。我认为这种样式可以很好地展示数据,但是您可以研究格式化gnuplot直方图的数据。

有很多方法可以使python本身或使用gnuplot(matplotlib,pygnuplot,gnuplot-
py)进行绘图,但我对它们不那么熟悉。Gnuplot可以很好地编写脚本以进行绘图,并且有很多方法可以使其在python,bash,C
++等中很好地发挥作用。



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5668646.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存