// I need to

ModernizeUpgrade without breaking it RescueFix what someone else started Build NewTurn an idea into a product Reach More PeopleGet found, get used, include everyone

// Company

Case StudiesExpertiseAbout + CareersResourcesContact Book a call →
≡articleBuild New

Teaching Your Computer To See The Easy Way

Get started with computer vision fast: a step-by-step tutorial for adding OpenCV to a Cinder C++ creative coding app, with a sample project on GitHub.

Art+Logic Oct 26, 20162 min read

From the Art+Logic archive · written in 2016. The tools may have moved on; the thinking still holds.

Teaching Your Computer To See The Easy Way

You’re curious about how SnapChat can apply those stamps, you want to create an autonomous controller for your USB controlled Nerf missile launcher, or maybe you have an actual business need. Here is a simple formula to get started with OpenCV so that you can achieve your Computer Vision dreams. Cinder provides a great creative computing framework with a simple pattern of update and draw cycles. It already has support for OpenGL rendering, and it can easily support OpenCV.

I used Cinder 0.9.0, OpenCV 3.0 in this tutorial.

  1. Download Cinder 1b. If you want to take it easy, download my project from Github here.

  2. Create a project using Cinder’s Tinderbox, choose your platform, and select the OpenCV cinderblock.

    Either use the provided OpenCV template…Teaching Your Computer To See The Easy Way

    Or select the OpenCV cinderblock and have TinderBox copy it to your project.

    Teaching Your Computer To See The Easy Way

  3. Modify your app class’ setup() method.This is called one time after the app class is instantiated. We’ll open the computer’s camera, initialize the detector, and create a buffer to draw the frame.

    auto size = this->getWindowSize();
    // Initialize the camera.
    fCaptureDev = Capture::create(size.x, size.y);
    fCaptureDev->start();
    // Set up detector with a HAAR cascade. See Resources folder for other types.
    auto path = cinder::app::Platform::get()->getResourcePath("haarcascade_eye.xml");
    fClassifier = new cv::CascadeClassifier(path.c_str());
    // Prepare a GPU buffer used to draw the captured frame.
    fGlFrame = gl::Texture::create(size.x, size.y);
  4. Modify your app class’ update() method.Do the hard work here such as detecting and tracking. Prepare anything you need so that your drawing method only has to draw.

    if (fCaptureDev->checkNewFrame())
    {
       // Capture the current frame.
       const Surface8uRef rgbFrame = fCaptureDev->getSurface();
       // Find the objects
       // Convert rgb -> gray -> cv::Mat
       cv::Mat ocvGrayImage = toOcv(Channel(*rgbFrame));
       fClassifier->detectMultiScale(ocvGrayImage, fDetections);
       // Get the frame in a format OpenGL can draw, i.e. load it to GPU.
       fGlFrame->update(*rgbFrame);
    }
  5. Modify your app class’ draw() method.Do as little as possible here. It can interfere with user event responsiveness.

    gl::color(1, 1, 1); // restore white color or the image is tinted
    gl::draw(fGlFrame);
    gl::color(0, 1, 0);
    for (const auto& detection : fDetections)
    {
       Rectf bounds(detection.tl().x,
       detection.tl().y,
       detection.br().x,
       detection.br().y);
       gl::drawStrokedCircle(bounds.getCenter(), bounds.getWidth() / 2);
    }

See the OpenCV site for tons of tutorials that will take you much farther than this.

keep going

More to explore in Build New.

All Build New resources →
let's talk

Got a problem like this one?

Tell us what's stuck. A real person replies within one business day.

Talk to our team →

Talk to people who know software, not a sales script.

Your first call is with Aja, our VP of Sales. He'll help make sure you're solving the right problem. Here's what you'll cover together:

  1. What you're trying to accomplish

    The business goal first, then the technology.

  2. Possible approaches

    And the tradeoffs of each, in plain language.

  3. Risks you may not have considered

    Technical and business, before they cost you.

  4. Whether AI changes the math

    What's practical, and what's just hype for your project.

  5. Sensible next steps

    Even if the right next step is not us.

book-a-call.ics

Loading the scheduler…

Open the booking page →

Some problems are easier to explain in writing. Send us a few lines about what you're working on, and we'll reply within one business day. Helpful to include:

  1. What the software does, or should do
  2. What's stuck, or what's worrying you
  3. Any deadline or budget you have in mind
note.md

Loading the form…