2

I would like to know where I can find 3 good algorithms, or 3 examples of C code to :

  • apply a sketch effect on an image (black and white)
  • apply a sketch effect on an image (color)
  • apply a cartoon effect on an image (color)

I already have a code for the black and white sketch, but it's too white..

void sketchFilter(int *pixels, int width, int height) {
changeImageToGray(pixels, width, height);

int *tempPixels = new int[width * height];
memcpy(tempPixels, pixels, width * height * sizeof(int));

int threshold = 7;
float num = 8;
for (int i = 1; i < height - 1; i++) {
    for (int j = 1; j < width - 1; j++) {
        Color centerColor(tempPixels[i * width + j]);
        int centerGray = centerColor.R();

        Color rightBottomColor(tempPixels[(i + 1) * width + j + 1]);
        int rightBottomGray = rightBottomColor.R();
        if (abs(centerGray - rightBottomGray) >= threshold) {
            pixels[i * width + j] = RGB2Color(0, 0, 0); // black
        }
        else {
            pixels[i * width + j] = RGB2Color(255, 255, 255); // white
        }
    }
}

delete [] tempPixels;
}

Any way to improve this code, or should I go with a completely different? How can I do both color cartoon (doodle?) and color sketch? Thanks!

timrau
  • 22,578
  • 4
  • 51
  • 64
Alex
  • 165
  • 2
  • 10

1 Answers1

2

The cartoon-like style is described in this page; it can be achieved via a smoothing followed by a(n aggressive) posterize effect. Both can be carried out using OpenCV, using any filter for the smotthing and PyrMeanShiftFiltering for the posterize.

Edit: The pencil (colour) sketch is described f.e. in this StackOverflow question.

Community
  • 1
  • 1
miguelao
  • 772
  • 5
  • 12
  • Great! Thanks I'll follow this tutorial. Any idea how I can improve my B&W Sketch? Do you know where I can find a tutorial for a color sketch? – Alex Apr 02 '14 at 17:46
  • Added a link for the pencil sketch. Some visual description of the process and steps here: http://www.photoshopessentials.com/photo-effects/portrait-to-sketch. – miguelao Apr 02 '14 at 19:02