OpenCV-Laplacian和Sobel滤波器的实现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenCV-Laplacian和Sobel滤波器的实现相关的知识,希望对你有一定的参考价值。

OpenCV Laplacian and Sobel Filters Implementation
  1. #pragma region Laplacian Sobel Filters
  2.  
  3.  
  4. Mat LaplacianFilter(Mat img, int level){
  5.  
  6. namedWindow("Original image", CV_WINDOW_AUTOSIZE);
  7. imshow("Original image", img);
  8.  
  9. Mat gray,draw,draw2;
  10.  
  11. cvtColor(img, gray, CV_RGB2GRAY);
  12.  
  13. /// Apply Laplace function
  14. Laplacian(gray, draw, CV_16S, 3, 1, 0, BORDER_DEFAULT);
  15. convertScaleAbs(draw, draw2);
  16.  
  17. imwrite("../../result.jpg", draw2);
  18. namedWindow("Result image", CV_WINDOW_AUTOSIZE);
  19. imshow("Result image", draw2);
  20. waitKey(0);
  21.  
  22. destroyWindow("Result image");
  23. destroyWindow("Original image");
  24.  
  25. return draw;
  26. }
  27.  
  28. Mat SobelFilter(Mat img, int level){
  29.  
  30. namedWindow("Original image", CV_WINDOW_AUTOSIZE);
  31. imshow("Original image", img);
  32.  
  33. Mat grey;
  34. cvtColor(img, grey, CV_BGR2GRAY);
  35.  
  36. Mat sobelx;
  37. Sobel(grey, sobelx, CV_32F, 1, 0);
  38.  
  39. double minVal, maxVal;
  40. minMaxLoc(sobelx, &minVal, &maxVal); //find minimum and maximum intensities
  41. cout << "minVal : " << minVal << endl << "maxVal : " << maxVal << endl;
  42.  
  43. Mat draw;
  44. sobelx.convertTo(draw, CV_8U, 255.0 / (maxVal - minVal), -minVal * 255.0 / (maxVal - minVal));
  45.  
  46. imwrite("../../result.jpg", draw);
  47. namedWindow("Result image", CV_WINDOW_AUTOSIZE);
  48. imshow("Result image", draw);
  49. waitKey(0);
  50.  
  51. destroyWindow("Result image");
  52. destroyWindow("Original image");
  53.  
  54. return draw;
  55. }
  56.  
  57. #pragma endregion

以上是关于OpenCV-Laplacian和Sobel滤波器的实现的主要内容,如果未能解决你的问题,请参考以下文章

来自opencv和Matlab的Sobel滤波器输出不同

对于大于 3 x 3 的尺寸,OpenCV 的 Sobel 滤波器的内核系数是多少?

学习 opencv---(11)OpenC 边缘检测:Canny算子,Sobel算子,Laplace算子,Scharr滤波器

图像梯度

实现Sobel算子滤波Robers算子滤波Laplace算子滤波

sobel算子原理与实现