水平轴图像"黑色墨水量"的直方图

use*_*828 8 python linux gnuplot imagemagick image-processing

我有一个黑白图像(或pdf)文件,并希望得到图像的水平轮廓的直方图.也就是说,对于图像中的每一列,我想要列中像素的灰度值之和.如果图像是X乘Y像素,我将得到0之间的X数(对于完全黑色的列)和255*Y(对于完全白色的列).

请看这个漫画的第二个面板 滑稽

我想要一个像这样的直方图,但是每个bin都代表图像中x坐标(像素)处的所有"黑色墨水".

作为一个贫穷的研究生,我只能使用Linux命令行,FOSS程序(ImageMagick,gnuplot,Perl,g ++等).像GIMP这样的东西只有在我可以通过终端运行命令时才会有用,因为我无法访问GUI.可视化输出文件稍后会有所帮助,但不是必需的.

有谁知道我可以提取这些信息的方式?搜索"图像配置文件"只会导致颜色配置文件的信息.

and*_*ras 12

我会用两个最喜欢的免费实用工具给出一个答案:python和gnuplot.

作为一名计算机研究生,我的建议是,如果你想免费做一些事情,python是你可以学习使用的最通用的工具之一.

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

#!/usr/bin/python

import Image            # basic image processing/manipulation, just what we want

im = Image.open('img.png')       # open the image file as a python image object
with 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
Run Code Online (Sandbox Code Playgroud)

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

#!/usr/bin/gnuplot

set terminal pngcairo size 925,900
set output 'plot.png'
#set terminal pdfcairo
#set output 'plot.pdf'
set multiplot

## first plot
set origin 0,0.025              # this plot will be on the bottom
set size 1,0.75                 # and fill 3/4 of the whole canvas

set title "Black count in XKCD 'Self-Description'"
set xlabel 'Column'
set ylabel "Black\ncount" norotate offset screen 0.0125

set lmargin at screen 0.15      # make plot area correct size
set rmargin at screen 0.95      # width = 740 px = (0.95-0.15)*925 px

set border 0                    # these settings are just to make the data
set grid                        # stand out and not overlap with the tics, etc.
set tics nomirror
set xtics scale 0.5 out 
set ytics scale 0

set 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 plot
set origin 0,0.75               # this plot will be on top
set size 1,0.25                 # and fill 1/4 of the canvas

unset ylabel; unset xlabel      # clean up a bit...
unset border; unset grid; unset tics; unset title

set size ratio -1               # ensures image proper ratio
plot 'img.png' binary filetype=png with rgbimage

unset multiplot         # important to unset multiplot!
Run Code Online (Sandbox Code Playgroud)

要运行这些脚本,请将它们保存在与要绘制的图像相同的目录中(在本例中为XKCD漫画,我保存为img.png).让它们可执行.在bash中这是

$ chmod 755 grayscalecount.py plot.plt
Run Code Online (Sandbox Code Playgroud)

然后(如果安装了python + image module + gnuplot),就可以运行了

$ ./grayscalecount.py
$ ./plot.plt
Run Code Online (Sandbox Code Playgroud)

在我的计算机上运行带有gnuplot 4.4.3的Ubuntu 11.10,我最后得到了这个很酷的情节:

在此输入图像描述

**旁注*:gnuplot可以制作很多不同的直方图.我认为这种风格很好地展示了数据,但你可以研究格式化gnuplot直方图的数据.

有很多方法可以让python自己创建绘图或使用gnuplot(matplotlib,pygnuplot,gnuplot-py),但我并不那么容易.Gnuplot非常适合绘图,并且有很多方法可以让它与python,bash,C++等完美搭配.