CG - OpenCV Laplacian and Sobel Filters Implementation


/ Published in: C++
Save to your folder(s)

OpenCV Laplacian and Sobel Filters Implementation


Copy this code and paste it in your HTML
  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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.