Binarized image Python-OpenCV

Several ways binarization

import cv2
import numpy as np
import matplotlib.pylab  as plt

Original = cv2.imread('cat.png')
ret,thresh1 = cv2.threshold(Original,127,255,cv2.THRESH_BINARY)
ret,thresh2 = cv2.threshold(Original,127,255,cv2.THRESH_BINARY_INV)
ret,thresh3 = cv2.threshold(Original,127,255,cv2.THRESH_TRUNC)
ret,thresh4 = cv2.threshold(Original,127,255,cv2.THRESH_TOZERO)
ret,thresh5 = cv2.threshold(Original,127,255,cv2.THRESH_TOZERO_INV)

titles = ['Original','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [Original, thresh1, thresh2, thresh3, thresh4, thresh5]

for i in range(6):
    plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])


#===============================黑白图可视化=====================================
w,h = Original.shape[:2]
result0 = np.hstack((Original,thresh1,thresh2))
result1 = np.hstack((thresh3,thresh4,thresh5))
result = np.vstack((result0,result1))
font = cv2.FONT_HERSHEY_TRIPLEX
for i in range(len(titles)):
    if i < 3:
        result = cv2.putText(result, titles[i], (5+i*200, 180), font, 0.8, (0, 0, 0), 1)
    else:
        result = cv2.putText(result, titles[i], (5+(i-3)*200, 360), font, 0.8, (255, 255, 255), 1)

cv2.imshow('result',result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Here Insert Picture Description
Grayscale directly follows: are: the original grayscale image, binarization, binarization and inverted,
cv2.THRESH_TRUNC: grayscale pixels is smaller than the threshold value does not change, greater than a threshold pixel gray value it is set as the threshold.
cv2.THRESH_TOZERO: gray scale pixel value smaller than the threshold value without any changes, and part is greater than the threshold, all of which gradation value becomes zero.
cv2.THRESH_TOZERO_INV: gray value of the pixel is greater than a threshold value without any change, the gradation pixel is smaller than the threshold value, all of which gradation value becomes zero.
Here Insert Picture Description

HOG calculated before the second moment of pictures we use them deskew (deskew) process.

def deskew(img):
    m = cv2.moments(img)
    if abs(m['mu02']) < 1e-2:
        return img.copy()
    skew = m['mu11']/m['mu02']
    M = np.float32([[1, skew, -0.5*SZ*skew], [0, 1, 0]])
    img = cv2.warpAffine(img,M,(SZ, SZ),flags=affine_flags)
    return img

Finding the binary image OpenCV algorithms - OTSU

import cv2
import numpy as np


src = cv2.imread("./cat.png")

cv2.imshow("input", src)
h, w = src.shape[:2]

# 自动阈值分割 OTSU
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cv2.imshow("binary", binary)
cv2.waitKey(0)
cv2.destroyAllWindows()

Here Insert Picture Description
Riddler-Calvard algorithm
Riddler-Calvard thresholding algorithm is a binary histogram, OTSU Triangle are the same as obtained with the threshold value calculated based on the histogram segmentation algorithm based on binarization.

First, assume that the initial threshold value T such as T = 127, then two pixels obtained cluster divided. Calculate their mean values ​​as follows:
m b ( T n ) = g = 0 n g p ( g ) (1) m_b(T_n) = \sum_{g=0}^ngp(g)\tag{1}

m f ( T n ) = g = T n + 1 L 1 g p ( g ) (2) m_f(T_n) = \sum_{g=T_n+1}^{L-1}gp(g)\tag{2}

其中g表示图像像素灰度值,灰度值范围 g = 0 , 1 , 2 , 3 , , , , , L 1 g={0,1,2,3,,,,,L-1} ,其中L=256表示256个灰度级别。P(g)表示图像直方图统计概率百分比。下标:f表示前景,b表示背景。计算得到新的阈值:
T n + 1 = m b ( T n ) + m f ( T n ) 2 (3) T_{n+1} = \frac{m_b(T_n)+m_f(T_n)}{2}\tag{3}

计算两个阈值之间的差值:
d e l t a = T n T n + 1 (4) delta = |T_n - T_{n+1}|\tag{4}
Reference and acknowledgment:
https://blog.csdn.net/qq_30490125/article/details/80458500
https://www.cnblogs.com/Undo-self-blog/p/8449393.html

Published 386 original articles · won praise 592 · views 720 000 +

Guess you like

Origin blog.csdn.net/wsp_1138886114/article/details/82883432