【问题标题】:python mahotas: Applying threshold filter and saving image as pgnpython mahotas:应用阈值过滤器并将图像保存为 pgn
【发布时间】:2014-11-05 18:03:29
【问题描述】:

我在使用从图像中提取字符的代码时遇到问题:

例子:

原图 处理后的图像

我正在应用一堆过滤器来尝试从这些标志中提取特定字符并将它们发送到我的 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)

我能做些什么来解决这个问题?

【问题讨论】:

    标签: python image ocr mahotas


    【解决方案1】:

    (这里是mahotas的作者):

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

    final = final.astype('uint8')
    

    final 是带有0s 和1s 的uint8 图像。因此,“白色”位非常暗。尝试将其乘以 255:

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

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

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

    【讨论】:

      猜你喜欢
      • 2013-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      • 2020-10-07
      • 1970-01-01
      • 2013-03-18
      相关资源
      最近更新 更多