Skip to content

Quick start

gongpx20069 edited this page Aug 11, 2020 · 11 revisions

What if we need to capture the dense optical flow between two images? We only need the following code:

import cv2
from opencv_flow import OpticFlow

of = OpticFLow(mode = 'pcaflow')

img1 = cv2.imread('test/1.jpg')
img2 = cv2.imread('test/2.jpg')
flow = of.getflow(img1, img2)

Suppose the size of img1 and img2 is (256, 256, 3), then the output flow size is (256, 256, 2)。 We can select the optical flow estimation algorithm by initializing the mode parameter in OpticFlow(mode = 'pcaflow'), or reset the optical flow estimation algorithm through the setmode(mode = 'pcaflow') function. As shown in the following code (mode = 'pcaflow', 'deepflow', 'disflow', 'franeback', 'simpleflow' or 'denseflow'. Corresponding to different methods in opencv and opencv-contrib ):

of.setmode(mode = 'pcaflow')

If we need to visualize flow (turn flow into image), we need to input the following code:

flow2img = of.show_flow_hsv(flow)
cv2.imshow('flow', flow2img)
cv2.waitKey(0)

Here, flow2img is a color image, whose size is (256, 256, 3). Of cause, after we get an image and its flow, we can predict the image of the next frame. The code is as follows:

img2 = of.predict_next(img1, flow)

The size of img1 is (256, 256, 3), the size of flow is (256, 256, 2), and img2 is a gray image with a size of (256, 256).

Clone this wiki locally