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
def OTSU(image
):
    
    if len(image
.shape
) == 2:
        gray 
= image
    
else:
        gray 
= cv
.cvtColor
(image
, cv
.COLOR_BGR2GRAY
)
    rows
,cols 
= gray
.shape
    
    grayHist 
= calcGrayHist
(gray
)
    
    uniformGrayHist 
= grayHist
/float(rows
*cols
)
    
    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
()
    
    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
()
    
    threshImage_out
[threshImage_out 
> thresh
] = 255
    
    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)
 
结果如下: 原图:  自适应阈值分割后的二值图:  可见,直方图阈值分割计数法能够较为有效的将背景何前景区分开来,比较完整的分割出图片中的目标物体。值得一提的是,对于任何一张图像,它的直方图中如果存在较为明显的双峰,用直方图分割技术法可以达到很好的效果,否则,达到的效果会很不理想.
                
                
                
        
    
 
转载请注明原文地址:https://ipadbbs.8miu.com/read-60134.html