In [1]:
import cv2

In [2]:
im = cv2.imread('data/src/lena_square_half.png')

In [3]:
th, im_th = cv2.threshold(im, 128, 255, cv2.THRESH_BINARY)

In [4]:
print(th)


128.0

In [5]:
cv2.imwrite('data/dst/opencv_th.jpg', im_th)


Out[5]:
True

In [6]:
th, im_th_tz = cv2.threshold(im, 128, 255, cv2.THRESH_TOZERO)

In [7]:
print(th)


128.0

In [8]:
cv2.imwrite('data/dst/opencv_th_tz.jpg', im_th_tz)


Out[8]:
True

In [9]:
# th, im_th_otsu = cv2.threshold(im, 128, 192, cv2.THRESH_OTSU)
# error: OpenCV(4.2.0) /tmp/opencv-20200105-17262-cwpzm4/opencv-4.2.0/modules/imgproc/src/thresh.cpp:1529: error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'threshold'

In [10]:
im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

In [11]:
th, im_gray_th_otsu = cv2.threshold(im_gray, 128, 192, cv2.THRESH_OTSU)

In [12]:
print(th)


117.0

In [13]:
cv2.imwrite('data/dst/opencv_th_otsu.jpg', im_gray_th_otsu)


Out[13]:
True