Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
530 views
in Technique[技术] by (71.8m points)

c - I want to convert an image into pencil sketch in OpenCV

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");
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...