代码拉取完成,页面将自动刷新
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Author :hhx
@Date :2022/5/13 9:53
@Description :
"""
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
import os
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False
# 将图片转为灰度图
# https://blog.csdn.net/weixin_43635647/article/details/99625105
# img = cv.imread('test.png', 0)
# cv.imshow("img", img)
# cv.waitKey()
def normalize(data):
min = np.min(data)
max = np.max(data)
res = (data - min) / (max - min)
return res, max, min
def OTSU(img_gray, GrayScale):
u1 = 0.0 # 背景像素的平均灰度值
u2 = 0.0 # 前景像素的平均灰度值
th = 0.0
# 总的像素数目
PixSum = img_gray.size
# 各个灰度值的像素数目
PixCount = np.zeros(GrayScale)
# 各灰度值所占总像素数的比例
PixRate = np.zeros(GrayScale)
# 统计各个灰度值的像素个数
for i in range(PixSum):
# 默认灰度图像的像素值范围为GrayScale
Pixvalue = img_gray[i]
PixCount[int(Pixvalue)] = PixCount[int(Pixvalue)] + 1
# 确定各个灰度值对应的像素点的个数在所有的像素点中的比例。
for j in range(GrayScale):
PixRate[j] = PixCount[j] * 1.0 / PixSum
Max_var = 0
# 确定最大类间方差对应的阈值
for i in range(1, GrayScale): # 从1开始是为了避免w1为0.
u1_tem = 0.0
u2_tem = 0.0
# 背景像素的比列
w1 = np.sum(PixRate[:i])
# 前景像素的比例
w2 = 1.0 - w1
if w1 == 0 or w2 == 0:
pass
else: # 背景像素的平均灰度值
for m in range(i):
u1_tem = u1_tem + PixRate[m] * m
u1 = u1_tem * 1.0 / w1
# 前景像素的平均灰度值
for n in range(i, GrayScale):
u2_tem = u2_tem + PixRate[n] * n
u2 = u2_tem / w2
# print(u1)
# 类间方差公式:G=w1*w2*(u1-u2)**2
tem_var = w1 * w2 * np.power((u1 - u2), 2)
# print(tem_var)
# 判断当前类间方差是否为最大值。
if Max_var < tem_var:
Max_var = tem_var # 深拷贝,Max_var与tem_var占用不同的内存空间。
th = i
return th
def preprocess_nor(data1):
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')
return data, max, min
def preprocess_div(data1):
data = data1.ravel()
id = np.where((data.astype('str') == 'nan'))
data = np.delete(data, id)
id1 = np.where((data > 0))[0].shape[0]
id2 = np.where((data < 0))[0].shape[0]
print(id1, id2)
print(data.shape)
data = data * 1000
min = np.min(data)
data = data - min
data = data.astype('int')
return data, np.max(data), min
if __name__ == '__main__':
data_1 = np.load('result/index_npy/2021-04_BLI.npy')
data_2 = np.load('result/index_npy/2021-04_index2.npy')
#
# data_1 = np.load('result/index_npy/2016-12_BLI.npy')
# data_2 = np.load('result/index_npy/2017-05_BLI.npy')
# #
# data_1 = np.load('result/index_npy/2021-07_index2.npy')
# data_2 = np.load('result/index_npy/2021-12_index2.npy')
choose = 1 # 0代表使用Maxmin归一化
if choose == 0:
data1, max1, min1 = preprocess_nor(data_1)
data2, max2, min2 = preprocess_nor(data_2)
else:
data1, max1, min1 = preprocess_div(data_1)
data2, max2, min2 = preprocess_div(data_2)
print(data1)
print(data2)
if choose == 0:
th_1 = (OTSU(data1, 1001))
th_2 = (OTSU(data2, 1001))
else:
th_1 = (OTSU(data1, max1 + 1))
th_2 = (OTSU(data2, max2 + 1))
if choose == 0:
th1 = (th_1 / 1000) * (max1 - min1) + min1
th2 = (th_2 / 1000) * (max2 - min2) + min2
else:
th1 = (th_1 + min1) / 1000
th2 = (th_2 + min2) / 1000
print('阈值1为', th_1, th1, -min1)
print('阈值2为', th_2, th2, -min2)
fig = plt.figure(figsize=(8, 8))
ax = plt.subplot(321)
n1, bins1, patches1 = plt.hist(data1, bins=100, density=True, color='g', alpha=1)
plt.axvline(th_1)
plt.axvline(-min1, color='r')
ax = plt.subplot(323)
cax = ax.matshow(data_1, cmap='coolwarm', vmin=-0.5, vmax=0.5)
fig.colorbar(cax)
ax = plt.subplot(325)
cax = ax.matshow(data_1 > th1, cmap='coolwarm')
fig.colorbar(cax)
ax = plt.subplot(322)
n1, bins1, patches1 = plt.hist(data2, bins=100, density=True, color='g', alpha=1)
plt.axvline(th_2)
plt.axvline(-min2, color='r')
ax = plt.subplot(324)
cax = ax.matshow(data_2, cmap='coolwarm', vmin=-0.5, vmax=0.5)
fig.colorbar(cax)
ax = plt.subplot(326)
cax = ax.matshow(data_2 > th2, cmap='coolwarm')
fig.colorbar(cax)
plt.tight_layout()
plt.show()
# fig = plt.figure(figsize=(8, 8))
# ax = plt.subplot(111)
# cax = ax.matshow(data_2, cmap='coolwarm', vmin=-0.5, vmax=0.5)
# for key, spine in ax.spines.items():
# # 'left', 'right', 'bottom', 'top'
# if key == 'right' or key == 'top' or key == 'bottom' or key == 'left':
# spine.set_visible(False)
# # fig.colorbar(cax)
# plt.xticks([])
# plt.yticks([])
# plt.tight_layout()
# plt.savefig('test.jpg')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。