I want to convert an image into pencil sketch in OpenCV

12,920

Solution 1

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.

Solution 2

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

Share:
12,920

Related videos on Youtube

Sayak
Author by

Sayak

Updated on June 04, 2022

Comments

  • Sayak
    Sayak almost 2 years

    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");
    }
    
  • Sayak
    Sayak about 13 years
    Thanks a lot for your reply. I will try to convert it in OpenCV. :)
  • Sayak
    Sayak about 13 years
    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?
  • karlphillip
    karlphillip about 13 years
    @Sayak Updated answer. Read all of it.
  • Sayak
    Sayak about 13 years
    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?
  • karlphillip
    karlphillip about 13 years
    No, there was a problem with your code. It's fixed now, I updated my answer.
  • Sayak
    Sayak about 13 years
    @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...
  • Prakash Vishwakarma
    Prakash Vishwakarma about 10 years
    @s.r.m Could you please tell me how to apply sober filter or edge detection filter
  • Paresh Thakor
    Paresh Thakor about 7 years
    Hello @s.r.m, would you let me know how to achieve pencil sketch effect using GPUImage? I am applying your method but it's not working.
  • Ayush
    Ayush over 2 years
    @karlphillip Those links are broken
  • karlphillip
    karlphillip over 2 years
    @Ayush I am not surprised, they were for websites that existed more than 10 years ago. Happy holydays!

Related