Search code examples
pythonimageocrmahotas

python mahotas: Applying threshold filter and saving image as pgn


I'm having a problem with a code that extracts characters from an image:

Example:

Original image original Processed image final

I am applying a bunch of filters to try and extract specific characters from these signs and send them to my OCR software (gaussian filters, water shedding and thresholding).

I want to apply an otsu threshold filter to an image, but when I try to save the image, it is getting converted to float64, making it unsaveable as a 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)

gives me

  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.

if I try to make an intermediate variable that saves the image with the threshold applied, the image goes blank:

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

What can I do to fix this?


Solution

  • (Author of mahotas here):

    My guess is that the image is correctly saved, but you are looking at it wrong. After the line

    final = final.astype('uint8')
    

    final is a uint8 image with 0s and 1s. Thus the "white" bits are very dark. Try multiplying it by 255:

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

    Or save it like this, but visualize it in a stretched version:

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