使用python-opencv 统计二值图像中白色像素点的个数(表示面积)

import cv2

#method1
def area1(image):
    list=[]
    h,w=image.shape[0],image.shape[1]
    for i in range(h):
        for j in range(w):
                if image[i][j]!=0:
                    coor=(j,i)
                    list.append(coor)
    return len(list)

#method2
def area2(image):
    sum=0
    h,w=image.shape[0],image.shape[1]
    for i in range(h):
        for j in range(w):
                if image[i][j]==255:
                    sum+=1
    return sum

#method3
def area3(image):
    sum=0
    h,w=image.shape[0],image.shape[1]
    for i in range(h):
        for j in range(w):
                if image[i][j].all()>0:
                    sum+=1
    return sum

if __name__ == '__main__':
    #opencv读取图像时默认为三通道,只有在灰度处理后才会显示单通道
    image=cv2.imread('temb.png',-1)
    print('二值图像白色像素个数(表征面积):',area1(image))
    print('二值图像白色像素个数(表征面积):', area2(image))
    print('二值图像白色像素个数(表征面积):', area3(image))

你可能感兴趣的:(opencv_python,python)