标签:通过 png 中值滤波 license 因此 i+1 平均值 muti dash
在图像处理中,Thresholding中文翻译过来叫二值化或者阈值化。二值化就是把图片传换成只有white和black这两种颜色。通过Thresholding,可以让图片中感兴趣的颜色变成主角--white,其余的颜色全部隐藏--black。另外,二值化后的图片也便于计算机进行分析,因为边缘轮廓十分清晰,所以计算机可以轻松找到边界线。然而,在找边界这方面,Thresholding并不是特别好的算法,有些时候遇到某些特殊图片效果也不好。
#导入工具包
from imutils import *
image = imread(‘image/coins.jpg‘) show(image)
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) plt.imshow(gray,‘gray‘) plt.axis(‘off‘) plt.show()
ret1,thresh1 = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) ret2,thresh2 = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV) ret3,thresh3 = cv2.threshold(gray,127,255,cv2.THRESH_TRUNC) ret4,thresh4 = cv2.threshold(gray,127,255,cv2.THRESH_TOZERO) ret5,thresh5 = cv2.threshold(gray,127,255,cv2.THRESH_TOZERO_INV) titles = [‘original‘,‘BINARY‘,‘BINARY_INV‘,‘TRUNC‘,‘TOZERO‘,‘TOZERO_INV‘] images = [gray, thresh1, thresh2, thresh3, thresh4, thresh5] plt.figure(figsize=(15,5)) for i in range(6): plt.subplot(2,3,i+1) plt.imshow(images[i],‘gray‘) plt.title(titles[i]) plt.axis(‘off‘) plt.show()
show(cv2.bitwise_and(image, image, mask=thresh1))
# Otsu’s Method
自动选择阈值
image = imread(‘image/opencv_logo.png‘) show(image)
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) plt.imshow(gray,‘gray‘) plt.axis(‘off‘) plt.show()
ret1,thresh1 = cv2.threshold(gray,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU) ret2,thresh2 = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU) print(‘ret1:‘,ret1) print(‘ret2:‘,ret2)
ret1: 164.0 ret2: 164.0
plt.imshow(thresh1,‘gray‘) plt.axis(‘off‘) plt.show()
plt.imshow(thresh2,‘gray‘) plt.axis(‘off‘) plt.show()
image = imread(‘image/license_plate.png‘) show(image)
# 变灰度图 image = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY) # 中值滤波 image = cv2.medianBlur(image,5) # 普通二值化 ret,th1 = cv2.threshold(image,127,255,cv2.THRESH_BINARY) # 平均值阈值 th2 = cv2.adaptiveThreshold(image,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,3) # 高斯阈值 th3 = cv2.adaptiveThreshold(image,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,3) titles = [‘original‘, ‘Global Thresholding‘, ‘Adaptive Mean Thresholding‘, ‘Adaptive Gaussian Thresholding‘] images = [image, th1, th2, th3] plt.figure(figsize=(10,5)) for i in range(4): plt.subplot(2,2,i+1) plt.imshow(images[i],‘gray‘) plt.axis(‘off‘) plt.title(titles[i]) plt.show()
image = imread(‘image/sudoku.jpg‘) show(image)
# 变灰度图 image = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY) # 中值滤波 image = cv2.medianBlur(image,5) # 普通二值化 ret,th1 = cv2.threshold(image,127,255,cv2.THRESH_BINARY) # 平均值阈值 th2 = cv2.adaptiveThreshold(image,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,3) # 高斯阈值 th3 = cv2.adaptiveThreshold(image,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,3) titles = [‘original‘, ‘Global Thresholding‘, ‘Adaptive Mean Thresholding‘, ‘Adaptive Gaussian Thresholding‘] images = [image, th1, th2, th3] plt.figure(figsize=(10,5)) for i in range(4): plt.subplot(2,2,i+1) plt.imshow(images[i],‘gray‘) plt.axis(‘off‘) plt.title(titles[i]) plt.show()
标签:通过 png 中值滤波 license 因此 i+1 平均值 muti dash
原文地址:https://www.cnblogs.com/liuwenhua/p/11565407.html