# Compute Optical Flow Velocities

This example shows how to compute the optical flow velocities for a moving object in a video or image sequence.

Read two image frames from an image sequence into the MATLAB workspace.

```I1 = imread('car_frame1.png'); I2 = imread('car_frame2.png'); ```

```modelname = 'ex_blkopticalflow.slx'; open_system(modelname) ``` The model reads the images by using the Image From Workspace block. To compute the optical flow velocities, you must first convert the input color images to intensity images by using the Color Space Conversion block. Then, find the velocities by using the `Optical Flow` block with these parameter values:

• Method - `Horn-Schunck`

• Compute optical flow between - `Two images`

• Smoothness factor - `1`

• Stop iterative solution - `When maximum number of iterations is reached`

• Maximum number of iterations - `10`

• Velocity output - `Horizontal and vertical components in complex form`

Overlay both the image frames by using the Compositing block and use the overlaid image to plot the results.

Run the model.

```out = sim(modelname); ```

Read the output velocities and the overlaid image.

```Vx = real(out.simout); Vy = imag(out.simout); img = out.simout1; ```

Create an optical flow object by using the `opticalFlow` function.

```flow = opticalFlow(Vx,Vy); ```

Display the overlaid image and plot the velocity vectors by using the `plot` function.

```figure imshow(img) hold on plot(flow,'DecimationFactor',[5 5],'ScaleFactor',40) ```
{"ft_lang_label":"__label__en","ft_lang_prob":0.68403715,"math_prob":0.95678633,"size":1414,"snap":"2021-43-2021-49","text_gpt3_token_len":315,"char_repetition_ratio":0.14609928,"word_repetition_ratio":0.0,"special_character_ratio":0.21570013,"punctuation_ratio":0.12749004,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9971421,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,1,null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-25T10:51:34Z\",\"WARC-Record-ID\":\"<urn:uuid:7b6f6550-d89a-4ee9-bdb5-e8e125b84359>\",\"Content-Length\":\"69888\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e8833acf-d91e-4955-aec9-f82c7b987731>\",\"WARC-Concurrent-To\":\"<urn:uuid:f19b0469-b57e-4dae-9c5d-3eb1e9779d6e>\",\"WARC-IP-Address\":\"23.13.150.165\",\"WARC-Target-URI\":\"https://de.mathworks.com/help/vision/ug/compute-optical-flow-velocities.html\",\"WARC-Payload-Digest\":\"sha1:4BGTL2AEZQEHS55QQ44OICM7T24OEN4L\",\"WARC-Block-Digest\":\"sha1:2RHTF37ZK7PM2DY4G54ETXDRTA4HFK6Z\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323587659.72_warc_CC-MAIN-20211025092203-20211025122203-00719.warc.gz\"}"}