1 Star 1 Fork 0

Haixu He/波段分布变化检测

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
improve_OTSU.py 5.12 KB
一键复制 编辑 原始数据 按行查看 历史
Haixu He 提交于 2022-05-19 21:30 . update
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Author :hhx
@Date :2022/5/13 12:30
@Description :改进的OTSU
"""
import numpy as np
from matplotlib import pyplot as plt
import itertools
import cv2
from scipy import stats
from multiprocessing import Pool
from tqdm import tqdm
# pre=[75.799, 84.475, 84.247,84.932,84.475, 84.703,
# 80.822 ,81.050, 82.420,85.388]
# plt.plot(pre)
# plt.show()
def normalize(data):
min = np.min(data)
max = np.max(data)
res = (data - min) / (max - min)
return res, max, min
def OTSU(result):
max_g = 0
suitable_th = 0
numbermax = max(result)
numbermin = min(result)
spac = (numbermax - numbermin) / 255
number = result.shape[0]
for threshold in tqdm(range(numbermin, numbermax + 1)):
bin_img = result > threshold
bin_img_inv = result <= threshold
fore_pix = np.sum(bin_img)
back_pix = np.sum(bin_img_inv)
if 0 == fore_pix:
continue
if 0 == back_pix:
continue
w0 = float(fore_pix) / number # p1t
u0 = float(np.sum(result * bin_img)) / fore_pix
w1 = float(back_pix) / number # p0t
u1 = float(np.sum(result * bin_img_inv)) / back_pix
prenumber = np.where((result > threshold - 1 * spac) & (result < threshold + 1 * spac))[0].shape[0]
ptb = prenumber / number
w = 1 - ptb
if w1 > 0.4:
g = w1 * w1 * u1 * u1 + w0 * u0 * u0
else:
g = w1 * u1 * u1 + w1 * w0 * u0 * u0
if g * w > max_g:
max_g = g * w
suitable_th = threshold
return suitable_th
def BUOTSU(result):
max_g = 0
suitable_th = 0
numbermax = np.max(result)
numbermin = np.min(result)
spac = (numbermax - numbermin) / 255
number = result.shape[0]
for threshold in tqdm(range(numbermin, numbermax + 1)):
bin_img = result > threshold
bin_img_inv = result <= threshold
fore_pix = np.sum(bin_img)
back_pix = np.sum(bin_img_inv)
if 0 == fore_pix:
continue
if 0 == back_pix:
continue
w0 = float(fore_pix) / number # p1t
u0 = float(np.sum(result * bin_img)) / fore_pix
w1 = float(back_pix) / number # p0t
u1 = float(np.sum(result * bin_img_inv)) / back_pix
prenumber = np.where((result > threshold - 1 * spac) & (result < threshold + 1 * spac))[0].shape[0]
ptb = prenumber / number
w = 1 - ptb
if w1 > 0.35:
g = w1 * w1 * u1 * u1 + w0 * u0 * u0
else:
g = w1 * u1 * u1 + w1 * w0 * u0 * u0
if g * w > max_g:
max_g = g * w
suitable_th = threshold
return suitable_th
def oldOTSU(result):
max_g = 0
suitable_th = 0
numbermax = max(result)
numbermin = min(result)
for threshold in range(numbermin, numbermax + 1):
bin_img = result > threshold
bin_img_inv = result <= threshold
fore_pix = np.sum(bin_img)
back_pix = np.sum(bin_img_inv)
if 0 == fore_pix:
break
if 0 == back_pix:
continue
w0 = float(fore_pix) / len(result) # p1t
u0 = float(np.sum(result * bin_img)) / fore_pix
w1 = float(back_pix) / len(result) # p0t
u1 = float(np.sum(result * bin_img_inv)) / back_pix
g = w1 * w0 * (u0 - u1) * (u0 - u1)
if g > max_g:
max_g = g
suitable_th = threshold
return suitable_th
def test():
img = cv2.imread('ttt.jpg', 0)
result = np.array(img).ravel().astype(np.uint8)
number = OTSU(result)
number1 = oldOTSU(result)
plt.hist(x = result, # 指定绘图数据
bins = 20, # 指定直方图中条块的个数
color = 'steelblue', # 指定直方图的填充色
edgecolor = 'black' # 指定直方图的边框色
)
# plt.xlabel('差值')
# plt.ylabel('数量')
# # 添加标题
# plt.title('分布')
# number1=nthreshSingleJob(result)
# plt.scatter(range(len(result)), result, s=1)
# # plt.axhline(number, color='r', linestyle='--', label='xxx')
# plt.axhline(number1, color='b', linestyle='--', label='xxx')
plt.show()
print("----number----", number)
print("----number1----", number1)
if __name__ == '__main__':
# test()
data1 = np.load('result/index_npy/2021-07_index2.npy')
data = data1.ravel()
id = np.where(data.astype('str') == 'nan')
data = np.delete(data, id)
data, max, min = normalize(data)
data = data * 1000
data = data.astype('int')
th = BUOTSU(data)
print(th)
th = (th / 1000) * (max - min) + min
print(th)
plt.hist(x=data, # 指定绘图数据
bins=100, # 指定直方图中条块的个数
color='steelblue', # 指定直方图的填充色
edgecolor='black' # 指定直方图的边框色
)
plt.show()
fig = plt.figure(figsize=(8, 8))
ax = plt.subplot(121)
cax = ax.matshow(data1, cmap='coolwarm')
fig.colorbar(cax)
ax = plt.subplot(122)
cax = ax.matshow(data1 > th, cmap='coolwarm')
print(cax)
fig.colorbar(cax)
plt.show()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/HaixuHe/Band-distribution-change-detection.git
git@gitee.com:HaixuHe/Band-distribution-change-detection.git
HaixuHe
Band-distribution-change-detection
波段分布变化检测
master

搜索帮助