3

私はスクリプトに取り組んできましたが、基本的に次のことを行う必要があります。

  • 画像をグレースケールにします(または二調です。どちらがうまく機能するかを確認するために両方で遊んでみます)。
  • 個々の列を処理し、各列の正味強度値を作成します。
  • 結果を順序付きリストに分割します。

ImageMagickでこれを行う非常に簡単な方法があります(出力テキストを処理するためにいくつかのLinuxユーティリティが必要ですが)が、PythonとPILでこれを行う方法は実際にはわかりません。

これが私がこれまでに持っているものです:

from PIL import Image

image_file = 'test.tiff'

image = Image.open(image_file).convert('L')

histo = image.histogram()
histo_string = ''

for i in histo:
  histo_string += str(i) + "\n"

print(histo_string)

これは何かを出力します(私は結果をグラフ化しようとしています)が、ImageMagick出力のようには見えません。これを使用して、スキャンした本の継ぎ目と内容を検出しています。

助けてくれた人に感謝します!


今のところ、機能する(厄介な)ソリューションがあります:

from PIL import Image
import numpy

def smoothListGaussian(list,degree=5):
  window=degree*2-1
  weight=numpy.array([1.0]*window)
  weightGauss=[]

  for i in range(window):
    i=i-degree+1
    frac=i/float(window)
    gauss=1/(numpy.exp((4*(frac))**2))
    weightGauss.append(gauss)

  weight=numpy.array(weightGauss)*weight
  smoothed=[0.0]*(len(list)-window)

  for i in range(len(smoothed)):
    smoothed[i]=sum(numpy.array(list[i:i+window])*weight)/sum(weight)

  return smoothed

image_file = 'verypurple.jpg'
out_file = 'out.tiff'

image = Image.open(image_file).convert('1')
image2 = image.load()
image.save(out_file)

intensities = []

for x in xrange(image.size[0]):
  intensities.append([])

  for y in xrange(image.size[1]):
    intensities[x].append(image2[x, y] )

plot = []

for x in xrange(image.size[0]):
  plot.append(0)

  for y in xrange(image.size[1]):
    plot[x] += intensities[x][y]

plot = smoothListGaussian(plot, 10)

plot_str = ''

for x in range(len(plot)):
  plot_str += str(plot[x]) + "\n"

print(plot_str)
4

2 に答える 2

11

numpyを使用しているようです。最初にグレースケール画像をnumpy配列に変換し、次にnumpyを使用して軸に沿って合計します。ボーナス:1D配列を入力として受け入れるように修正すると、平滑化関数の実行速度が大幅に向上することがわかります。

>>> from PIL import Image
>>> import numpy as np
>>> i = Image.open(r'C:\Pictures\pics\test.png')
>>> a = np.array(i.convert('L'))
>>> a.shape
(2000, 2000)
>>> b = a.sum(0) # or 1 depending on the axis you want to sum across
>>> b.shape
(2000,)
于 2010-11-16T22:09:05.093 に答える
7

PIL のドキュメントから、histogram画像内の各ピクセル値のピクセル数のリストが得られます。グレースケール イメージの場合、0 から 255 までの 256 の異なる値があり、から返されるリストにimage.histogramは 256 のエントリが含まれます。

于 2010-11-15T19:48:06.530 に答える