# OpenCV学习Laplacian 变换及其运用

1711人阅读 评论(0)

int main(int /*argc*/, char** /*argv*/) {

Mat img, imgLaplacian, imgResult;

//------------------------------------------------------------------------------------------- test, first of all
// now do it by hand
img = (Mat_<uchar>(4, 4) << 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 11, 12, 13, 14, 15);
cout << img << endl;

// first, the good result
/*
src_gray: 输入图像。
dst: 输出图像
ddepth: 输出图像的深度。 因为输入图像的深度是 CV_8U ，这里我们必须定义 ddepth = CV_16S 以避免外溢。
kernel_size: 内部调用的 Sobel算子的内核大小，此例中设置为3。
scale, delta 和 BORDER_DEFAULT: 使用默认值。
*/
//Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT );
Laplacian(img, imgLaplacian, CV_8UC1);
cout << "opencv laplacian" << endl;
cout << imgLaplacian << endl;

Mat kernel = (Mat_<float>(3, 3) <<
0, 1, 0,
1, -4, 1,
0, 1, 0);
int window_size = 3;

// now, reaaallly by hand
// note that, for avoiding padding, the result image will be smaller than the original one.
Mat frame, frame32;
Rect roi;
imgLaplacian = Mat::zeros(img.size(), CV_32F);
for (int y = 0; y<img.rows - window_size / 2 - 1; y++) {
for (int x = 0; x<img.cols - window_size / 2 - 1; x++) {
roi = Rect(x, y, window_size, window_size);
//使用括号运算符，从图像中提取感兴趣区域
frame = img(roi);
frame.convertTo(frame, CV_32F);
frame = frame.mul(kernel);
float v = sum(frame)[0];
imgLaplacian.at<float>(y, x) = v;
}
}
imgLaplacian.convertTo(imgLaplacian, CV_8U);
cout << "dudee" << imgLaplacian << endl;

// a little bit less "by hand"..
// using cv::filter2D
filter2D(img, imgLaplacian, -1, kernel);
cout << imgLaplacian << endl;

//------------------------------------------------------------------------------------------- real stuffs now

// ok, now try different kernel
kernel = (Mat_<float>(3, 3) <<
1, 1, 1,
1, -8, 1,
1, 1, 1); // another approximation of second derivate, more stronger

// do the laplacian filtering as it is
// well, we need to convert everything in something more deeper then CV_8U
// because the kernel has some negative values,
// and we can expect in general to have a Laplacian image with negative values
// BUT a 8bits unsigned int (the one we are working with) can contain values from 0 to 255
// so the possible negative number will be truncated
filter2D(img, imgLaplacian, CV_32F, kernel);
img.convertTo(img, CV_32F);
imgResult = img - imgLaplacian;

// convert back to 8bits gray scale
imgResult.convertTo(imgResult, CV_8U);
imgLaplacian.convertTo(imgLaplacian, CV_8U);

namedWindow("laplacian", CV_WINDOW_AUTOSIZE);
imshow("laplacian", imgLaplacian);

namedWindow("result", CV_WINDOW_AUTOSIZE);
imshow("result", imgResult);

while (true) {
char c = (char)waitKey(10);
if (c == 27) { break; }
}

return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：63709次
• 积分：1042
• 等级：
• 排名：千里之外
• 原创：41篇
• 转载：2篇
• 译文：1篇
• 评论：12条
常用相关网址
YUAN-KAI WANG http://www.ykwang.tw/single-image-defogging.html 雷佳亚 http://www.cse.cuhk.edu.hk/~leojia/index.html 徐立 http://lxu.me Dani Lischinski http://www.cs.huji.ac.il/~danix/ 人在旅途 http://www.cnblogs.com/Imageshop/ 主页代码汇 http://www.csee.wvu.edu/~xinl/source.html CVPapers http://www.cvpapers.com
最新评论