用python写一段代码:
随机抽取图像中100个像素点对应的RGB三通道值,分别对每一个通道绘制散点图,其中横坐标为像素点序号,纵坐标为该通道的值,纵坐标的坐标轴的值从0开始,分别计算对应的平均值、极差、方差、标准差,在图中绘制置信度为70%的区域(也就是这个区域包含80%的像素点)在图中绘制一条水平线,各个像素点到这条水平线的距离最短。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.image as mpimg
def compute_statistics(channel_values):
mean = np.mean(channel_values)
range_val = np.max(channel_values) - np.min(channel_values)
variance = np.var(channel_values)
std_dev = np.std(channel_values)
# Calculate 70% confidence interval using percentiles
confidence_interval = np.percentile(channel_values, [15, 85])
return mean, range_val, variance, std_dev, confidence_interval
def horizontal_regression(y):
return np.mean(y)
def random_pixel_channel_values(image, num_pixels=100):
# Flatten the image array to get all pixel values
pixels = image.reshape(-1, 3)
# Randomly select num_pixels from all pixel values
selected_pixels = pixels[np.random.choice(pixels.shape[0], num_pixels, replace=False)]
# Split the selected pixels into RGB channels
r_channel_values = selected_pixels[:, 0]
g_channel_values = selected_pixels[:, 1]
b_channel_values = selected_pixels[:, 2]
return r_channel_values, g_channel_values, b_channel_values
# Replace 'path/to/your/image.png' with the actual image file path
image_path = 'total_fire4.jpg'
image_array = mpimg.imread(image_path)
r_values, g_values, b_values = random_pixel_channel_values(image_array)
# Compute horizontal regression for each channel
r_constant = horizontal_regression(r_values)
g_constant = horizontal_regression(g_values)
b_constant = horizontal_regression(b_values)
# Compute statistics for each channel
r_mean, r_range, r_variance, r_std_dev, r_confidence_interval = compute_statistics(r_values)
g_mean, g_range, g_variance, g_std_dev, g_confidence_interval = compute_statistics(g_values)
b_mean, b_range, b_variance, b_std_dev, b_confidence_interval = compute_statistics(b_values)
# Create subplots for each channel
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(18, 6))
# Plotting scatter plots for each channel
ax1.scatter(range(len(r_values)), r_values, color='red', label='Red Channel', s=5)
ax2.scatter(range(len(g_values)), g_values, color='green', label='Green Channel', s=5)
ax3.scatter(range(len(b_values)), b_values, color='blue', label='Blue Channel', s=5)
# Adding horizontal regression line to the plots
ax1.axhline(y=r_constant, color='red', label='Horizontal Regression', linewidth=2)
ax2.axhline(y=g_constant, color='green', label='Horizontal Regression', linewidth=2)
ax3.axhline(y=b_constant, color='blue', label='Horizontal Regression', linewidth=2)
# Adding confidence intervals to the plots
ax1.axhline(y=r_confidence_interval[0], color='red', linestyle='dashed', linewidth=1)
ax1.axhline(y=r_confidence_interval[1], color='red', linestyle='dashed', linewidth=1)
ax2.axhline(y=g_confidence_interval[0], color='green', linestyle='dashed', linewidth=1)
ax2.axhline(y=g_confidence_interval[1], color='green', linestyle='dashed', linewidth=1)
ax3.axhline(y=b_confidence_interval[0], color='blue', linestyle='dashed', linewidth=1)
ax3.axhline(y=b_confidence_interval[1], color='blue', linestyle='dashed', linewidth=1)
# Set y-axis limits to ensure 0 to 255 range
ax1.set_ylim(0, 255)
ax2.set_ylim(0, 255)
ax3.set_ylim(0, 255)
# Add statistics to each plot
ax1.text(0.1, 0.9, f'Mean: {r_mean:.2f}\nRange: {r_range:.2f}\nStd Dev: {r_std_dev:.2f}', transform=ax1.transAxes, fontsize=10)
ax2.text(0.1, 0.9, f'Mean: {g_mean:.2f}\nRange: {g_range:.2f}\nStd Dev: {g_std_dev:.2f}', transform=ax2.transAxes, fontsize=10)
ax3.text(0.1, 0.9, f'Mean: {b_mean:.2f}\nRange: {b_range:.2f}\nStd Dev: {b_std_dev:.2f}', transform=ax3.transAxes, fontsize=10)
ax1.set_xlabel('Pixel Index')
ax1.set_ylabel('Red Channel Value')
ax1.legend()
ax2.set_xlabel('Pixel Index')
ax2.set_ylabel('Green Channel Value')
ax2.legend()
ax3.set_xlabel('Pixel Index')
ax3.set_ylabel('Blue Channel Value')
ax3.legend()
plt.tight_layout() # Ensures labels do not overlap
plt.show()