blImagePencilSketch — A quick algorithm to turn an image or video into pencil sketch using opencv

Introduction

This is a very short post showing how we can turn an image or a video into a pencil sketch using the blImageAPI.

The algorithm

The idea is simple and looks as follows:

  1. Read in an image or video frame
  2. Copy the read image
  3. Invert the copied image
  4. Blur the inverted/copied image
  5. Blend the two images together using the “BlendLinearDodge” algorithm from my image blending post

The code

I’ve defined a couple of functions in a file called blImagePencilSketch.hpp which follows:

blImagePencilSketch.hpp (Click to see code...)

Note: You have to be careful about the type of the image sent to the function. I haven’t made the function work for all depths, so the images have to be “unsigned char” or 32-bit “float” but nothing else.

Usage

Note: The functions allow some flexibility in choosing the blur parameters, which have to be odd for the function not to fail

The following snippet shows a quick example where we read frames from a webcam and show them in pencil sketch.

int main(int argc, char *argv[])
{
    // Connect to the webcam
    blCaptureDevice Webcam;
    Webcam.ConnectToWebcam(0);

    // The captured frame
    blImage< blColor3<unsigned char> > Frame;

    // The pencil drawn frame
    blImage< blColor3<unsigned char> > PencilDrawnFrame;

    char key = 0;

    while(key != 'q')
    {
        // Query a frame
        Webcam.QueryFrame(Frame);

        // Turn the queried frame
        // into a pencil sketch
        ImageToPencilSketch(Frame,PencilDrawnFrame);

        // Show both the original and
        // pencil sketch frames
        cvShowImage("Original Frame",Frame);
        cvShowImage("Pencil Sketch Frame",PencilDrawnFrame);

        // Wait for a key input
        key = cvWaitKey(1);
    }
}

The following video shows the resulting output:

Downloads

I have put all the files into a zip file which can be downloaded here. All you have to do is extract it somewhere, let’s say in a directory called blImageAPI, and then include the blImageAPI.hpp file as follows:

#include "blImageAPI/blImageAPI.hpp"
using namespace blImageAPI;

Note:  Everything is declared in a namespace blImageAPI, and such you would use it as: blImageAPI::blImage

[download#1]

Updates

Check back because I’ll be updating the functions to work with all image types

  1. Dec/26/2010 — The function was using CV_GAUSSIAN no matter what SmoothType was being passed to the function. Now, I’ve changed it to using the SmoothType parameter now.

About Vincenzo Barbato

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few.

Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes.

Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration.

His outlook on life:

"Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

About Enzo

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few. Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes. Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration. His outlook on life: "Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

Leave a Reply

Your email address will not be published. Required fields are marked *