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)]
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()