• Stars
    star
    655
  • Rank 68,333 (Top 2 %)
  • Language
    C++
  • License
    Other
  • Created over 13 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Alternative approach to interfacing with OpenCv from openFrameworks.

Introduction

ofxCv represents an alternative approach to wrapping OpenCV for openFrameworks.

Installation

First, pick the branch that matches your version of openFrameworks:

Either clone out the source code using git:

> cd openFrameworks/addons/
> git clone https://github.com/kylemcdonald/ofxCv.git

Or download the source from GitHub here, unzip the folder, rename it from ofxCv-master to ofxCv and place it in your openFrameworks/addons folder.

To run the examples, import them into the project generator, create a new project, and open the project file in your IDE.

Goals

ofxCv has a few goals driving its development.

Wrap complex things in a helpful way

Sometimes this means: providing wrapper functions that require fewer arguments than the real CV functions, providing a smart interface that handles dynamic memory allocation to make things faster for you, or providing in place and out of place alternatives.

Present the power of OpenCv clearly

This means naming things in an intuitive way, and, more importantly, providing classes that have methods that transform the data represented by that class. It also means providing demos of CV functions, and generally being more useful than ofxOpenCv.

Interoperability of openFrameworks and OpenCv

Making it easy to work directly with CV by providing lightweight conversion functions, and providing wrappers for CV functions that do the conversions for you.

Elegant internal OpenCv code

Provide clean implementations of all functions in order to provide a stepping stone to direct OpenCV use. This means using function names and variable names that follow the OpenCV documentation, and spending the time to learn proper CV usage so I can explain it clearly to others through code. Sometimes there will be heavy templating in order to make OF interoperable with OpenCV, but this should be avoided in favor of using straight OpenCV as often as possible.

Usage

Sometimes this readme will fall out of date. Please refer to the examples as the primary reference in that case.

Project setup

Using ofxCv requires:

  • ofxCv/libs/ofxCv/include/ Which contains all the ofxCv headers.
  • ofxCv/libs/ofxCv/src/ Which contains all the ofxCv source.
  • ofxCv/src/ Which ties together all of ofxCv into a single include.
  • opencv/include/ The OpenCv headers, located in addons/ofxOpenCv/
  • opencv/lib/ The precompiled static OpenCv libraries, located in addons/ofxOpenCv/

Your linker will also need to know where the OpenCv headers are. In XCode this means modifying one line in Project.xconfig:

HEADER_SEARCH_PATHS = $(OF_CORE_HEADERS) "../../../addons/ofxOpenCv/libs/opencv/include/" "../../../addons/ofxCv/libs/ofxCv/include/"

Alternatively, I recommend using OFXCodeMenu to add ofxCv to your project.

Including ofxCv

Inside your ofApp.h you will need one include:

#include "ofxCv.h"

OpenCv uses the cv namespace, and ofxCv uses the ofxCv namespace. You can automatically import them by writing this in your .cpp files:

using namespace cv;
using namespace ofxCv;

If you look inside the ofxCv source, you'll find lots of cases of ofxCv:: and cv::. In some rare cases, you'll need to write cv:: in your code. For example, on OSX Rect and Point are defined by OpenCv, but also MacTypes.h. So if you're using an OpenCv Rect or Point you'll need to say so explicitly with cv::Rect or cv::Point to disambiguate.

ofxCv takes advantage of namespaces by using overloaded function names. This means that the ofxCv wrapper for cv::Canny() is also called ofxCv::Canny(). If you write simply Canny(), the correct function will be chosen based on the arguments you pass.

Working with ofxCv

Unlike ofxOpenCv, ofxCv encourages you to use either native openFrameworks types or native OpenCv types, rather than introducing a third type like ofxCvImage. To work with OF and OpenCv types in a fluid way, ofxCv includes the toCv() and toOf() functions. They provide the ability to convert openFrameworks data to OpenCv data and vice versa. For large data, like images, this is done by wrapping the data rather than copying it. For small data, like vectors, this is done by copying the data.

The rest of ofxCv is mostly helper functions (for example, threshold()) and wrapper classes (for example, Calibration).

toCv() and copy()

toCv() is used to convert openFrameworks data to OpenCv data. For example:

ofImage img;
img.load("image.png");
Mat imgMat = toCv(img);

This creates a wrapper for img called imgMat. To create a deep copy, use clone():

Mat imgMatClone = toCv(img).clone();

Or copy(), which works with any type supported by toCv():

Mat imgCopy;
copy(img, imgCopy);

toCv() is similar to ofxOpenCv's ofxCvImage::getCvImage() method, which returns an IplImage*. The biggest difference is that you can't always use toCv() "in place" when calling OpenCv code directly. In other words, you can always write this:

Mat imgMat = toCv(img);
cv::someFunction(imgMat, ...);

But you should avoid using toCv() like this:

cv::someFunction(toCv(img), ...);

Because there are cases where in place usage will cause a compile error. More specifically, calling toCv() in place will fail if the function requires a non-const reference for that parameter.

imitate()

imitate() is primarily used internally by ofxCv. When doing CV, you regularly want to allocate multiple buffers of similar dimensions and channels. imitate() follows a kind of prototype pattern, where you pass a prototype image original and the image to be allocated mirror to imitate(mirror, original). imitate() has two big advantages:

  • It works with Mat, ofImage, ofPixels, ofVideoGrabber, and anything else that extends ofBaseHasPixels.
  • It will only reallocate memory if necessary. This means it can be used liberally.

If you are writing a function that returns data, the ofxCv style is to call imitate() on the data to be returned from inside the function, allocating it as necessary.

drawMat() vs. toOf()

Sometimes you want to draw a Mat to the screen directly, as quickly and easily as possible, and drawMat() will do this for you. drawMat() is not the most optimal way of drawing images to the screen, because it creates a texture every time it draws. If you want to draw things efficiently, you should allocate a texture using ofImage img; once and draw it using img.draw().

  1. Either use Mat mat = toCv(img); to treat the ofImage as a Mat, modify the mat, then img.update() to upload the modified pixels to the GPU.
  2. Alternatively; call toOf(mat, img) each time after modifying the Mat. This will only reallocate the texture if necessary, e.g. when the size has changed.

Working with OpenCv 2

OpenCv 2 is an incredibly well designed API, and ofxCv encourages you to use it directly. Here are some hints on using OpenCv.

OpenCv Types

OpenCv 2 uses the Mat class in place of the old IplImage. Memory allocation, copying, and deallocation are all handled automatically. operator= is a shallow, reference-counted copy. A Mat contains a collection of Scalar objects. A Scalar contains a collection of basic types (unsigned char, bool, double, etc.). Scalar is a short vector for representing color or other multidimensional information. The hierarchy is: Mat contains Scalar, Scalar contains basic types.

Different functions accept Mat in different ways:

  • Mat will create a lightweight copy of the underlying data. It's easy to write, and it allows you to use toCv() "in-place" when passing arguments to the function.
  • Mat& allows the function to modify the header passed in. This means the function can allocate if necessary.
  • const Mat& means that the function isn't going to modify the underlying data. This should be used instead of Mat when possible. It also allows "in-place" toCv() usage.

Mat creation

If you're working with Mat directly, it's important to remember that OpenCv talks about rows and cols rather than width and height. This means that the arguments are "backwards" when they appear in the Mat constructor. Here's an example of creating a Mat wrapper for some grayscale unsigned char* pixels for which we know the width and height:

Mat mat = Mat(height, width, CV_8UC1, pixels, 0);

Mat operations

Basic mathematical operations on Mat objects of the same size and type can be accomplished with matrix expressions. Matrix expressions are a collection of overloaded operators that accept Mat, Scalar, and basic types. A normal mathematical operation might look like:

float x, a, b;
...
x = (a + b) * 10;

A matrix operation looks similar:

Mat x, a, b;
...
x = (a + b) * 10;

This will add every element of a and b, then multiply the results by 10, and finally assign the result to x.

Available matrix expressions include mathematical operators +, -, / (per element division), * (matrix multiplication), .mul() (per-element multiplication). As well as comparison operators !=, ==, <, >, >=, <= (useful for thresholding). Binary operators &, |, ^, ~. And a few others like abs(), min(), and max(). For the complete listing see the OpenCv documention or mat.hpp.

Code Style

ofxCv tries to have a consistent code style. It's most similar to the K&R variant used for Java, and the indentation is primarily determined by XCode's auto-indent feature.

Multiline comments are used for anything beyond two lines.

Case statements have a default: fall-through with the last case.

When two or three similar variables are initialized, commas are used instead of multiple lines. For example Mat srcMat = toCv(src), dstMat = toCv(dst);. This style was inherited from reading Jason Saragih's FaceTracker.


ofxCv was developed with support from Yamaguchi Center for Arts and Media.

More Repositories

1

FreeWifi

How to get free wifi.
Python
2,870
star
2

ofxFaceTracker

CLM face tracking addon for openFrameworks based on Jason Saragih's FaceTracker.
C++
1,383
star
3

FaceTracker

Real time deformable face tracking in C++ with OpenCV 3.
C++
996
star
4

AudioNotebooks

Collection of notebooks and scripts related to audio processing and machine learning.
Jupyter Notebook
422
star
5

Parametric-t-SNE

Running parametric t-SNE by Laurens Van Der Maaten with Octave and oct2py.
Jupyter Notebook
264
star
6

AppropriatingNewTechnologies

A half-semester class at ITP.
C++
252
star
7

cv-examples

A collection of computer vision examples in JavaScript for the browser.
JavaScript
237
star
8

Coloring-t-SNE

Exploration of methods for coloring t-SNE.
Jupyter Notebook
220
star
9

ethereum-nft-activity

Estimate the total emissions for popular CryptoArt platforms.
Jupyter Notebook
183
star
10

ml-notebook

Dockerfile for multiple machine learning tools.
Shell
162
star
11

ofxFft

FFT addon for openFrameworks that wrapps FFTW and KissFFT.
C++
139
star
12

SmileCNN

Smile detection with a deep convolutional neural net, with Keras.
Jupyter Notebook
138
star
13

ofxCcv

libccv addon for openFrameworks
C
123
star
14

ofxEdsdk

Interfacing with Canon cameras from openFrameworks for OSX. An alternative to ofxCanon and CanonCameraWrapper.
C++
111
star
15

nvidia-co2

Adds gCO2eq emissions to nvidia-smi.
Python
110
star
16

OpenFit

Open source jeans.
Processing
109
star
17

ml-examples

Examples of machine learning, with an emphasis on deep learning.
Jupyter Notebook
109
star
18

CloudToGrid

Example of converting a 2d point cloud to a 2d grid via the assignment problem.
Jupyter Notebook
96
star
19

python-utils

Disorganized collection of useful functions for working with audio and images, especially in the context of machine learning.
Python
93
star
20

LightLeaks

An immersive installation built from a pile of mirror balls and a few projectors.
Jupyter Notebook
92
star
21

openFrameworksDemos

Collection of assorted demos and examples for openFrameworks that don't fit anywhere else.
C++
92
star
22

Makerbot

Experiments and projects while in residence at Makerbot Industries.
C++
91
star
23

gpt-2-poetry

Generating poetry with GPT-2.
Jupyter Notebook
89
star
24

ofxDmx

DMX Pro wrapper for openFrameworks
C++
83
star
25

ofxBlackmagic

Simplified and optimized Black Magic DeckLink SDK grabber.
C++
79
star
26

ethereum-emissions

Estimating the daily energy usage for Ethereum.
Jupyter Notebook
75
star
27

ofxBlur

A very fast, configurable GPU blur addon that can also simulate bloom and different kernel shapes.
C++
64
star
28

ofxAssignment

A tool for matching point clouds or other kinds of data. Useful for making grids from point clouds.
C++
62
star
29

ExhaustingACrowd

JavaScript
53
star
30

SharingFaces

C++
48
star
31

COVIDPause

Chrome extension for pausing all mentions of COVID-19.
JavaScript
45
star
32

SharingInterviews

A collection of interviews about creators sharing work, with an emphasis on open source, media art, and digital communities.
44
star
33

i2i-realtime

Python
44
star
34

ofxFaceShift

Network-based addon for interfacing with FaceShift Studio from openFrameworks.
C++
39
star
35

KernelizedSorting

Mirror of Kernelized Sorting code by Novi Quadrianto.
Python
39
star
36

BlindSelfPortrait

An interactive installation that guides your hand to draw a self portrait.
Jupyter Notebook
38
star
37

ImageRearranger

Rearrange mosaics by similarity.
Jupyter Notebook
37
star
38

ofxCameraFilter

A one-shot effect for simulating: vignetting, lens distortion, chromatic aberration, blur/bloom, and noise grain.
C++
36
star
39

ofxTesseract

tesseract-ocr wrapper for openFrameworks
C++
33
star
40

arxiv-visual-summary

Tool for extracting a visual summary of new papers uploaded to ArXiv.
HTML
33
star
41

EmbeddingScripts

Collection of scripts for visualizing high dimensional data with scikit-learn and bh_tsne
Python
32
star
42

ofxFaceTracker-iOS

Example of using ofxFaceTracker on iOS.
Objective-C++
31
star
43

ofxTiming

Timing utilities for handling recurring events, fading, framerate counting.
C++
31
star
44

ofxLibdc

Open Frameworks wrapper for libdc1394.
C
30
star
45

ofxVirtualKinect

Creates a virtual kinect depth image from an arbitrary position and orientation, using ofxKinect.
C++
30
star
46

mueller-unredacter

Generating text completions based on the Mueller report
HTML
28
star
47

whopaysartists

EJS
27
star
48

ofxAudioDecoder

An openFrameworks addon for m4a/aac, mp3, wav, and other file loading.
C++
27
star
49

ofxAutostereogram

Small library for producing autostereograms, as popularized by the "Magic Eye" book series.
C++
27
star
50

covid-mobility-data

Simple script for digitizing the plots in .pdf files from Google's "Community Mobile Reports".
Python
27
star
51

3dsav

Code for 3d Sensing and Visualization class.
C++
25
star
52

ofxZxing

openFrameworks wrapper of ZXing for detecting and decoding QR Codes in real time.
C++
23
star
53

structured-light

Automatically exported from code.google.com/p/structured-light
C++
21
star
54

Messages

Endless Bytebeat synthesis. Generative shader code for audio and visuals.
C++
21
star
55

Eyeshine

C
21
star
56

SoundParts

Collection of classes for working with sound in C++.
C++
21
star
57

ofxLaunchpad

Interface for Novation Launchpad MIDI controller.
C++
19
star
58

MultiscaleTuring

An implementation of multiscale turing patterns with openFrameworks and OpenCV.
C++
18
star
59

reverse-tunnel

Make a reverse tunnel from OSX to a Linux machine.
Python
18
star
60

facepp

Face tracking and augmentation: a collaboration between Zach Lieberman, Daito Manabe, and Kyle McDonald.
C++
18
star
61

ofxPathfinder

Small and efficient A* pathfinding addon for openFrameworks, supporting variable terrain costs.
C++
17
star
62

prnetjs

Port of PRNet face analysis tool to JavaScript using TensorFlow.js
HTML
17
star
63

socialroulette.net

PHP
16
star
64

ofxMetaballs

Metaballs implementations for openFrameworks using marching cubes and marching tetrahedrons.
C++
16
star
65

sakoku-explorer

Explore your data from Facebook and Google.
Svelte
16
star
66

FisheyeToEquirectangular

Scripts for converting pairs of Hikvision fisheye videos to equirectangular videos.
Python
15
star
67

ofxHeadPoseEstimator

openFrameworks example using ofxKinect to demonstrate research from Gabriele Fanelli.
C++
15
star
68

Transcranial

Interactive dance performance with Klaus Obermaier and Daito Manabe.
Max
14
star
69

ScreenLab

ScreenLab 0x02 residency with Joanie Lemercier.
C++
14
star
70

ableton-web-sync

JavaScript
14
star
71

prores-raw-export

Objective-C
13
star
72

ofxBvh

openFrameworks addon for parsing, rendering, manipulating and saving BVH files.
C++
13
star
73

ofxConnexion

Wraps 3dConnexionClient for openFrameworks on OSX
C++
13
star
74

ofxCurvesTool

An interface for controlling a 1D cubic spline, continuously evaluated and stored in a lookup table.
C++
13
star
75

DohaInstallation

Multi-monitor interactive installation for Wafaa Bilal's 3rdi.
C++
12
star
76

DigitalInteraction

Code related to the FITC 2013 "Digital Interaction" workshop with Daito Manabe.
C++
11
star
77

Barneys

Work on a custom 4m sculpture designed to scatter light in every direction.
JavaScript
11
star
78

BaristaBot

BaristaBot draws your portrait in your latte.
C++
11
star
79

UVCExample

Example of using libuvc with openFrameworks on Mac.
C
10
star
80

HowWeActTogether-Tracking

Facetracking for How We Act Together.
JavaScript
10
star
81

t-SNEPreprocessingComparison

Comparison of two techniques for pre-processing data for t-SNE (PCA and convolutional autoencoder).
Jupyter Notebook
10
star
82

tSNESearch

Example of loading t-SNE organized sounds into openFrameworks.
C++
9
star
83

Serendipity

A visualization: every second a few people hit "play" on the same Spotify track.
JavaScript
9
star
84

Roseheading

Endless glitch facets of a "fractured, frozen" mosaic, our data in the cloud.
Java
9
star
85

TheJanusMachine

C++
8
star
86

PhotoMosaic

PhotoMosaic app that loads from a folder of images and regularly transitions.
C++
8
star
87

3dCalibration

Tools for calibrating 3d cameras to 2d cameras using openFrameworks.
C++
8
star
88

AndyWarholMachine

Interactive installation for "Andy Warhol: Manufactured" at the Anchorage Museum.
C++
8
star
89

ofxVCGLib

VCG for OF: based on work from Akira-Hayasaka, wrapping the VCG library for OF friendliness
C
8
star
90

ofxVicon

Wrapper for interfacing to the Vicon motion capture system with openFrameworks.
C++
8
star
91

AppleStore

PHP
7
star
92

GoingPublic

Tweets anything sent via direct message that is prefixed with a ~ (tilde).
PHP
7
star
93

Highsight

Cam on wire.
C++
7
star
94

CameraHacking

Processing sketches for an analog+digital camera hacking workshop with Chris Woebken.
Java
7
star
95

facework

Facework
TypeScript
6
star
96

express-photobooth

Example of a basic photobooth with Express, getUserMedia, and canvas-to-blob.
JavaScript
6
star
97

SubdivisionOfRoam

Installation for Chris Milk, in collaboration with Golan Levin and Emily Gobeille.
C++
6
star
98

HappyThings

A background app that automatically posts a screenshot every time you smile.
PHP
6
star
99

kylemcdonald.net

Repository for my website: things that can't be hosted elsewhere.
HTML
6
star
100

everyautocomplete

Get every autocomplete result.
HTML
6
star