学习中......
1.绘制原图像的直方图
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
ricea=cv.imread("./lenargb1.jpg")
# print(ricea)
width=ricea.shape[1]
height=ricea.shape[0]
hist1=cv.calcHist([ricea],[0],None,[256],[0,256])
plt.title("Histogram");plt.plot(hist1, color="g")#曲线直方图
结果图1
2. 根据直方图,选择合适的阈值进行二值化处理得到二值化图像
new_img = cv.cvtColor(ricea, cv.COLOR_BGR2GRAY)
height, width = new_img.shape[0:2]
# 设置阈值
thresh = 80
# 遍历每一个像素点
for row in range(height):
for col in range(width):
# 获取到灰度值
gray = new_img[row, col]
# 如果灰度值高于阈值 就等于255