Skip to content

Instantly share code, notes, and snippets.

@fiorentinogiuseppe
Created January 17, 2020 18:29
Show Gist options
  • Save fiorentinogiuseppe/cfccd7035be3e9acb2e84c8c55a07c91 to your computer and use it in GitHub Desktop.
Save fiorentinogiuseppe/cfccd7035be3e9acb2e84c8c55a07c91 to your computer and use it in GitHub Desktop.
Função que aplica a binarização na imagem
def binarization(image):
"""
Função que aplica a binarização na imagem.
Parameters
----------
image : PIL.Image.Image
Imagem para ser binarizada.
Returns
-------
PIL.Image.Image
Imagem binarizada.
"""
image = image.convert('RGB')
npimage = np.asarray(image).astype(np.uint8)
npimage[:, :, 0] = 0
npimage[:, :, 2] = 0
im = cv2.cvtColor(npimage, cv2.COLOR_RGB2GRAY)
ret, thresh = cv2.threshold(im, 80, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
binimage = Image.fromarray(thresh)
return binimage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment