写一段利用python语言,读取悬臂梁振动冲击视频,利用圆心标靶检测算法提取结构6个测点的位移并绘制每个测点的位移曲线的算法
时间: 2024-03-10 13:43:46 浏览: 24
好的,以下是利用Python语言读取悬臂梁振动冲击视频,利用圆心标靶检测算法提取结构6个测点的位移并绘制每个测点的位移曲线的算法:
1. 导入需要的库
```python
import cv2
import numpy as np
import matplotlib.pyplot as plt
```
2. 读取视频文件
```python
cap = cv2.VideoCapture('video.mp4') #替换为实际视频文件名
```
3. 定义圆心标靶检测函数
```python
def detect_circles(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)
circles = np.uint16(np.around(circles))
return circles
```
4. 提取结构6个测点的圆心坐标
```python
circles = []
while True:
ret, frame = cap.read()
if ret == False:
break
circle = detect_circles(frame)
circles.append(circle)
circles = np.array(circles)
x = circles[:, :, 0].mean(axis=0)
y = circles[:, :, 1].mean(axis=0)
```
5. 定义计算位移的函数
```python
def calculate_displacement(x, y, ref_x, ref_y):
displacement = np.sqrt((x - ref_x) ** 2 + (y - ref_y) ** 2)
return displacement
```
6. 提取每个测点的位移并绘制位移曲线
```python
displacements = []
for i in range(len(circles)):
displacement = calculate_displacement(circles[i][:, 0], circles[i][:, 1], x, y)
displacements.append(displacement)
displacements = np.array(displacements)
plt.figure(figsize=(10, 5))
for i in range(6):
plt.plot(displacements[:, i], label='Point %d' % (i + 1))
plt.legend()
plt.xlabel('Frame')
plt.ylabel('Displacement')
plt.title('Displacement vs. Frame')
plt.show()
```
以上就是利用Python语言读取悬臂梁振动冲击视频,利用圆心标靶检测算法提取结构6个测点的位移并绘制每个测点的位移曲线的算法。