gpt4 book ai didi

python mahotas : Applying threshold filter and saving image as pgn

转载 作者:太空宇宙 更新时间:2023-11-03 18:06:34 25 4
gpt4 key购买 nike

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

示例:

原始图像 original处理后的图像 final

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

我想对图像应用大津阈值过滤器,但当我尝试保存图像时,它被转换为 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

我可以做什么来解决这个问题?

最佳答案

(此处为 mahotas 的作者):

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

final = final.astype('uint8')

final 是一个包含 01uint8 图像。因此,“白色”位非常暗。尝试将其乘以 255:

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

或者像这样保存它,但以拉伸(stretch)版本可视化它:

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

关于 python mahotas : Applying threshold filter and saving image as pgn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26764015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com