我正试图将图像(从我的硬盘驱动器)转换为OpenCV中的铅笔草图。我使用的是Visual Studio 2010。我知道了下面的步骤来做这件事。
- 反转图像(使其为负值)
- 应用高斯模糊 通过线性减淡或颜色减淡混合以上图像。
我已经完成了前两个步骤(非常简单)。现在我需要关于如何在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");
}
我已经申请了
- 索贝尔滤波器或边缘检测滤波器
- 倒转上图
我用上面的方法得到了一个铅笔素描图像
好的,你真的需要看看:blImageBlending -在openv中模拟photoshop的混合模式。源代码准确地显示了该操作是如何完成的。代码的原始开发人员使用名为blImage的数据结构,这是基于shared_ptr
和IplImage*
的用户定义的图像数据结构。当然,你不需要它。但是知道它的定义将帮助您理解代码。
我相信你有能力将这段代码转换为纯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个不同的图像,它们都需要通过线性减淡进行组合/混合。你在底片上执行高斯模糊,这是你的错误。