# -*- coding: utf-8 -*-
import sys
import cv2
import numpyas np
import time
import sys
#
# from PyQt5.QtCore import *
# from PyQt5.QtGui import *
# from PyQt5.QtWidgets import *
current_pos= None
tl= None
br= None
#鼠标事件
def get_rect(im, title='get_rect'): # (a,b) = get_rect(im, title='get_rect')
mouse_params= {'tl': None, 'br': None, 'current_pos': None,
'released_once': False}
cv2.namedWindow(title)
cv2.moveWindow(title, 100, 100)
def onMouse(event, x, y, flags, param):
param['current_pos']= (x, y)
if param['tl']is not None and not (flags & cv2.EVENT_FLAG_LBUTTON):
param['released_once']= True
if flags & cv2.EVENT_FLAG_LBUTTON:
if param['tl']is None:
param['tl']= param['current_pos']
elif param['released_once']:
param['br']= param['current_pos']
cv2.setMouseCallback(title, onMouse, mouse_params)
cv2.imshow(title, im)
while mouse_params['br']is None:
im_draw= np.copy(im)
if mouse_params['tl']is not None:
cv2.rectangle(im_draw, mouse_params['tl'],
mouse_params['current_pos'], (0, 240, 120))
cv2.imshow(title, im_draw)
_= cv2.waitKey(10)
cv2.destroyWindow(title)
tl= (min(mouse_params['tl'][0], mouse_params['br'][0]),
min(mouse_params['tl'][1], mouse_params['br'][1]))
br= (max(mouse_params['tl'][0], mouse_params['br'][0]),
max(mouse_params['tl'][1], mouse_params['br'][1]))
return (tl, br)#tl=(y1,x1), br=(y2,x2)
#读取摄像头/视频,然后用鼠标事件画框
def readVideo(pathName, skipFrame, new_path): #pathName为视频文件路径,skipFrame为视频的第skipFrame帧
cap= cv2.VideoCapture(0)#读取摄像头
if not cap.isOpened(): #如果为发现摄像头,则按照路径pathName读取视频文件
cap= cv2.VideoCapture(pathName)#读取视频文件,如pathName='D:/test/test.mp4'
c= 1
s= 0
while(cap.isOpened()):
s+= 1
if s== 2:
break
ret, frame= cap.read()
print('................................s%s',s)
gray= cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if(c>=skipFrame):
mask = np.zeros(gray.shape, dtype=np.uint8)#掩码操作,该矩阵与图片大小类型一致,为初始化全0像素值,之后对其操作区域赋值为1即可
if(c==skipFrame):
(a,b)= get_rect(frame, title='get_rect')#鼠标画矩形框
img01, img02 = frame, frame
gray01, gray02 = gray, gray
fps= cap.get(cv2.CAP_PROP_FPS)# 获取视频帧率
fourcc = cv2.VideoWriter_fourcc(*'MPEG')# 使用XVID编码器
print('test info ...............')
Width_choose= b[0]- a[0]# 选中区域的宽
Height_choose= b[1]- a[1]# 选中区域的高
print("视频选中区域的宽:%d" % Width_choose, '\n'"视频选中区域的高:%d" % Height_choose)
print(Width_choose)
print(Height_choose)
cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
out= cv2.VideoWriter(new_path, cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), fps,
(Width_choose, Height_choose))# 参数分别是:保存的文件名、编码器、帧率、视频宽高
Video_choose = np.zeros((Width_choose, Height_choose, 3), np.uint8)
num= 0
while True:
num+= 1
print num
grabbed, frame1= cap.read()# 逐帧采集视频流
if not grabbed:
break
if num== 374:
print frame1
print('....................fram')
print grabbed
print frame1
if frame1.any()==None:
print('..................ssssss')
break
gray_lwpCV= cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)#cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY) # 转灰度图
frame_data= np.array(gray_lwpCV)# 每一帧循环存入数组
box_data= frame_data[a[1]:b[1], a[0]:b[0]]# 取矩形目标区域
pixel_sum= np.sum(box_data, axis=1)# 行求和q
x= range(Height_choose)
emptyImage= np.zeros((Width_choose* 10, Height_choose* 2, 3), np.uint8)
Video_choose= frame1[a[1]:b[1], a[0]:b[0]]
out.write(Video_choose)
cv2.imshow('Video_choose', Video_choose)
for iin x:
cv2.rectangle(emptyImage, (i* 2, (Width_choose- pixel_sum[i]// 255)* 10),
((i+ 1)* 2, Width_choose* 10),
(0, 240, 120), 1)
emptyImage = cv2.resize(emptyImage, (320, 240))
# lwpCV_box = cv2.rectangle(frame, tuple(self.coor[1, :]), tuple(self.coor[2, :]), (0, 255, 0), 2)
cv2.imshow('lwpCVWindow', frame)# 显示采集到的视频流
# videoWriter.write(lwpCV_box) # 将截取到的画面写入“新视频”
# videoWriter = ('lwpCVWindow', frame)
# cv2.imshow('sum', emptyImage) # 显示画出的条形图
key= cv2.waitKey(1)& 0xFF
if key== ord('q'):
break
# out.release()
# camera.release()
# cv2.destroyAllWindows()
else:
img1, img2 = prev_frame, frame
gray1, gray2 = prev_frame, frame
cv2.imshow('frame', frame)
c= c+ 1
prev_gray = gray
prev_frame= frame
if cv2.waitKey(1)& 0xFF == ord('q'): #点击视频窗口,按q键退出
break
cap.release()
cv2.destroyAllWindows()