python mahotas:应用阈值过滤器并将图像保存为pgn

时间:2014-11-05 18:03:30

标签: python image ocr mahotas

我遇到了从图片中提取字符的代码问题:

示例:

原始图片 original 处理过的图像 final

我正在应用一堆过滤器来尝试从这些标志中提取特定字符并将它们发送到我的OCR软件(高斯滤波器,水流脱落和阈值处理)。

我想对图像应用otsu阈值过滤器,但是当我尝试保存图像时,它会转换为float64,使其无法保存为png:

seeds,nseeds = mahotas.label(dnaf < T)

labeled = mahotas.cwatershed(dnaf.max() - dnaf, seeds)

labeled = labeled.astype('uint8')

T = mahotas.thresholding.otsu(labeled)

pylab.imshow(labeled > T)
pylab.show()

mahotas.imsave('py.png', labeled > T)

给了我

  File "imgtest2.py", line 67, in <module>
    mahotas.imsave('py.png', labeled > T)
  File "/usr/local/lib/python2.7/site-packages/mahotas/io/freeimage.py", line 798, in imsave
    write(img, filename)
  File "/usr/local/lib/python2.7/site-packages/mahotas/io/freeimage.py", line 586, in write
    bitmap, fi_type = _array_to_bitmap(array)
  File "/usr/local/lib/python2.7/site-packages/mahotas/io/freeimage.py", line 653, in _array_to_bitmap
    'mahotas.freeimage: cannot write arrays of given type and shape.')
  ValueError: mahotas.freeimage: cannot write arrays of given type and shape.

如果我尝试制作一个中间变量来保存应用了阈值的图像,则图像变为空白:

seeds,nseeds = mahotas.label(dnaf < T)

labeled = mahotas.cwatershed(dnaf.max() - dnaf, seeds)

labeled = labeled.astype('uint8')

T = mahotas.thresholding.otsu(labeled)

final = labeled > T

final = final.astype('uint8')

pylab.imshow(final)
pylab.show()

mahotas.imsave('py.png', final)

final

我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:1)

(mahotas的作者):

我的猜测是图像被正确保存,但你看错了。行后

final = final.astype('uint8')

final是带有uint80 s的1图片。因此,&#34;白色&#34;比特很暗。尝试将它乘以255:

mahotas.imsave('py.png', 255 * final)

或者像这样保存它,但是在拉伸版本中可视化:

pylab.imshow(255 * final)
pylab.show()