1

我从图像中提取字符的代码有问题:

例子:

原始图像 原来的 处理后的图像 最终的

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

最终的

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

4

1 回答 1

1

(这里是mahotas的作者):

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

final = final.astype('uint8')

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

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

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

pylab.imshow(255 * final)
pylab.show()
于 2014-11-06T09:59:49.140 回答