作业15:
计算第一帧选定的边框,和接下来每一帧随机生成的边框的重叠率,并输出到命令行。
参考链接:
https://blog.csdn.net/u011418530/article/details/79986041(Python 里的%s 和 %d都是什么意思?)
https://www.cnblogs.com/graceting/p/3875438.html(python 中 print 函数用法总结)
1、
print("The coincide of is %f" % solve_coincide(w, new_r))#******
2、
coincide = abs(intersection / (area1 + area2 - intersection))####abs
3、
new_r = np.array([left_x_up, left_y_up, right_x_down, right_y_down])#用np.array写矩阵
答案:
import cv2 as cv
import numpy as np
import random
frames= 0
capture = cv.VideoCapture(0) # 创建一个VideoCapture对象
result_video = "result.mp4"
r = None
while(True):
ret, frame = capture.read() # 一帧一帧读取视频
cv.imshow('frame ', frame)#视频frame的第一帧用于画框
if frames == 0:
r = cv.selectROI(frame)
L_x=r[0]
L_y=r[1]
R_x=r[0]+r[2]
R_y=r[1]+r[3]
w=np.array([L_x,L_y,R_x,R_y])
# Crop image
imCrop = frame[int(r[1]):int(r[1] + r[3]), int(r[0]):int(r[0] + r[2])]#这里才是真正的左X左Y,右X右Y呢
cv.imshow('cut ', imCrop) # 显示一下带框的结果(视频cut的第一帧(带框))
frames += 1
cv.rectangle(frame, r, (55, 255, 155), 5)
#cv.putText(frame, 'frame_%s' % frame, (word_x, word_y), cv.FONT_HERSHEY_SIMPLEX, 1, (55, 255, 155), 2)
x=r[0]+r[2]//2#r中的元素一定要分清楚Imcrop里谁是左X谁是右X谁是左Y谁是右,按这个对照imCrop = frame[right_y_down:left_y_up, left_x_up:right_x_down, :]
y=r[1]+r[3]//2#//为python整除
left_x_up = x-random.randint(0,100)
left_y_up = y+random.randint(0,100)#注意这里的调参
right_x_down = x + random.randint(0,100)
right_y_down = y - random.randint(0,100)
new_r = np.array([left_x_up, left_y_up, right_x_down, right_y_down])#******
def mat_inter(box1, box2):
# 判断两个矩形是否相交
# box=(xA,yA,xB,yB)
L_x, L_y, R_x, R_y = box1
left_x_up, left_y_up, right_x_down, right_y_down = box2
lx = abs((L_x + R_x) / 2 - (left_x_up + right_x_down) / 2)
ly = abs((L_y + R_y) / 2 - (left_y_up + right_y_down) / 2)
sax = abs(L_x - R_x)
sbx = abs(left_x_up - right_x_down)
say = abs(L_y - R_y)
sby = abs(left_y_up - right_y_down)
if lx <= (sax + sbx) / 2 and ly <= (say + sby) / 2:
return True
else:
return False
def solve_coincide(box1, box2):
# box=(xA,yA,xB,yB)
# 计算两个矩形框的重合度
if mat_inter(box1, box2) == True:
L_x, L_y, R_x, R_y = box1
left_x_up, left_y_up, right_x_down, right_y_down = box2
col = min(R_x, right_x_down) - max(L_x, left_x_up)
row = min(R_y, right_y_down) - max(L_y, left_y_up)
intersection = col * row
area1 = abs(R_x - L_x) * abs(R_y - L_y)
area2 = abs(right_x_down - left_x_up) * abs(right_y_down - left_y_up)
coincide = abs(intersection / (area1 + area2 - intersection))
return coincide
else:
return False
print("The coincide of is %f" % solve_coincide(w, new_r))#******
#left_x_up = min(left_x_up, right_x_down)
#left_y_up = max(left_y_up, right_y_down)
#right_x_down = max(left_x_up, right_x_down)
#right_y_down = min(left_y_up, right_y_down)
cv.rectangle(frame, (left_x_up, left_y_up), (right_x_down, right_y_down), (55, 255, 155), 5)
imCrop = frame[right_y_down:left_y_up, left_x_up:right_x_down, :]
# cv.rectangle(frame, r, (55, 255, 155), 5)
# cv.putText(frame, 'frame_%s' % frame, (word_x, word_y), cv.FONT_HERSHEY_SIMPLEX, 1, (55, 255, 155), 2)
cv.imshow('angle', frame) # 带有目标框的视频
cv.imwrite('cut' + str(frames) + '.jpg', imCrop) # 存储每帧框内图片,注意加上.jpg存储图片
#cv.imshow('angle',frame) #带有目标框的视频
if cv.waitKey(1) &0xFF == ord('q'): # 按q停止
break
capture.release() # 释放cap,销毁窗口
cv.destroyAllWindows()