4

I am trying to convert an image (from my hard drive) to a pencil sketch in OpenCV. I am using Visual Studio 2010. I came to know the following steps to do this.

  1. invert the image (make negative)
  2. apply Gaussian blur.
  3. blend the above images by a linear dodge or color dodge.

I have done the first 2 steps (very easy). Now I need information about how to do the linear dodge in C.

edited to add…

I have made the following code for the pencil sketch. But does it make a pencil sketch? Please see the result. How can I make it better?

int main( int argc, char** argv )
{
    int col_1, row_1;
    uchar b_1, g_1, r_1, b_2, g_2, r_2, b_d, g_d, r_d;

    IplImage* img = cvLoadImage( "input file");
    IplImage* img1 = cvCreateImage( cvSize( img->width,img->height ), img->depth, img->nChannels);
    IplImage* img2 = cvCreateImage( cvSize( img->width,img->height ), img->depth, img->nChannels);
    IplImage* dst = cvCreateImage( cvSize( img->width,img->height ), img->depth, img->nChannels);
    IplImage* gray= cvCreateImage(cvGetSize(img), img->depth, 1);

    cvNamedWindow("Input", CV_WINDOW_AUTOSIZE );
    cvNamedWindow("Output", CV_WINDOW_AUTOSIZE );

    cvShowImage("Input", img );
    cvNot(img, img1);
    cvSmooth( img1, img2, CV_BLUR, 25,25,0,0);

    for( row_1 = 0; row_1 < img1->height; row_1++ )
    {
        for ( col_1 = 0; col_1 < img1->width; col_1++ )
        {
            b_1 = CV_IMAGE_ELEM( img1, uchar, row_1, col_1 * 3 );
            g_1 = CV_IMAGE_ELEM( img1, uchar, row_1, col_1 * 3 + 1 );
            r_1 = CV_IMAGE_ELEM( img1, uchar, row_1, col_1 * 3 + 2 );

            b_2 = CV_IMAGE_ELEM( img2, uchar, row_1, col_1 * 3 );
            g_2 = CV_IMAGE_ELEM( img2, uchar, row_1, col_1 * 3 + 1 );
            r_2 = CV_IMAGE_ELEM( img2, uchar, row_1, col_1 * 3 + 2 );

            b_d = b_1 + b_2;
            g_d = g_1 + g_2;
            r_d = r_1 + r_2;

            dst->imageData[img1->widthStep * row_1 + col_1* 3] = b_d;
            dst->imageData[img1->widthStep * row_1 + col_1 * 3 + 1] = g_d;
            dst->imageData[img1->widthStep * row_1 + col_1 * 3 + 2] = r_d;
        }
    }
    cvCvtColor(dst, gray, CV_BGR2GRAY);
    cvShowImage("Output", gray );

    cvWaitKey(0);
    cvReleaseImage( &img );
    cvReleaseImage( &gray);
    cvDestroyWindow("Input");
    cvDestroyWindow("Output");
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sayak
  • 339
  • 1
  • 6
  • 19
  • your question is very strange. "Does it make a pencil sketch"? I don't know, does it? What did you get when you ran it? – Mat Apr 18 '11 at 05:42
  • It was not quite a pencil sketch. i cannot get the pencil effect. i have the algorithm.. http://www.barbato.us/2010/12/22/a-quick-algorithm-to-turn-an-image-or-video-into-pencil-sketch-using-opencv/ – Sayak Apr 18 '11 at 05:53
  • @karlphillip.. what did u mean by conversion of unsigned char and signed char? how to achieve that? – Sayak Apr 19 '11 at 02:13
  • It would be very important if you could provide a reference that shows the resulting image of these steps. Or at least point an image that shows the effect you are looking for. – karlphillip Apr 19 '11 at 03:08

2 Answers2

3

I have applied

  1. sobel filter or edge detection filter
  2. invert the above image

I got a pencil sketch image by the above method

j0k
  • 22,600
  • 28
  • 79
  • 90
s.r.m
  • 31
  • 2
2

Ok, you seriously need to take a look at: blImageBlending — Emulating photoshop’s blending modes in opencv. That source code shows exactly how that operation is done. The original developer of the code uses a data structure named blImage, which is a user-defined image data structure based on shared_ptr and IplImage*. You don't need it, of course. But knowing its definition will help you understand the code.

I trust you are capable of converting this code to pure OpenCV.

EDIT:

There were several problems with the code you came up. Anyway, it's fixed now and I simply commented out the problems on your code so you can spot them more easily.

#include <cv.h>
#include <highgui.h>

int main( int argc, char** argv )
{
    int col_1, row_1;
    uchar b_1, g_1, r_1, b_2, g_2, r_2, b_d, g_d, r_d;

    IplImage* img = cvLoadImage("test.png");
    IplImage* img1 = cvCreateImage( cvSize( img->width,img->height ), img->depth, img->nChannels);
    IplImage* img2 = cvCreateImage( cvSize( img->width,img->height ), img->depth, img->nChannels);
    IplImage* dst = cvCreateImage( cvSize( img->width,img->height ), img->depth, img->nChannels);
    IplImage* gray= cvCreateImage(cvGetSize(img), img->depth, 1);

    cvNamedWindow("Input", CV_WINDOW_AUTOSIZE );
    cvNamedWindow("Output", CV_WINDOW_AUTOSIZE );

    cvShowImage("Input", img );
    cvNot(img, img1);
 //   cvSmooth(img1, img2, CV_BLUR, 25,25,0,0);
    cvSmooth(img, img2, CV_GAUSSIAN, 7, 7, 0, 0); // last fix :)

    for( row_1 = 0; row_1 < img1->height; row_1++ )
    {
        for ( col_1 = 0; col_1 < img1->width; col_1++ )
        {
            b_1 = CV_IMAGE_ELEM( img1, uchar, row_1, col_1 * 3 );
            g_1 = CV_IMAGE_ELEM( img1, uchar, row_1, col_1 * 3 + 1 );
            r_1 = CV_IMAGE_ELEM( img1, uchar, row_1, col_1 * 3 + 2 );

            b_2 = CV_IMAGE_ELEM( img2, uchar, row_1, col_1 * 3 );
            g_2 = CV_IMAGE_ELEM( img2, uchar, row_1, col_1 * 3 + 1 );
            r_2 = CV_IMAGE_ELEM( img2, uchar, row_1, col_1 * 3 + 2 );

//            b_d = b_1 + b_2;
//            g_d = g_1 + g_2;
//            r_d = r_1 + r_2;

            b_d = std::min(255, b_1 + b_2);
            g_d = std::min(255, g_1 + g_2);
            r_d = std::min(255, r_1 + r_2);

            dst->imageData[img1->widthStep * row_1 + col_1* 3] = b_d;
            dst->imageData[img1->widthStep * row_1 + col_1 * 3 + 1] = g_d;
            dst->imageData[img1->widthStep * row_1 + col_1 * 3 + 2] = r_d;
        }
    }
   cvCvtColor(dst, gray, CV_BGR2GRAY);
   cvShowImage("Output", gray );

   cvWaitKey(0);
   cvReleaseImage( &img );
   cvReleaseImage( &img1 ); // Yes, you must release all the allocated memory.
   cvReleaseImage( &img2 );
   cvReleaseImage( &dst );
   cvReleaseImage( &gray);
   cvDestroyWindow("Input");
   cvDestroyWindow("Output");
}

EDIT:

I made a small change to the code to fix the last problem. You were not following the steps:

  • invert the image (make negative)
  • apply Gaussian Blur
  • blend the above images by linear dodge or color dodge

The negative image must be completely isolated from the Gaussian blur. These operations result in 2 different images, and they both need to be combined/blended by linear dodge. You were executing the Gaussian blur on the negative image, and that was your mistake. I believe it's fixed.

karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • Thanks a lot for your reply. I will try to convert it in OpenCV. :) – Sayak Apr 16 '11 at 01:50
  • i am done with the algorithm. you can see the code how i developed the algorithm. pls let me know why i cant get the exact pencil sketch effect? – Sayak Apr 18 '11 at 06:07
  • @Sayak Updated answer. Read all of it. – karlphillip Apr 19 '11 at 17:09
  • hi Karl, I have applied the changes you made in the code. but it still doesnt work. maybe the test image that you have selected shows a pencil sketch effect. but i have tried with load of portraits and landscapes, it doesnt work. maybe i can call it a "ghost" effect :) the negative effect is too much in some portraits. is there anything wrong in the linear dodge blending algorithm? – Sayak Apr 20 '11 at 02:51
  • No, there was a problem with your code. It's fixed now, I updated my answer. – karlphillip Apr 20 '11 at 03:30
  • @Karl.. i got it. the effect is likely to be pencil sketch. but it was not the effect that i was thinking about. maybe i will send you an image. then you can understand. anyway, thanks a ton... – Sayak Apr 20 '11 at 08:31
  • @karlphillip Those links are broken – Ayush Nov 21 '21 at 07:47
  • @Ayush I am not surprised, they were for websites that existed more than 10 years ago. Happy holydays! – karlphillip Nov 22 '21 at 03:22