识别该图片中每隔0.01s的荷载数据,并导入到Excel中。
import cv2
from PIL import Image
import sys
def cut_image(image):
width, height = image.size
print(width, height)
item_width = int(width / 800)
box_list = []
# (left, upper, right, lower)
for i in range(800):#0.01s为一段,8s=800个0.01s
box = (i*item_width,0,(i+1)*item_width,height)
box_list.append(box)
image_list = [image.crop(box) for box in box_list] #crop用于切割图片
return image_list
def save_images(image_list):
index = 1
for image in image_list:
image.save('D:/python_opencv/img/'+str(index) + '.png', 'PNG')
index += 1
if __name__ == '__main__':
file_path = "D:/002.png"
image = Image.open(file_path)
#image.show()
image_list = cut_image(image)
save_images(image_list)
import cv2
import numpy as np
import shutil
import os
import math
import matplotlib.pyplot as plt
Img_FOLDER = "D:/python_opencv/img"
list_y = []
for i in range(800):
img = cv2.imread('D:/python_opencv/img/'+str(i+1) + '.png')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
low_hsv = np.array([0,0,46])
high_hsv = np.array([180,255,220])
mask = cv2.inRange(hsv,lowerb=low_hsv,upperb=high_hsv) #利用cv2.inRange函数设阈值,去除背景部分
dst = cv2.GaussianBlur(mask,(3,3),0)
yx = np.column_stack(np.where(mask==255))
yx = 702-yx
y = max(yx[:,0])
y_new = y*50/702 - 10 #根据像素与所需尺度的关系确定
list_y.append(y_new)
list_x = np.arange(0,8,0.01)
plt.plot(list_x,list_y,label='Frist line',linewidth=3,color='r')
plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()
import xlwt
wb = xlwt.Workbook()
# 添加一个表
ws = wb.add_sheet('result')
# 3个参数分别为行号,列号,和内容
# 需要注意的是行号和列号都是从0开始的
ws.write(0, 0, "x")
ws.write(0, 1, "y")
i = 1
for x in list_x:
ws.write(i, 0, x)
i += 1
j = 1
for y in list_y:
ws.write(j, 1, y)
j += 1
# 保存excel文件
wb.save('D:/python_opencv/result.xls')