只能实现单张照片标记,无循环功能,日后改进
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import numpy as np
from scipy.io import savemat
from PIL import Image
# 加载图像
def load_image(path):
return np.array(Image.open(path))
# 初始化坐标列表
coordinates = []
# 鼠标点击事件处理函数
def onclick(event):
ix, iy = event.xdata, event.ydata
coordinates.append([ix, iy])
print(f'在x={ix}, y={iy}处标记点')
# 可选:动态地在图上标记点
ax.plot(ix, iy, 'ro')
fig.canvas.draw()
# 图像路径
image_path = r'F:\crowd_data_self\img_6.jpg'
img = load_image(image_path)
fig, ax = plt.subplots()
ax.imshow(img)
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
# 关闭绘图窗口后
# 准备.mat文件数据结构
mat_data = {
'__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Sat Oct 24 00:00:31 2015',
'__version__': '1.0',
'__globals__': [],
'image_info': np.array([[(np.array(coordinates), np.array([[len(coordinates)]], dtype=np.uint8))]],
dtype=[('location', 'O'), ('number', 'O')])
}
# 保存为.mat文件
savemat('img_6.mat', mat_data)
print("数据已保存到'img_6.mat'")
存档供自我学习