我想在OpenCV中将图像转换为铅笔草图

时间:2011-04-15 10:02:39

标签: c opencv

我正在尝试将图片(从我的硬盘驱动器)转换为OpenCV中的铅笔草图。我正在使用Visual Studio 2010.我开始了解以下步骤。

  1. 反转图像(制作否定)
  2. 申请Gaussian blur
  3. 通过线性躲闪或颜色躲闪来混合上面的图像。
  4. 我已完成前两个步骤(非常简单)。现在我需要有关如何在C中进行线性闪避的信息。

    编辑

    以添加...

    我为铅笔素描制作了以下代码。但它是否会制作铅笔素描?请看结果。我怎样才能让它变得更好?

    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");
    }
    

2 个答案:

答案 0 :(得分:3)

我申请了

  1. sobel滤波器或边缘检测滤波器
  2. 反转上面的图片
  3. 我通过上述方法获得了铅笔素描图像

答案 1 :(得分:2)

好的,你真的需要看看:blImageBlending — Emulating photoshop’s blending modes in opencv。该源代码确切地显示了该操作是如何完成的。该代码的原始开发人员使用data structure named blImage,这是基于shared_ptrIplImage*的用户定义的图像数据结构。当然,你不需要它。但了解其定义将有助于您理解代码。

我相信您能够将此代码转换为纯OpenCV。

修改

您提出的代码存在一些问题。无论如何,它已经修复了,我只是在你的代码上注释了问题,这样你就可以更容易地发现它们。

#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");
}

修改

我对代码进行了一些小修改以修复最后一个问题。你没有遵循这些步骤:

  • 反转图像(制作否定)
  • 应用高斯模糊
  • 通过线性闪避或颜色闪避来混合上述图像

负像必须与高斯模糊完全隔离。这些操作产生2个不同的图像,并且它们都需要通过线性闪避进行组合/混合。你在负片上执行高斯模糊,那是你的错。我相信它是固定的。