#高斯滤波
def clean_img(img):
return cv.GaussianBlur(img,(5,5),0)
#图片添加文字, state=0:静止, state=1:运动
def addword(img,state):
font = ImageFont.truetype("font/simsun.ttc", 32)
img_pil = Image.fromarray(img)
draw = ImageDraw.Draw(img_pil)
if state == 0:
draw.text((0, 0), "机械臂在静止", font = font, fill = (0, 2, 255))
else:
draw.text((0, 0), "机械臂在晃动", font=font, fill=(0, 2, 255))
img = np.array(img_pil)
return img
[x1, y1] = [288, 135]
[x2, y2] = [358, 166]
#边缘图中只显示感兴趣的部分
def black(edges):
global x1, y1, x2, y2
wide, length = edges.shape
edges[0:wide, 0:x1] = 0
edges[ 0:y1, 0:length] = 0
edges[0:wide, x2:length] = 0
edges[y2:wide, 0:length] = 0
return edges
#检测并绘制直线
def draw_line(img):
img = clean_img(img)
gray = cv.cvtColor(np.array(img), cv.COLOR_BGR2GRAY)
edges = cv.Canny(gray, 60, 200)#转化为边缘图
lines = cv.HoughLines(black(edges), 1, np.pi / 180, 20)
global a, b
for line in lines:
rho, theta = line[0]
x1 = int(np.cos(theta) * rho - b * (-np.sin(theta)))
y1 = int(np.sin(theta) * rho - b * (np.cos(theta)))
x2 = int(np.cos(theta) * rho - a * (-np.sin(theta)))
y2 = int(np.sin(theta) * rho - a * (np.cos(theta)))
cv.line(img, (x1, y1), (x2, y2), (0, 0, 200), 3)#在图片上绘制直线
k = (y2-y1)/(x2-x1)
return img, k#输出第一条直线的斜率
1、
建立缓存数组list
2、
#IMG:需要处理的图片集合, img:其中一张图片, M:缓存数组容量
for img in IMG:
if len(list) < M:
将img添加到list
else:
处理list中相关信息
将list中图片输出
清空list
添加img到list中
while True:
ret, frame = cap.read()
if ret:
if len(list) < M:
list.append(frame)
else:
list.append(frame)
sum_k = 0
count = 0
for i in list:
img, k = draw_line(i)
sum_k += k
list[count] = img
count += 1
avr_k = sum_k/M
if abs(avr_k-k0) > ep:
state = 1
else:
state = 0
for i in list:
img = addword(i, state)
out.write(img)
cv.namedWindow('jpg', cv.WINDOW_NORMAL)
cv.imshow('jpg', img)
list = []
if cv.waitKey(1) & 0xFF == ord('q'):
break
else:
break
import cv2 as cv
import numpy as np
from PIL import ImageFont, ImageDraw, Image
#均值滤波
def clean_img(img):
return cv.GaussianBlur(img,(5,5),0)
#边缘图中只显示感兴趣的部分
def black(edges):
global x1, y1, x2, y2
wide, length = edges.shape
edges[0:wide, 0:x1] = 0
edges[ 0:y1, 0:length] = 0
edges[0:wide, x2:length] = 0
edges[y2:wide, 0:length] = 0
return edges
#图片添加文字, state=0:静止, state=1:运动
def addword(img,state):
font = ImageFont.truetype("font/simsun.ttc", 32)
img_pil = Image.fromarray(img)
draw = ImageDraw.Draw(img_pil)
if state == 0:
draw.text((0, 0), "机械臂在静止", font = font, fill = (0, 2, 255))
else:
draw.text((0, 0), "机械臂在晃动", font=font, fill=(0, 2, 255))
img = np.array(img_pil)
return img
#检测并绘制直线
def draw_line(img):
img = clean_img(img)
gray = cv.cvtColor(np.array(img), cv.COLOR_BGR2GRAY)
edges = cv.Canny(gray, 60, 200)
lines = cv.HoughLines(black(edges), 1, np.pi / 180, 20)
global a, b
for line in lines:
rho, theta = line[0]
x1 = int(np.cos(theta) * rho - b * (-np.sin(theta)))
y1 = int(np.sin(theta) * rho - b * (np.cos(theta)))
x2 = int(np.cos(theta) * rho - a * (-np.sin(theta)))
y2 = int(np.sin(theta) * rho - a * (np.cos(theta)))
cv.line(img, (x1, y1), (x2, y2), (0, 0, 200), 3)
k = (y2-y1)/(x2-x1)
return img, k
cap = cv.VideoCapture('D:/computer_view_exercise1/flapping_demo_video.avi')
[x1, y1] = [288, 135]
[x2, y2] = [358, 166]
b = 300
a = 375
k0 = (y2-y1)/(x2-x1)#初始斜率
ep = 0.05#判断是否运动的斜率阈值
count = 0#计数器
state = 0#运动标志变量
fourcc = cv.VideoWriter_fourcc(*'XVID')
fps = cap.get(cv.CAP_PROP_FPS)
# 设置视频大小
size = (int(cap.get(cv.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv.CAP_PROP_FRAME_HEIGHT)))
out = cv.VideoWriter('D:/computer_view_exercise1/out.avi',fourcc ,fps, size)
k = k0
list = []
M = 12#缓存最多图片张数
while True:
ret, frame = cap.read()
if ret:
if len(list) < M:
list.append(frame)
else:
list.append(frame)
sum_k = 0
count = 0
for i in list:
img, k = draw_line(i)
sum_k += k
list[count] = img
count += 1
avr_k = sum_k/M
if abs(avr_k-k0) > ep:
state = 1
else:
state = 0
for i in list:
img = addword(i, state)
out.write(img)
cv.namedWindow('jpg', cv.WINDOW_NORMAL)
cv.imshow('jpg', img)
list = []
if cv.waitKey(1) & 0xFF == ord('q'):
break
else:
break
sum_k = 0
count = 0
for i in list:
img, k = draw_line(i)
sum_k += k
list[count] = img
count += 1
avr_k = sum_k/M
if abs(avr_k-k0) > ep:
state = 1
else:
state = 0
for i in list:
img = addword(i, state)
out.write(img)
cv.namedWindow('jpg', cv.WINDOW_NORMAL)
cv.imshow('jpg', img)
list = []
cap.release()
out.release()
cv.destroyAllWindows()