python如何提取指定范围灰度

我有一张灰度图片,比如说中间有个正方形,里面有不同的灰度值,比如说我想小于100范围的灰度,然后计算这个范围的值占这个方格的面积多少

import cv2
import numpy as np
fileName = '1.png'
print(fileName)
img = cv2.imread(fileName,0)
info = img.shape
height = info[0]
width = info[1]
dst = np.zeros((height, width, 3), np.uint8)
total = height*width
need = 0
for h in range(0, height):
for j in range(0, width):
print(img[h][j])
if int(img[h][j]<100):
need+=1
print("当前灰度小于100的像素有{}个,图片像素总数为{}".format(need,total))
print("当前灰度小于100的像素占面积比例为:{}".format(float(need/total)))
觉得好的话请采纳答案,感谢。修改上面的数字100为自己想要的数值就好,当然,自行去理解cv矩阵的原理会比较好一点。