开发者

I want to convert an image into pencil sketch in OpenCV

开发者 https://www.devze.com 2023-02-26 00:50 出处:网络
I am trying to 开发者_StackOverflow社区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.

I am trying to 开发者_StackOverflow社区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");
}


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消