import numpy as np
import cv2
#读入图像
img = cv2.imread('dataset/mask/00004.png')
img = cv2.resize(img, (128, 128))
#灰度化
GrayImage=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#二值化
ret, thresh2=cv2.threshold(GrayImage,127,255,cv2.THRESH_BINARY_INV)
img = np.array(thresh2)
x, y = img.shape #x:高;y:宽
w = 0
b = 0
for row in range(x):
for col in range(y):
if (img[row][col]) == 0:
w = w+1
else:
b = b+1
rate1 = w/(x*y)
rate2 = b/(x*y)
print("白色占比:", round(rate1*100,2),'%')
print("黑色占比:", round(rate2*100,2),'%')