- OpenCV => 3.2 (brew installed)
- Operating System / Platform => macOS 10.12
- Compiler => ipython
i use ipython to learn optical flow and when i try to convert two direction of dense optical flow into a colorful one i found that ver 3.2 could not convert image correctly (or maybe there are some delay in my loop?) but when i use ver 2.4, everything is fine, and image is converted very fast. could you help me find the problem?
here is my code
import numpy as np
import cv2
import time
def modify_image(img):
h = img.shape[0]
w = img.shape[1]
# vertical
if w < h:
resize = (256, int(256 * 1.0 / w * h)) # (w, h)
# horizontal
else:
resize = (int(256 * 1.0 / h * w), 256) # (w, h)
# modify
img = cv2.resize(img, resize)
return img
def color_to_grey(img_clr):
img_gry = cv2.cvtColor(img_clr, cv2.COLOR_BGR2GRAY)
return img_gry
def flow_to_image(flw, flw_bnd=15):
bnd_arr = np.full_like(flw, flw_bnd)
img_flw = flw.copy()
img_flw[(img_flw < -flw_bnd)] = -flw_bnd
img_flw[(img_flw > +flw_bnd)] = +flw_bnd
img_flw = ((img_flw + bnd_arr) / (2 * bnd_arr) * 255).astype(np.uint8)
return img_flw
cap = cv2.VideoCapture(0)
cv2.namedWindow('img_clr')
cv2.namedWindow('img_gry')
cv2.namedWindow('dof_gry_x')
cv2.namedWindow('dof_gry_y')
cv2.namedWindow('dof_hsv')
cv2.namedWindow('dof_bgr')
cv2.moveWindow('img_clr', 300, 125)
cv2.moveWindow('img_gry', 775, 125)
cv2.moveWindow('dof_gry_x', 300, 415)
cv2.moveWindow('dof_gry_y', 775, 415)
cv2.moveWindow('dof_hsv', 300, 705)
cv2.moveWindow('dof_bgr', 775, 705)
img_gry_prv = None
while True:
# Capture frame-by-frame
ret, frm = cap.read()
# img_clr
img_clr = modify_image(frm)
# img_gry
img_gry_nxt = color_to_grey(img_clr)
# dof_gry_x dof_gry_y
if img_gry_prv is None:
img_gry_prv = img_gry_nxt
dof = cv2.calcOpticalFlowFarneback(prev=img_gry_prv, next=img_gry_nxt, flow=None, pyr_scale=0.500, levels=3, winsize=10, iterations=1, poly_n=7, poly_sigma=1.5, flags=cv2.OPTFLOW_FARNEBACK_GAUSSIAN)
img_gry_prv = img_gry_nxt
dof_x = dof[..., 0]
dof_y = dof[..., 1]
dof_gry_x = flow_to_image(dof_x)
dof_gry_y = flow_to_image(dof_y)
# dof_hsv
mag, ang = cv2.cartToPolar(dof_x, dof_y)
dof_hsv = np.zeros_like(img_clr)
dof_hsv[...,0] = ang*180/np.pi/2
dof_hsv[...,1] = 255
dof_hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
# dof_bgr
dof_bgr = cv2.cvtColor(dof_hsv,cv2.COLOR_HSV2BGR)
# display
cv2.imshow('img_clr',img_clr)
cv2.imshow('img_gry',img_gry_nxt)
cv2.imshow('dof_gry_x',dof_gry_x)
cv2.imshow('dof_gry_y',dof_gry_y)
cv2.imshow('dof_hsv',dof_hsv)
cv2.imshow('dof_bgr',dof_bgr)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
time.sleep(0.1)
cv2.destroyAllWindows()