图像像素灰度处理
1.计算某行相邻像素的灰度变化率
a=img[:,100]
def normalization(value):
# """标准化
# 公式:(原始值-均值)/方差
# :return 范围任意,标准化后数据均值为0,标准差为1
# """
new_value = (value - value.mean()) / value.std()
return new_value
b=normalization(a)
#画出灰度曲线
import matplotlib.pyplot as plt
values = range(0,len(a),1)
gray = a
plt.plot(values, gray, linewidth=2)
plt.title("gray Number",fontsize=14)
plt.xlabel("Value", fontsize=14)
plt.ylabel("gray of Value", fontsize=14)
# plt.tick_params(axis='x', labelsize=10)
# plt.axis([0, 6, 0, 30])
plt.show()
#计算一阶微分
import matplotlib.pyplot as plt
list_with_diff = []
for n in range(1, len(lista)):
list_with_diff.append(lista[n] - lista[n-1])
# print("Difference between adjacent elements in the list: \n",
# list_with_diff)
#计算一阶微分变化率
import matplotlib.pyplot as plt
list_with_diff = []
for n in range(1, len(lista)):
list_with_diff.append(abs((lista[n] - lista[n-1])/lista[n] ))
# print("Difference between adjacent elements in the list: \n",
# list_with_diff)
#画出一阶微分曲线
c = list_with_diff
import matplotlib.pyplot as plt
values = range(0,len(c),1)
gray = c
plt.plot(values, gray, linewidth=2)
plt.title("gray Number",fontsize=14)
plt.xlabel("Value", fontsize=14)
plt.ylabel("gray of Value", fontsize=14)
# plt.tick_params(axis='x', labelsize=10)
# plt.axis([0, 6, 0, 30])
plt.show()
计算峰值
from scipy.signal import find_peaks
# x is the vector from which you want to extract the peaks
c = np.array(c)
peaks, _ = find_peaks(c,height=0.05)
plt.plot(c)
plt.plot(peaks, c[peaks], "x")