直接参考opencv文档:
1,函数原型:
C++: void cv::cartToPolar ( InputArray x, InputArray y, OutputArray magnitude, OutputArray angle, bool angleInDegrees = false ) Python: magnitude, angle = cv.cartToPolar( x, y[, magnitude[, angle[, angleInDegrees]]] )2,参数说明:
参考文档直接献上大作: https://www.researchgate.net/publication/308277922_Fast_Optical_Flow_Using_Dense_Inverse_Search 算法解析参考: https://blog.csdn.net/xiechaoyi123/article/details/92798967 参考示例: https://blog.csdn.net/weixin_43922139/article/details/84785637?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param
dis光流的简单用法:
def dis_optical_main(): video_dir = r'E:\disopticalflow' src_name = '768x576.mp4' video_src = os.path.join(video_dir, src_name) result_name = 'optical_result.mp4' cap = cv2.VideoCapture(video_src) if not cap.isOpened(): print('cap open failed.') return fps = float(cap.get(cv2.CAP_PROP_FPS)) height0 = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) width0 = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # if zoom width = width0 # maybe other values width = width if width else width0 height = height0 * width // width0 vw = cv2.VideoWriter(os.path.join(video_dir, os.path.basename(result_name)), cv2.VideoWriter_fourcc(*'DIVX'), fps, (width, height)) ret, pre_frame = cap.read() if not ret: print('cap read failed.') vw.release() return pre_frame = cv2.resize(pre_frame, (width, height)) pre_frame = cv2.cvtColor(pre_frame, cv2.COLOR_BGR2GRAY) flow_obj = cv2.DISOpticalFlow_create(cv2.DISOPTICAL_FLOW_PRESET_ULTRAFAST) frame_cnt = 1 while True: print(frame_cnt) ret, cur_frame = cap.read() if ret: cur_frame = cv2.resize(cur_frame, (width, height)) cur_frame = cv2.cvtColor(cur_frame, cv2.COLOR_BGR2GRAY) flow = flow_obj.calc(pre_frame, cur_frame, None, ) x = flow[..., 0] # x方向光流 # print(x.shape, type(x)) y = flow[..., 1] # y方向光流 magnitude, angle = cv2.cartToPolar(x, y, angleInDegrees=True) # 转换为极坐标下 大小与角度 print(magnitude[0][0], angle[0][0]) flow_mask = np.where(magnitude >= 0.7, 255, 0).astype(np.uint8) # binary img # contours, hierarchy = cv2.findContours(flow_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # cv2.drawContours(flow_mask, contours, -1, 255, thickness=-1) # flow_frame = cv2.bitwise_and(flow, flow, mask=flow_mask) # print(flow_frame.shape, type(flow_frame)) # (576, 768, 2) <class 'numpy.ndarray'> flow_frame = cv2.bitwise_and(flow_mask, cur_frame) flow_frame = cv2.cvtColor(flow_frame, cv2.COLOR_GRAY2BGR) cv2.putText(flow_frame, "frameid: %d" % frame_cnt, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA) vw.write(flow_frame) frame_cnt += 1 else: break #if cv2.waitKey(1) & 0xFF == ord('q'): # break if cv2.waitKey(1) == 27: break cap.release() vw.release() if __name__ == '__main__': dis_optical_main()