import os import cv2 import numpy as np import skvideo skvideo.setFFmpegPath('anaconda3/envs/test/bin/') import skvideo.io print ("hi") # caminho para o vídeo de entrada path2video = (r"C:\Users\alif\Desktop\Optical_Flow\VIDEOS/Tudo_bem_3.mp4") outputPath = (r"C:\Users\alif\Desktop\Optical_Flow\OptFlowMaps") # read the video to create frames-generator videogen = skvideo.io.vread(path2video) print(f'shape of video {path2video} = {videogen.shape}') # reading first frame as first and previous frames # New image (array) filled with zeros in same dimensions as input frame (3 chanels) frame_ind = 0 hsv = np.zeros_like(videogen[frame_ind]) # conver input frame from RGB to gray prevF = cv2.cvtColor(videogen[frame_ind], cv2.COLOR_RGB2GRAY) print("Reading frames...") # range com a largura de videogen for index in range(len(videogen)): # printando a informação de qual frame está sendo processado por iteração print(f"{index}/{len(videogen)}") # conver input frame from RGB to gray nextF = cv2.cvtColor(videogen[index], cv2.COLOR_RGB2GRAY) # maintain similar size of next anf previous frames; shape[1] é a width, e shape[2] é a high dim = (prevF.shape[1], prevF.shape[0]) nextF = cv2.resize(nextF, dim) #computing optical flow map by Farneback method. Returns 2D vector of size input frame # cv.calcOpticalFlowFarneback(prev, next, flow, pyr_scale, levels, winsize, iterations, poly_n, poly_sigma, flags ) flow = cv2.calcOpticalFlowFarneback(prevF, # prev nextF, # next None, # flow 0.5, # pyr_sacel 3, # levels 15, # winsize 3, # iterations 5, # poly_n 1.2, # poly_sigma 0) # flags
# Computando a magnitude e o ângulo dos vetores 2D mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1]) # Setting hue image de acordo com o a direção do fluxo óptico hsv[..., 0] = (((ang * 180) / (np.pi)) / 2) # setting hue image intensities de acordo com a magnitude do fluxo óptico normalizado hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX) # Convertendo hue image from HSV to BGR format bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) # fluxo óptico é salvo com o nome do frame dest = outputPath + str(index) + ".png" cv2.imwrite(dest, bgr) # Fluxo óptico se torna o frame anterior e loop se resume no novo nextF prevF = nextF
Excellent video! It helped me a lot :3
Glad it helped!
i know the pixel width in meters. now i have a sequence of point-symmetrical images. i want to measure a speed field. #skyimager
import os
import cv2
import numpy as np
import skvideo
skvideo.setFFmpegPath('anaconda3/envs/test/bin/')
import skvideo.io
print ("hi")
# caminho para o vídeo de entrada
path2video = (r"C:\Users\alif\Desktop\Optical_Flow\VIDEOS/Tudo_bem_3.mp4")
outputPath = (r"C:\Users\alif\Desktop\Optical_Flow\OptFlowMaps")
# read the video to create frames-generator
videogen = skvideo.io.vread(path2video)
print(f'shape of video {path2video} = {videogen.shape}')
# reading first frame as first and previous frames
# New image (array) filled with zeros in same dimensions as input frame (3 chanels)
frame_ind = 0
hsv = np.zeros_like(videogen[frame_ind])
# conver input frame from RGB to gray
prevF = cv2.cvtColor(videogen[frame_ind], cv2.COLOR_RGB2GRAY)
print("Reading frames...")
# range com a largura de videogen
for index in range(len(videogen)):
# printando a informação de qual frame está sendo processado por iteração
print(f"{index}/{len(videogen)}")
# conver input frame from RGB to gray
nextF = cv2.cvtColor(videogen[index], cv2.COLOR_RGB2GRAY)
# maintain similar size of next anf previous frames; shape[1] é a width, e shape[2] é a high
dim = (prevF.shape[1], prevF.shape[0])
nextF = cv2.resize(nextF, dim)
#computing optical flow map by Farneback method. Returns 2D vector of size input frame
# cv.calcOpticalFlowFarneback(prev, next, flow, pyr_scale, levels, winsize, iterations, poly_n, poly_sigma, flags )
flow = cv2.calcOpticalFlowFarneback(prevF, # prev
nextF, # next
None, # flow
0.5, # pyr_sacel
3, # levels
15, # winsize
3, # iterations
5, # poly_n
1.2, # poly_sigma
0) # flags
# Computando a magnitude e o ângulo dos vetores 2D
mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
# Setting hue image de acordo com o a direção do fluxo óptico
hsv[..., 0] = (((ang * 180) / (np.pi)) / 2)
# setting hue image intensities de acordo com a magnitude do fluxo óptico normalizado
hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
# Convertendo hue image from HSV to BGR format
bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
# fluxo óptico é salvo com o nome do frame
dest = outputPath + str(index) + ".png"
cv2.imwrite(dest, bgr)
# Fluxo óptico se torna o frame anterior e loop se resume no novo nextF
prevF = nextF