幂率分布
概率函数
假设变量x服从参数为 $\alpha$的幂率分布,其概率密度函数可以表示为
通式
图像

灰度直方图
我们选用高对比度的图片high.png

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| from PIL import Image import numpy as np import matplotlib.pyplot as plt
image = Image.open('high.png')
gray_image = image.convert('L')
pixel_values = np.array(gray_image.histogram())
plt.bar(range(256), pixel_values, color='gray') plt.xlabel('Pixel Value') plt.ylabel('Frequency') plt.title('Image Histogram')
plt.show()
|

随后我们将用幂率分布对其进行拟合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| alpha = 3.0 xmin = 10.0 xmax = 250.0
x = np.linspace(xmin, xmax, 1000)
def power_law_pdf(x, alpha, xmin): return alpha * (xmin ** alpha) / x ** (alpha + 1) y = 300000*power_law_pdf(x, alpha, xmin)+3000
plt.plot(x, y, label=f'Power Law Distribution (alpha={alpha})')
plt.title('Power Law Distribution Curve') plt.xlabel('Value') plt.ylabel('Probability Density')
plt.legend()
plt.show()
|

或者我们把直方图均匀化
1 2 3 4 5 6 7 8
| import cv2 import matplotlib.pyplot as plt image = cv2.imread(r'high.png', cv2.IMREAD_GRAYSCALE) equ = cv2.equalizeHist(image) plt.hist(equ.ravel(), 256) plt.show() cv2.imshow('result', equ) cv2.waitKey()
|

在对其进行拟合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| import cv2 import matplotlib.pyplot as plt image = cv2.imread(r'high.png', cv2.IMREAD_GRAYSCALE) equ = cv2.equalizeHist(image) plt.hist(equ.ravel(), 256)
import numpy as np import matplotlib.pyplot as plt
from PIL import Image
alpha = 10.0 xmin = 70.0 xmax = 250.0
x = np.linspace(xmin, xmax, 1000)
def power_law_pdf(x, alpha, xmin): return alpha * (xmin ** alpha) / x ** (alpha + 1) y = 500000*power_law_pdf(x, alpha, xmin)+3000
plt.plot(x, y, label=f'Power Law Distribution (alpha={alpha})')
plt.title('Power Law Distribution Curve') plt.xlabel('Value') plt.ylabel('Probability Density')
plt.legend()
plt.show()
|
