import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
#计算灰度直方图
def calcGrayHist(grayimage):
#灰度图像矩阵的高,宽
rows, cols = grayimage.shape
print(grayimage.shape)
#存储灰度直方图
grayHist = np.zeros([256],np.uint64)
for r in range(rows):
for c in range(cols):
grayHist[grayimage[r][c]] += 1
return grayHist
#OTSU自动阈值分割
def OTSU(image):
if len(image.shape) == 2:
gray = image
else:
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
rows,cols = gray.shape
#1.计算灰度直方图
grayHist = calcGrayHist(gray)
#2.灰度直方图归一化
uniformGrayHist = grayHist/float(rows*cols)
#3.计算零阶累计矩何一阶累计矩
zeroCumuMoment = np.zeros([256],np.float32)
oneCumuMoment = np.zeros([256],np.float32)
for k in range(256):
if k == 0:
zeroCumuMoment[k] = uniformGrayHist[0]
oneCumuMoment[k] = (k)*uniformGrayHist[0]
else:
zeroCumuMoment[k] = zeroCumuMoment[k-1] + uniformGrayHist[k]
oneCumuMoment[k] = oneCumuMoment[k-1] + k*uniformGrayHist[k]
#计算类间方差
variance = np.zeros([256],np.float32)
for k in range(255):
if zeroCumuMoment[k] == 0 or zeroCumuMoment[k] == 1:
variance[k] = 0
else:
variance[k] = math.pow(oneCumuMoment[255]*zeroCumuMoment[k] -
oneCumuMoment[k],2)/(zeroCumuMoment[k]*(1.0-zeroCumuMoment[k]))
#找到阈值、
threshLoc = np.where(variance[0:255] == np.max(variance[0:255]))
thresh = threshLoc[0][0]
#阈值处理
threshold = np.copy(gray)
threshold[threshold > thresh] = 255
threshold[threshold <= thresh] = 0
return threshold, thresh
#阈值分割:直方图技术法
def threshTwoPeaks(image):
if len(image.shape) == 2:
gray = image
else:
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
print(666666)
#计算灰度直方图
histogram = calcGrayHist(gray)
#寻找灰度直方图的最大峰值对应的灰度值
maxLoc = np.where(histogram==np.max(histogram))
firstPeak = maxLoc[0][0]
#寻找灰度直方图的第二个峰值对应的灰度值
measureDists = np.zeros([256],np.float32)
for k in range(256):
measureDists[k] = pow(k-firstPeak,2)*histogram[k]
maxLoc2 = np.where(measureDists==np.max(measureDists))
secondPeak = maxLoc2[0][0]
#找到两个峰值之间的最小值对应的灰度值,作为阈值
thresh = 0
if firstPeak > secondPeak:#第一个峰值再第二个峰值的右侧
temp = histogram[int(secondPeak):int(firstPeak)]
minloc = np.where(temp == np.min(temp))
thresh = secondPeak + minloc[0][0] + 1
else:#第一个峰值再第二个峰值的左侧
temp = histogram[int(firstPeak):int(secondPeak)]
minloc = np.where(temp == np.min(temp))
thresh =firstPeak + minloc[0][0] + 1
#找到阈值之后进行阈值处理,得到二值图
threshImage_out = gray.copy()
#大于阈值的都设置为255
threshImage_out[threshImage_out > thresh] = 255
threshImage_out[threshImage_out <= thresh] = 0
return thresh, threshImage_out
def threshTwoPeaks(image):
if len(image.shape) == 2:
gray = image
else:
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
print(666666)
#计算灰度直方图
histogram = calcGrayHist(gray)
#寻找灰度直方图的最大峰值对应的灰度值
maxLoc = np.where(histogram==np.max(histogram))
firstPeak = maxLoc[0][0]
#寻找灰度直方图的第二个峰值对应的灰度值
measureDists = np.zeros([256],np.float32)
for k in range(256):
measureDists[k] = pow(k-firstPeak,2)*histogram[k]
maxLoc2 = np.where(measureDists==np.max(measureDists))
secondPeak = maxLoc2[0][0]
#找到两个峰值之间的最小值对应的灰度值,作为阈值
thresh = 0
if firstPeak > secondPeak:#第一个峰值再第二个峰值的右侧
temp = histogram[int(secondPeak):int(firstPeak)]
minloc = np.where(temp == np.min(temp))
thresh = secondPeak + minloc[0][0] + 1
else:#第一个峰值再第二个峰值的左侧
temp = histogram[int(firstPeak):int(secondPeak)]
minloc = np.where(temp == np.min(temp))
thresh =firstPeak + minloc[0][0] + 1
#找到阈值之后进行阈值处理,得到二值图
threshImage_out = gray.copy()
#大于阈值的都设置为255
threshImage_out[threshImage_out > thresh] = 255
#小于阈值的都设置为0
threshImage_out[threshImage_out <= thresh] = 0
return thresh, threshImage_out
if __name__ == "__main__":
img = cv.imread('./123.png')
kkk,kkkk = threshTwoPeaks(img)
print(kkk)
cv.imshow('66',kkkk)
cv.waitKey(0)
结果如下:
原图:
自适应阈值分割后的二值图:
可见,直方图阈值分割计数法能够较为有效的将背景何前景区分开来,比较完整的分割出图片中的目标物体。值得一提的是,对于任何一张图像,它的直方图中如果存在较为明显的双峰,用直方图分割技术法可以达到很好的效果,否则,达到的效果会很不理想.