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()
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.
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()
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:
其中g表示图像像素灰度值,灰度值范围
,其中L=256表示256个灰度级别。P(g)表示图像直方图统计概率百分比。下标:f表示前景,b表示背景。计算得到新的阈值:
计算两个阈值之间的差值:
Reference and acknowledgment:
https://blog.csdn.net/qq_30490125/article/details/80458500
https://www.cnblogs.com/Undo-self-blog/p/8449393.html