# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import cv2
import numpy as np

img = cv2.imread("imgs/bookpage.jpg")

retval, threshold = cv2.threshold(img, 12, 255, cv2.THRESH_BINARY)

cv2.imshow('original',img)
cv2.imshow('threshold',threshold)
cv2.waitKey(0)
cv2.destroyAllWindows()

grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
retval2, threshold2 = cv2.threshold(grayscaled, 12, 255, cv2.THRESH_BINARY)

cv2.imshow('original',img)
cv2.imshow('threshold',threshold)
cv2.imshow('threshold2',threshold2)
cv2.waitKey(0)
cv2.destroyAllWindows()

# +
# Gaussian Adaptive Threshold
# -

gaus = cv2.adaptiveThreshold(grayscaled, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 1)
# take grayscaled img with max of 255, first take the adaptive gaussian treshold, then make it binary

cv2.imshow('original',img)
cv2.imshow('threshold',threshold)
cv2.imshow('Gaussian',gaus)
cv2.imshow('threshold2',threshold2)
cv2.waitKey(0)
cv2.destroyAllWindows()

retval3,otsu = cv2.threshold(grayscaled,125,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# Otsu threshold. Not good for this example.

# +
cv2.imshow('original',img)
cv2.imshow('threshold',threshold)
cv2.imshow('threshold2',threshold2)
cv2.imshow('Gaussian',gaus)
cv2.imshow('Otsu', otsu)

cv2.waitKey(0)
cv2.destroyAllWindows()

StarcoderdataJupyterScriptsDedupFiltered