import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 定义图像文件夹路径和裁剪的中心坐标
image_folder = 'E:\\data_gray'
# 创建保存子图像的目录
output_folder = 'E:\\data_gray_out'
coordinates = [(860, 2800), (2020, 2800), (3100, 2800), (860, 9000), (2020, 9000),(3200, 9000), (860, 10000), (2020, 10000),(3100, 10000)]
# 遍历图像文件夹中的.bmp格式图像
for filename in os.listdir(image_folder):
if filename.endswith('.bmp'):
# 读取大图像
image = cv2.imread(os.path.join(image_folder, filename))
os.makedirs(output_folder, exist_ok=True)
# 绘制大图像和子图像标题
fig, axs = plt.subplots(3, 3)
filename = filename.split(".")[0]
fig.suptitle(filename)
for i, coord in enumerate(coordinates):
x, y = coord
sub_image = image[y-32:y+32, x-32:x+32]
# 绘制裁图
axs[i//3, i%3].imshow(cv2.cvtColor(sub_image, cv2.COLOR_BGR2RGB))
axs[i//3, i%3].set_title(f'Subimage {i+1}')
axs[i//3, i%3].axis('off')
# 保存绘制后的图像
output_filename = f'{filename}_org.png'
plt.savefig(os.path.join(output_folder, output_filename))
plt.close(fig)
# 直方图
for i, coord in enumerate(coordinates):
x, y = coord
sub_image = image[y-32:y+32, x-32:x+32]
mean_value = np.mean(sub_image)
max_value = np.max(sub_image)
min_value = np.min(sub_image)
diff_value = max_value - min_value
# 绘制灰度直方图
plt.subplot(3, 3, i+1)
plt.hist(sub_image.flatten(), bins=256, range=[0, 256], color='gray')
plt.title(f'M: {mean_value:.1f} , {max_value} - {min_value} = {diff_value}', fontsize=9)
# 保存灰度直方图
output_folder = 'E:\\data_gray_out'
output_path = os.path.join(output_folder, f'{filename}_histogram.png')
plt.subplots_adjust(wspace=0.6, hspace=0.6)
plt.savefig(output_path)
plt.close()
guangxue
最新推荐文章于 2024-06-04 13:03:32 发布