直方图技术求阈值

#直方图技术
import sys,cv2
import numpy as np
import matplotlib.pyplot as plt
def calcGrayHist(image):
    rows,cols = image.shape
    grayHist = np.zeros([256],np.uint64)
    for r in range(rows):
        for c in range(cols):
            grayHist[image[r][c]] +=1#把图像灰度值作为索引
    return(grayHist)
def threshTwoPeaks(image):
    #计算灰度直方图
    histogram = calcGrayHist(image)
    x_range = range(256)
    plt.plot(x_range,histogram,'r',linewidth = 2,c='black')
    #设置坐标轴范围
    y_maxValue = np.max(histogram)
    plt.axis([0,255,0,y_maxValue])
    #设置坐标轴标签
    plt.xlabel('gray Level')
    plt.ylabel('number of pixels')
    plt.show()
    print(histogram)
    print(histogram.shape)
    
    #直方图最大峰值对应的灰度值
    maxLoc = np.where(histogram==np.max(histogram))
    print(maxLoc)
    firstPeak = maxLoc[0][0]
    print(firstPeak)
    #第二个峰值对应的灰度直方图
    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))
    print(measureDists)
    print(maxLoc2)
    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
    print(temp)
    print(minLoc)
    threshImage_out = image.copy()
    threshImage_out[threshImage_out>thresh]=255
    threshImage_out[threshImage_out<=thresh]=0
    return(thresh,threshImage_out)
if __name__ =='__main__':
    src = cv2.imread('E:/sy2/6/img7.jpg',cv2.IMREAD_GRAYSCALE)   
    re,ra = threshTwoPeaks(src)
    print(re)
    print(ra)
    cv2.imshow('ra',ra)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

结果

[   0    0    0    0    0    0    1    2    2    0    2    1    6    6
    1   11   12   12    5    7    5   12    9   10    6   13   11   10
   12   11   11    8   16   17   27   21   29   34   32   42   47   43
   57   68   68   82   71   96  100  101  133  154  179  222  208  220
  271  288  324  395  403  491  534  611  639  703  800  901  995 1011
 1120 1290 1418 1507 1634 1732 1853 2017 2196 2241 2408 2517 2769 2873
 3139 3288 3417 3535 3705 3951 4030 4148 4148 4127 4147 3996 3919 3826
 3673 3445 3093 2855 2627 2302 1873 1751 1509 1365 1182 1009  879  787
  679  591  499  478  429  376  349  349  266  261  265  254  191  220
  197  193  182  146  177  177  180  144  155  134  155  144  146  138
  121  154  137  136  147  147  138  125  141  145  141  159  140  134
  144  145  158  164  153  136  152  170  155  163  184  188  169  149
  172  199  202  167  205  195  225  204  217  241  249  249  234  241
  258  226  245  280  264  256  261  260  281  322  330  329  312  365
  363  357  351  377  349  376  304  358  355  416  387  398  388  398
  440  474  439  479  504  501  478  561  540  550  483  536  528  524
  571  512  492  433  446  369  388  372  379  344  338  353  321  407
  388  424  381  334  256  349  396  378  266  152   79   28   20   11
    4    5    5    6]
(256,)
(array([91, 92], dtype=int64),)
91
[0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00
 0.0000000e+00 7.2250000e+03 1.4112000e+04 1.3778000e+04 0.0000000e+00
 1.3122000e+04 6.4000000e+03 3.7446000e+04 3.6504000e+04 5.9290000e+03
 6.3536000e+04 6.7500000e+04 6.5712000e+04 2.6645000e+04 3.6288000e+04
 2.5205000e+04 5.8800000e+04 4.2849000e+04 4.6240000e+04 2.6934000e+04
 5.6628000e+04 4.6475000e+04 4.0960000e+04 4.7628000e+04 4.2284000e+04
 4.0931000e+04 2.8800000e+04 5.5696000e+04 5.7188000e+04 8.7723000e+04
 6.5856000e+04 8.7725000e+04 9.9144000e+04 8.9888000e+04 1.1356800e+05
 1.2224700e+05 1.0750000e+05 1.3685700e+05 1.5667200e+05 1.5021200e+05
 1.7351200e+05 1.4377500e+05 1.8585600e+05 1.8490000e+05 1.7816400e+05
 2.2357300e+05 2.4640000e+05 2.7225900e+05 3.2056800e+05 2.8475200e+05
 2.8512000e+05 3.3197500e+05 3.3292800e+05 3.5283600e+05 4.0448000e+05
 3.8728300e+05 4.4190000e+05 4.4909400e+05 4.7902400e+05 4.6583100e+05
 4.7522800e+05 5.0000000e+05 5.1897600e+05 5.2635500e+05 4.8932400e+05
 4.9392000e+05 5.1600000e+05 5.1189800e+05 4.8826800e+05 4.7222600e+05
 4.4339200e+05 4.1692500e+05 3.9533200e+05 3.7112400e+05 3.2270400e+05
 2.9136800e+05 2.5170000e+05 2.2428900e+05 1.8387200e+05 1.5381100e+05
 1.1836800e+05 8.5425000e+04 5.6560000e+04 3.3345000e+04 1.5804000e+04
 4.0300000e+03 0.0000000e+00 4.1480000e+03 1.6508000e+04 3.7323000e+04
 6.3936000e+04 9.7975000e+04 1.3773600e+05 1.7997700e+05 2.2048000e+05
 2.5053300e+05 2.8550000e+05 3.1786700e+05 3.3148800e+05 3.1653700e+05
 3.4319600e+05 3.3952500e+05 3.4944000e+05 3.4159800e+05 3.2691600e+05
 3.1731900e+05 3.1480000e+05 2.9943900e+05 2.8604400e+05 2.6397100e+05
 2.7532800e+05 2.6812500e+05 2.5417600e+05 2.5442100e+05 2.7361600e+05
 2.2370600e+05 2.3490000e+05 2.5466500e+05 2.6009600e+05 2.0799900e+05
 2.5432000e+05 2.4132500e+05 2.5012800e+05 2.4915800e+05 2.1082400e+05
 2.6921700e+05 2.8320000e+05 3.0258000e+05 2.5401600e+05 2.8659500e+05
 2.5942400e+05 3.1387500e+05 3.0470400e+05 3.2251400e+05 3.1795200e+05
 2.9052100e+05 3.8500000e+05 3.5633700e+05 3.6774400e+05 4.1292300e+05
 4.2865200e+05 4.1745000e+05 3.9200000e+05 4.5810900e+05 4.8778000e+05
 4.9082100e+05 5.7240000e+05 5.2094000e+05 5.1509600e+05 5.7153600e+05
 5.9392000e+05 6.6755000e+05 7.1438400e+05 6.8681700e+05 6.2886400e+05
 7.2367200e+05 8.3300000e+05 7.8135500e+05 8.4499200e+05 9.8053600e+05
 1.0294880e+06 9.5062500e+05 8.6062400e+05 1.0197880e+06 1.2107160e+06
 1.2606820e+06 1.0688000e+06 1.3450050e+06 1.3111800e+06 1.5500250e+06
 1.4394240e+06 1.5678250e+06 1.7824360e+06 1.8846810e+06 1.9282560e+06
 1.8535140e+06 1.9521000e+06 2.1364980e+06 1.9128640e+06 2.1190050e+06
 2.4740800e+06 2.3826000e+06 2.3592960e+06 2.4557490e+06 2.4970400e+06
 2.7540810e+06 3.2200000e+06 3.3663300e+06 3.4229160e+06 3.3100080e+06
 3.9478400e+06 4.0020750e+06 4.0112520e+06 4.0185990e+06 4.3973280e+06
 4.1464690e+06 4.5496000e+06 3.7455840e+06 4.4907520e+06 4.5329950e+06
 5.4063360e+06 5.1180750e+06 5.3554880e+06 5.3113320e+06 5.5417520e+06
 6.2308400e+06 6.8256000e+06 6.4273990e+06 7.1294360e+06 7.6250160e+06
 7.7033760e+06 7.4687500e+06 8.9064360e+06 8.7096600e+06 9.0112000e+06
 8.0376030e+06 9.0584000e+06 9.0610080e+06 9.1301760e+06 1.0100419e+07
 9.1934720e+06 8.9667000e+06 8.0087680e+06 8.3709740e+06 7.0272360e+06
 7.4965480e+06 7.2912000e+06 7.5348990e+06 6.9364160e+06 6.9117620e+06
 7.3198080e+06 6.7490250e+06 8.6756120e+06 8.3842920e+06 9.2872960e+06
 8.4585810e+06 7.5150000e+06 5.8370560e+06 8.0632960e+06 9.2699640e+06
 8.9646480e+06 6.3906500e+06 3.6990720e+06 1.9472710e+06 6.9899200e+05
 5.0562000e+05 2.8160000e+05 1.0368400e+05 1.3122000e+05 1.3284500e+05
 1.6137600e+05]
(array([224], dtype=int64),)
[4148 4148 4127 4147 3996 3919 3826 3673 3445 3093 2855 2627 2302 1873
 1751 1509 1365 1182 1009  879  787  679  591  499  478  429  376  349
  349  266  261  265  254  191  220  197  193  182  146  177  177  180
  144  155  134  155  144  146  138  121  154  137  136  147  147  138
  125  141  145  141  159  140  134  144  145  158  164  153  136  152
  170  155  163  184  188  169  149  172  199  202  167  205  195  225
  204  217  241  249  249  234  241  258  226  245  280  264  256  261
  260  281  322  330  329  312  365  363  357  351  377  349  376  304
  358  355  416  387  398  388  398  440  474  439  479  504  501  478
  561  540  550  483  536  528  524]
(array([49], dtype=int64),)
141
[[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值