'''

  Livro-Introdução-a-Visão-Computacional-com-Python-e-OpenCV-3

Repositório de imagens
https://github.com/opencv/opencv/tree/master/samples/data
'''

import cv2
import numpy as np
from matplotlib import pyplot as plt
#import mahotas

VERMELHO = (0, 0, 255)
VERDE = (0, 255, 0)
AZUL = (255, 0, 0)
AMARELO = (0, 255, 255)
BRANCO = (255,255,255)
CIANO = (255, 255, 0)
PRETO = (0, 0, 0)

img = cv2.imread('ponte2.jpg') # Flag 1 = Color, 0 = Gray, -1 = Unchanged
img = img[::2,::2] # Diminui a imagem

#Binarização com limiar
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

suave = cv2.GaussianBlur(img, (7, 7), 0) # aplica blur
(T, bin) = cv2.threshold(suave, 160, 255, cv2.THRESH_BINARY)
(T, binI) = cv2.threshold(suave, 160, 255, cv2.THRESH_BINARY_INV)

'''
resultado = np.vstack([
    np.hstack([suave, bin]),
    np.hstack([binI, cv2.bitwise_and(img, img, mask = binI)])
    ])
'''
resultado = np.vstack([
    np.hstack([img, suave]),
    np.hstack([bin, binI])
    ])

cv2.imshow("Binarização da imagem", resultado)
cv2.waitKey(0)

#Threshold adaptativo

bin1 = cv2.adaptiveThreshold(suave, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 21, 5)
bin2 = cv2.adaptiveThreshold(suave, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 21, 5)

resultado = np.vstack([
    np.hstack([img, suave]),
    np.hstack([bin1, bin2])
    ])
cv2.imshow("Binarização adaptativa da imagem", resultado)
cv2.waitKey(0)

#Threshold com Otsu e Riddler-Calvard
'''
T = mahotas.thresholding.otsu(suave)
temp = img.copy()
temp[temp > T] = 255
temp[temp < 255] = 0
temp = cv2.bitwise_not(temp)
T = mahotas.thresholding.rc(suave)
temp2 = img.copy()
temp2[temp2 > T] = 255
temp2[temp2 < 255] = 0
temp2 = cv2.bitwise_not(temp2)
resultado = np.vstack([
    np.hstack([img, suave]),
    np.hstack([temp, temp2])
    ])
cv2.imshow("Binarização com método Otsu e Riddler-Calvard", resultado)
cv2.waitKey(0)
'''


[((22, 6, 22, 30), 'cv2.imread', 'cv2.imread', ({(22, 17, 22, 29): '"""ponte2.jpg"""'}, {}), "('ponte2.jpg')", False, 'import cv2\n'), ((26, 6, 26, 43), 'cv2.cvtColor', 'cv2.cvtColor', ({(26, 19, 26, 22): 'img', (26, 24, 26, 42): 'cv2.COLOR_BGR2GRAY'}, {}), '(img, cv2.COLOR_BGR2GRAY)', False, 'import cv2\n'), ((28, 8, 28, 40), 'cv2.GaussianBlur', 'cv2.GaussianBlur', ({(28, 25, 28, 28): 'img', (28, 30, 28, 36): '(7, 7)', (28, 38, 28, 39): '0'}, {}), '(img, (7, 7), 0)', False, 'import cv2\n'), ((29, 11, 29, 60), 'cv2.threshold', 'cv2.threshold', ({(29, 25, 29, 30): 'suave', (29, 32, 29, 35): '160', (29, 37, 29, 40): '255', (29, 42, 29, 59): 'cv2.THRESH_BINARY'}, {}), '(suave, 160, 255, cv2.THRESH_BINARY)', False, 'import cv2\n'), ((30, 12, 30, 65), 'cv2.threshold', 'cv2.threshold', ({(30, 26, 30, 31): 'suave', (30, 33, 30, 36): '160', (30, 38, 30, 41): '255', (30, 43, 30, 64): 'cv2.THRESH_BINARY_INV'}, {}), '(suave, 160, 255, cv2.THRESH_BINARY_INV)', False, 'import cv2\n'), ((43, 0, 43, 48), 'cv2.imshow', 'cv2.imshow', ({(43, 11, 43, 36): '"""Binarização da imagem"""', (43, 38, 43, 47): 'resultado'}, {}), "('Binarização da imagem', resultado)", False, 'import cv2\n'), ((44, 0, 44, 14), 'cv2.waitKey', 'cv2.waitKey', ({(44, 12, 44, 13): '(0)'}, {}), '(0)', False, 'import cv2\n'), ((48, 7, 48, 98), 'cv2.adaptiveThreshold', 'cv2.adaptiveThreshold', ({(48, 29, 48, 34): 'suave', (48, 36, 48, 39): '255', (48, 41, 48, 67): 'cv2.ADAPTIVE_THRESH_MEAN_C', (48, 69, 48, 90): 'cv2.THRESH_BINARY_INV', (48, 92, 48, 94): '21', (48, 96, 48, 97): '5'}, {}), '(suave, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.\n    THRESH_BINARY_INV, 21, 5)', False, 'import cv2\n'), ((49, 7, 49, 102), 'cv2.adaptiveThreshold', 'cv2.adaptiveThreshold', ({(49, 29, 49, 34): 'suave', (49, 36, 49, 39): '255', (49, 41, 49, 71): 'cv2.ADAPTIVE_THRESH_GAUSSIAN_C', (49, 73, 49, 94): 'cv2.THRESH_BINARY_INV', (49, 96, 49, 98): '21', (49, 100, 49, 101): '5'}, {}), '(suave, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.\n    THRESH_BINARY_INV, 21, 5)', False, 'import cv2\n'), ((55, 0, 55, 59), 'cv2.imshow', 'cv2.imshow', ({(55, 11, 55, 47): '"""Binarização adaptativa da imagem"""', (55, 49, 55, 58): 'resultado'}, {}), "('Binarização adaptativa da imagem', resultado)", False, 'import cv2\n'), ((56, 0, 56, 14), 'cv2.waitKey', 'cv2.waitKey', ({(56, 12, 56, 13): '(0)'}, {}), '(0)', False, 'import cv2\n'), ((39, 4, 39, 27), 'numpy.hstack', 'np.hstack', ({(39, 14, 39, 26): '[img, suave]'}, {}), '([img, suave])', True, 'import numpy as np\n'), ((40, 4, 40, 26), 'numpy.hstack', 'np.hstack', ({(40, 14, 40, 25): '[bin, binI]'}, {}), '([bin, binI])', True, 'import numpy as np\n'), ((52, 4, 52, 27), 'numpy.hstack', 'np.hstack', ({(52, 14, 52, 26): '[img, suave]'}, {}), '([img, suave])', True, 'import numpy as np\n'), ((53, 4, 53, 27), 'numpy.hstack', 'np.hstack', ({(53, 14, 53, 26): '[bin1, bin2]'}, {}), '([bin1, bin2])', True, 'import numpy as np\n')]