OpenCV - 图片二值化,计算白色像素点的个数
直接上代码吧:
import cv2 import numpy as np from PIL import Image area = 0 def getWhitePixel(img): global area image=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(image,(5,5),0) ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) height, width = th3.shape for i in range(height): for j in range(width): if th3[i, j] == 255: area += 1 return area