OpenCV is a very powerful open-source library for computer vision. The library is written in C++ so it can be easily integrated in your Cinder application. There is a very useful OpenCV Cinder block provided within Cinder package available at the GitHub repository (https://github.com/cinder/Cinder-OpenCV).
We will add OpenCV Cinder block to your project, which also illustrates the usual way of adding any other Cinder block to your project. Perform the following steps to do so:
Blocks.
Next, drag the opencv
folder inside the Blocks
group. Be sure to select the Create groups for any added folders radio button, as shown in the following screenshot:include
folder inside the opencv
folder in your project structure, so delete any reference to others. The final project structure should look like the following screenshot:$(CINDER_PATH)/blocks/opencv/lib/macosx/libopencv_imgproc.a $(CINDER_PATH)/blocks/opencv/lib/macosx/libopencv_core.a $(CINDER_PATH)/blocks/opencv/lib/macosx/libopencv_objdetect.a
These paths are shown in the following screenshot:
$(CINDER_PATH)/blocks/opencv/include
This path is shown in the following screenshot:
#include "CinderOpenCV.h"
OpenCV Cinder block provides the toOcv
and fromOcv
functions for data exchange between Cinder and OpenCV. After setting up your project you can use them, as shown in the following short example:
Surface mImage, mImageOutput; mImage = loadImage( loadAsset("image.png") ); cv::Mat ocvImage(toOcv(mImage)); cv::cvtColor(ocvImage, ocvImage, CV_BGR2GRAY ); mImageOutput = Surface(fromOcv(ocvImage));
You can use the toOcv
and fromOcv
functions to convert between Cinder and OpenCV types, storing image data such as Surface
or Channel
handled through the ImageSourceRef
type; there are also other types, as shown in the following table:
Cinder types |
OpenCV types |
---|---|
|
|
|
|
|
|
|
|
|
|
In this example we are linking against the following three files from the OpenCV package:
You can find the documentation on all OpenCV modules at http://docs.opencv.org/index.html.
3.22.71.220