# Animated line drawings with OpenCV

DevFeed: [Animated line drawings with OpenCV](<https://devfeed.tech/articles/animated-line-drawings-with-opencv-21653.md>)

Original publisher: [Read original article](<https://www.windytan.com/2017/12/animated-line-drawings-with-opencv.html>)

Author: Oona Räisänen (noreply@blogger.com)

Published: 2017-12-30T12:15:00Z

Content type: tutorial

Language: en

Sources: [Oona Räisänen](<https://devfeed.tech/sources/oona-raisanen.md>)

Topics: [OpenCV](<https://devfeed.tech/topics/opencv.md>), [Image processing](<https://devfeed.tech/topics/image-processing.md>), [image effects](<https://devfeed.tech/topics/image-effects.md>), [generative art](<https://devfeed.tech/topics/generative-art.md>)

Tags: [animations](<https://devfeed.tech/tags/animations.md>), [art](<https://devfeed.tech/tags/art.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [cmake](<https://devfeed.tech/tags/cmake.md>), [computer-vision](<https://devfeed.tech/tags/computer-vision.md>), [drawing](<https://devfeed.tech/tags/drawing.md>), [graphics](<https://devfeed.tech/tags/graphics.md>), [image](<https://devfeed.tech/tags/image.md>), [matrix](<https://devfeed.tech/tags/matrix.md>), [music](<https://devfeed.tech/tags/music.md>), [opencv](<https://devfeed.tech/tags/opencv.md>), [pixel](<https://devfeed.tech/tags/pixel.md>), [plotting](<https://devfeed.tech/tags/plotting.md>), [programming](<https://devfeed.tech/tags/programming.md>)

## AI overview

The excerpt shows how to use OpenCV's C++ drawing tools to create pixel-level animations and save them as video. A minimal CMake project links OpenCV, and a zero-initialized 1920x1080 `cv::Mat` provides a black canvas. The example builds animated concentric rings with rotating spokes; antialiased subpixel positions are represented by multiplying coordinates by 8 and using a three-bit shift. To give the graphics a vector-display glow, the author applies Gaussian blur, noting that convolution is CPU-intensive and that blur calculation consumed much of the rendering time. The excerpt ends mid-discussion, so it does not establish the full rendering or video-writing workflow.

## Source excerpt

OpenCV is a pretty versatile C++ computer vision library. Because I use it every day it has also become my go-to tool for creating simple animations at pixel level, for fun, and saving them as video files. This is not one of its core functions but happens to be possible using its GUI drawing tools. Below we'll take a look at some video art I wrote for a music project. It goes a bit further than just line drawings but the rest is pretty much just flavouring. As you'll see, creating images in OpenCV has a lot in common with how you would work with layers and filters in an image editor like GIMP or Photoshop. Setting it up It doesn't take a lot of boilerplate to initialize an OpenCV project. Here's my minimal CMakeLists.txt: cmake_minimum_required (VERSION 2.8) project (marmalade) find_package (OpenCV REQUIRED) add_executable (marmalade marmalade.cc) target_link_libraries (marmalade ${OpenCV_LIBS}) I also like to set compiler flags to enforce the C++11 standard, but this is not necessary. In the main .cc file I have: #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" Now you can build the project by just typing cmake . && make in the terminal. Basic shapes First, we'll need an empty canvas. It will be a matrix (cv::Mat) with three unsigned char channels for RGB at Full HD resolution: const cv::Size video_size(1920, 1080); cv::Mat mat_frame = cv::Mat::zeros(video_size, CV_8UC3); This will also initialize everything to zero, i.e. black. Now we can draw our graphics! I had an initial idea of an endless cascade of concentric rings each rotating at a different speed. There might be color and brightness variations as well but otherwise it would stay static the whole time. You can't see a circle's rotation around its center, so we'll add some features to them as well, maybe some kind of bars or spokes. A simplified render method for a ring would look like this: void Ring::RenderTo(cv::Mat& mat_output) const { cv::circle(mat_output, 8 * center_, 8 *