AlessioMaddaluno

Untitled

Oct 26th, 2020
864
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Algorithm steps:
  3.     - Reduce Noise with a Gaussian Blur
  4.     - Apply Laplacian Operator
  5.     - Find Zero Cross points
  6. */
  7.  
  8. #include <opencv2\opencv.hpp>
  9. #include <iostream>
  10.  
  11. using namespace std;
  12. using namespace cv;
  13.  
  14. Mat zeroCrossing(Mat &src);
  15.  
  16. int main(int argc, char **argv){
  17.  
  18.     if(argc < 4){
  19.         cerr << "usage: " <<argv[0] <<"[image-name] [filter-size] [sigma]" <<endl;
  20.         return 1;
  21.     }
  22.  
  23.     Mat img = imread(samples::findFile(argv[1]), IMREAD_GRAYSCALE);
  24.     if(img.empty()){
  25.         cerr << "[x] Invalid image" <<endl;
  26.         return 1;
  27.     }
  28.  
  29.     int ksize = atoi(argv[2]);
  30.     int sigma = atoi(argv[3]);
  31.  
  32.     Mat image_noiseFiltered;
  33.     filter2D(img,image_noiseFiltered,CV_32F,getGaussianKernel(ksize,sigma));
  34.  
  35.     Mat image_laplacianFiltered;
  36.     Laplacian(image_noiseFiltered,image_laplacianFiltered,CV_32F,ksize);
  37.  
  38.     Mat image_zerocrossed = zeroCrossing(image_laplacianFiltered);
  39.  
  40.     imshow("Origianl Image", img);
  41.     imshow("Output", image_zerocrossed);
  42.  
  43.     waitKey(0);
  44.  
  45.     return 0;
  46. }
  47.  
  48. Mat zeroCrossing(Mat &src){
  49.  
  50.     double th,max,min;
  51.     Mat dst = Mat::zeros(src.rows,src.cols,CV_8U);
  52.     minMaxLoc(src,nullptr,&th);
  53.     th*=.04;
  54.    
  55.     for(int i=1;i<src.rows-1;i++){
  56.         for(int j=1;j<src.cols-1;j++){
  57.            
  58.             Mat neigh = src(Rect(j-1,i-1,2,2));
  59.             minMaxLoc(neigh,&min,&max);
  60.  
  61.             bool flag;
  62.             if(src.at<float>(i,j) > 0)
  63.                 flag = min<0 ?true:false;
  64.             else
  65.                 flag = max>0 ?true:false;
  66.  
  67.             if(max-min>th && flag)
  68.                 dst.at<uchar>(i,j) = 255;    
  69.            
  70.         }
  71.     }
  72.  
  73.     return dst;
  74. }
RAW Paste Data