'''

  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)
'''


[((395, 419), 'cv2.imread', 'cv2.imread', (['"""ponte2.jpg"""'], {}), "('ponte2.jpg')\n", (405, 419), False, 'import cv2\n'), ((532, 569), 'cv2.cvtColor', 'cv2.cvtColor', (['img', 'cv2.COLOR_BGR2GRAY'], {}), '(img, cv2.COLOR_BGR2GRAY)\n', (544, 569), False, 'import cv2\n'), ((579, 611), 'cv2.GaussianBlur', 'cv2.GaussianBlur', (['img', '(7, 7)', '(0)'], {}), '(img, (7, 7), 0)\n', (595, 611), False, 'import cv2\n'), ((637, 686), 'cv2.threshold', 'cv2.threshold', (['suave', '(160)', '(255)', 'cv2.THRESH_BINARY'], {}), '(suave, 160, 255, cv2.THRESH_BINARY)\n', (650, 686), False, 'import cv2\n'), ((699, 752), 'cv2.threshold', 'cv2.threshold', (['suave', '(160)', '(255)', 'cv2.THRESH_BINARY_INV'], {}), '(suave, 160, 255, cv2.THRESH_BINARY_INV)\n', (712, 752), False, 'import cv2\n'), ((972, 1018), 'cv2.imshow', 'cv2.imshow', (['"""Binarização da imagem"""', 'resultado'], {}), "('Binarização da imagem', resultado)\n", (982, 1018), False, 'import cv2\n'), ((1019, 1033), 'cv2.waitKey', 'cv2.waitKey', (['(0)'], {}), '(0)\n', (1030, 1033), False, 'import cv2\n'), ((1065, 1161), 'cv2.adaptiveThreshold', 'cv2.adaptiveThreshold', (['suave', '(255)', 'cv2.ADAPTIVE_THRESH_MEAN_C', 'cv2.THRESH_BINARY_INV', '(21)', '(5)'], {}), '(suave, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.\n    THRESH_BINARY_INV, 21, 5)\n', (1086, 1161), False, 'import cv2\n'), ((1164, 1264), 'cv2.adaptiveThreshold', 'cv2.adaptiveThreshold', (['suave', '(255)', 'cv2.ADAPTIVE_THRESH_GAUSSIAN_C', 'cv2.THRESH_BINARY_INV', '(21)', '(5)'], {}), '(suave, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.\n    THRESH_BINARY_INV, 21, 5)\n', (1185, 1264), False, 'import cv2\n'), ((1349, 1406), 'cv2.imshow', 'cv2.imshow', (['"""Binarização adaptativa da imagem"""', 'resultado'], {}), "('Binarização adaptativa da imagem', resultado)\n", (1359, 1406), False, 'import cv2\n'), ((1407, 1421), 'cv2.waitKey', 'cv2.waitKey', (['(0)'], {}), '(0)\n', (1418, 1421), False, 'import cv2\n'), ((912, 935), 'numpy.hstack', 'np.hstack', (['[img, suave]'], {}), '([img, suave])\n', (921, 935), True, 'import numpy as np\n'), ((941, 963), 'numpy.hstack', 'np.hstack', (['[bin, binI]'], {}), '([bin, binI])\n', (950, 963), True, 'import numpy as np\n'), ((1289, 1312), 'numpy.hstack', 'np.hstack', (['[img, suave]'], {}), '([img, suave])\n', (1298, 1312), True, 'import numpy as np\n'), ((1318, 1341), 'numpy.hstack', 'np.hstack', (['[bin1, bin2]'], {}), '([bin1, bin2])\n', (1327, 1341), True, 'import numpy as np\n')]