直接上代码
前言不算,案例示范可行。

# #!/usr/bin/env python
# # -*- coding:utf-8 -*-
# # Author's_name_is_NIKOLA_SS
# #pip install   -i https://pypi.mirrors.ustc.edu.cn/simple/
# #matplotlib.rcParams['font.family'] = 'SimHei'  # 用来正常显示中文
# #plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

# ! /usr/bin/env python
# -*- coding: utf-8 -*-
# import cv2
# import numpy as np
# from PIL import Image
#
# area = 0
#
#
# def ostu(img):
#     global area
#     image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # 转灰度
#     blur = cv2.GaussianBlur( image, (5, 5), 0 )  # 阈值一定要设为 0 !高斯模糊
#     ret3, th3 = cv2.threshold( blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU )  # 二值化 0 = black ; 1 = white
#     # cv2.imshow('image', th3)
#     # a = cv2.waitKey(0)
#     # print a
#     height, width = th3.shape
#     for i in range( height ):
#         for j in range( width ):
#             if th3[i, j] == 255:
#                 area += 1
#     return area
#
# img=cv2.imread(r"hp.jpg")
#
# ostu(img)

# 全局阈值
# def threshold_demo(image):
#     gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#     # ret, binary = cv2.threshold(gray,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
#     # ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_TRIANGLE)
#     # ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_TRUNC)
#     ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
#     print("阈值:", ret)
#     cv2.imshow("binary", binary)
#
# # 局部阈值
# def local_threshold(image):
#     gray = cv2.cvtColor(image,cv2.COLOR_BGRA2GRAY)
#     # binary = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,25,10)
#     binary = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 25, 10)
#     cv2.imshow("binary ", binary)
#
# def custom_threshold(image):
#     gray = cv2.cvtColor(image,cv2.COLOR_BGRA2GRAY)
#     h, w = gray.shape[:2]
#     m = np.reshape(gray, [1, w*h])
#     mean = m.sum()/(w*h)
#     # binary = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,25,10)
#     ret, binary = cv2.threshold(gray, mean, 255, cv2.THRESH_BINARY)
#     cv2.imshow("binary ", binary)
#
# def custom_Threshold(input_img_file):
#     image = cv2.imread(input_img_file)
#     cv2.imshow("image", image)  # 显示二值化图像
#     gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#     h, w = gray.shape[:2]
#     m = np.reshape(gray, [1, h*w]) #将图像转为1行h*w列
#     mean = m.sum() / (h*w)  #计算图像的均值,用均值作为阈值,来分割图像
#     ret, binary = cv2.threshold(gray, mean, 255, cv2.THRESH_BINARY)
#     print("threshold value %s" % ret)
#     cv2.imshow("cudtom_binary", binary)
#     cv2.waitKey(0)
#     cv2.destroyAllWindows()

重点附上:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author's_name_is_NIKOLA_SS

import cv2
import numpy as np

if __name__ == "__main__":
    img = cv2.imread(r"W:\PY\PYDD\ffgg\335.jpg")#读取文件图片
    cv2.namedWindow("input image", cv2.WINDOW_AUTOSIZE)
    cv2.imshow("input image", img)
    # local_threshold(img)

    area0=0
    area1=0
    size01=0
    image = cv2.cvtColor( img, cv2.COLOR_BGR2GRAY )  # 转灰度
    blur = cv2.GaussianBlur( image, (5, 5), 0 )  # 阈值一定要设为 0 !高斯模糊
    ret3, th3 = cv2.threshold( blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU )  # 二值化 0 = black ; 1 = white
    cv2.imshow('image', th3)
    # a = cv2.waitKey(0)
    # print a
    height, width = th3.shape
    for i in range( height ):
        for j in range( width ):
            if th3[i, j] == 0:
                area0 += 1
            else:
                area1 +=1

    print("白色像素点的数量统计",area0)
    print("黑色像素点的数量统计",area1)
    size01=height*width
    print("图像长度",height)
    print("图像宽度",width)
    print("图像总像素点个数-图像大小",size01)

    # dds0=area0/(size01)
    # dds1=area1/(size01)
    #
    # print("白色像素点的比例统计",dds0)
    # print("黑色像素点的比例统计",dds1)

    cv2.waitKey( 0 )
    cv2.destroyAllWindows()

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐