代码如下:
import os
import cv2
import math
from math import sqrt
import numpy
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import xlwt
import xlrd
import openpyxl
from PIL import Image
import matplotlib.image as mpimg
workbook = openpyxl.Workbook()
sheet = workbook.active
t = 0 #行
#图片文件夹路径
folder_path = 'C:/Tianyu/AAV/cut_imgs_partial'
for filename in os.listdir(folder_path):
# 读每张图像
img = cv2.imread(f'C:/Tianyu/AAV/cut_imgs/{filename}', 0)
img_1 = cv2.resize(img, (76,76))
h = img_1.shape[0]
w = img_1.shape[1]
x0 = round(w / 2)
y0 = round(h / 2)
if w > h:
R = math.floor(h / 2) - 1
else:
R = math.floor(w / 2) - 1
a = 36 #分成几等分
t = t + 1
s = 1
#每张图片上10个圆
for i in range(0,10):
e = R-round(i*R/10) #等分圆的半径
#每个圆36等分
for n in range(1,a+1):
xx = math.floor(e*np.cos(2*np.pi*(n-1)/a)) + x0 # 坐标
yy = math.floor(e*np.sin(2*np.pi*(n-1)/a)) + y0
#打印灰度值
# print(img_1[xx][yy])
#列数
s = s + 1
sheet.cell(row=t, column = s, value = img_1[xx][yy])
#打印文件名
print(f"{filename}")
sheet.cell(row=t, column = 1, value = f'{filename}')
#Excel保存路径
workbook.save('C:/Tianyu/AAV/内切圆统计/内切圆-partial/内切圆(36×10采样)-Partial.xlsx')