Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.42 KB | None | 0 0
  1. void utilities::swapQuads(cv::Mat &img) {
  2.     // Rearranges amplitude image
  3.     int cx = img.cols / 2;
  4.     int cy = img.rows / 2;
  5.     cv::Mat q0(img, cv::Rect(0, 0, cx, cy));   // Top-Left - Create a ROI per quadrant
  6.     cv::Mat q1(img, cv::Rect(cx, 0, cx, cy));  // Top-Right
  7.     cv::Mat q2(img, cv::Rect(0, cy, cx, cy));  // Bottom-Left
  8.     cv::Mat q3(img, cv::Rect(cx, cy, cx, cy)); // Bottom-Right
  9.    
  10.     // swap quadrants (Top-Left with Bottom-Right)
  11.     cv::Mat tmp;                          
  12.     q0.copyTo(tmp);
  13.     q3.copyTo(q0);
  14.     tmp.copyTo(q3);
  15.    
  16.     // swap quadrant (Top-Right with Bottom-Left)
  17.     q1.copyTo(tmp);                    
  18.     q2.copyTo(q1);
  19.     tmp.copyTo(q2);
  20. }
  21.  
  22. void utilities::dft_mag(cv::Mat &src, cv::Mat &complexI, cv::Mat &magI){
  23.     // Creates fourier domain amplitude image
  24.     cv::Mat planes[2];
  25.     double minv, maxv;
  26.     cv::split(complexI, planes);
  27.     cv::magnitude(planes[0], planes[1], magI);
  28.     magI += cv::Scalar::all(1);
  29.     cv::log(magI, magI);
  30.     cv::minMaxLoc(src, &minv, &maxv);
  31.     cv::normalize(magI, magI, minv, maxv, CV_MINMAX);
  32. }
  33.  
  34. cv::Mat utilities::create_mask(Filter filter, int rows, int cols, int d0, int d1, int d2){
  35.     // Dij, centers, and 2 channel mask full of zeros variables
  36.     double di;
  37.     int cx = cols / 2, cy = rows / 2;
  38.     cv::Mat mask(rows, cols, CV_32FC2, cv::Scalar(0));
  39.  
  40.     // Goes through the roi and sets appropriate mask locations for filter to 1
  41.     for(int i = 0; i < rows; i++){
  42.         for(int j = 0; j < cols; j++){
  43.             // Gets distance
  44.             di = sqrt(pow(i-cy, 2) + pow(j-cx, 2));
  45.  
  46.             // Switch for filter
  47.             switch(filter){
  48.                 // Low Pass Filter
  49.                 case lowpass :
  50.                     if(di <= d0){
  51.                         mask.at<cv::Vec2f>(i, j)[0] = 1;
  52.                         mask.at<cv::Vec2f>(i, j)[1] = 1;
  53.                     }
  54.                     break;
  55.  
  56.                 // High Pass Filter
  57.                 case highpass:
  58.                     if(di >= d0){
  59.                         mask.at<cv::Vec2f>(i, j)[0] = 1;
  60.                         mask.at<cv::Vec2f>(i, j)[1] = 1;
  61.                     }
  62.                     break;
  63.  
  64.                 // Notch / Band Stop Filter
  65.                 case notch:
  66.                     if(di <= d1 || di >= d2){
  67.                         mask.at<cv::Vec2f>(i, j)[0] = 1;
  68.                         mask.at<cv::Vec2f>(i, j)[1] = 1;
  69.                     }
  70.                     break;
  71.  
  72.                 // Band Pass Filter
  73.                 case bandpass:
  74.                     if(di >= d1 && di <= d2){
  75.                         mask.at<cv::Vec2f>(i, j)[0] = 1;
  76.                         mask.at<cv::Vec2f>(i, j)[1] = 1;
  77.                     }
  78.                     break;
  79.  
  80.                 // Low Pass and Notch Filters
  81.                 case lpn:
  82.                     if(di <= d0 || (di >= d1 && di <= d2)){
  83.                         mask.at<cv::Vec2f>(i, j)[0] = 1;
  84.                         mask.at<cv::Vec2f>(i, j)[1] = 1;
  85.                     }
  86.                     break;
  87.  
  88.                 // High Pass and Bound Pass Filters
  89.                 case hpbp:
  90.                     if(di >= d0 && (di <= d1 || di >= d2)){
  91.                         mask.at<cv::Vec2f>(i, j)[0] = 1;
  92.                         mask.at<cv::Vec2f>(i, j)[1] = 1;
  93.                     }
  94.                     break;
  95.  
  96.                 // Nothing
  97.                 default : break;
  98.             }
  99.                    
  100.         }
  101.     }
  102.     // Returns mask
  103.     return mask;
  104. }
  105.  
  106. void utilities::cvidft(cv::Mat &tgt, cv::Mat &magI, cv::Mat &complexI){
  107.     // Creates fourier domain amplitude image
  108.     utilities::dft_mag(tgt, complexI, magI);
  109.  
  110.     // Converts back to image, crops padding, and saves roi to target
  111.     utilities::swapQuads(complexI);
  112.     cv::idft(complexI, complexI, cv::DFT_SCALE | cv::DFT_REAL_OUTPUT);
  113.     cv::normalize(complexI, complexI, 0, 255, CV_MINMAX);
  114.     complexI.convertTo(complexI, CV_8U);
  115.     complexI = complexI(cv::Rect(0, 0, tgt.cols, tgt.rows));
  116.     complexI.copyTo(tgt);
  117. }
  118.  
  119. void utilities::cvdft(cv::Mat &src, cv::Mat &magI, cv::Mat &complexI){
  120.     // Gets min/max
  121.     double minv, maxv;
  122.     cv::minMaxLoc(src, &minv, &maxv);
  123.  
  124.     // Pads image if needed
  125.     cv::Mat padded;
  126.     int m = cv::getOptimalDFTSize(src.rows);
  127.     int n = cv::getOptimalDFTSize(src.cols);
  128.     cv::copyMakeBorder(src, padded, 0, m - src.rows, 0, n - src.cols, cv::BORDER_CONSTANT, cv::Scalar::all(0));
  129.  
  130.     // Create planes and run dft
  131.     cv::Mat planes[] = {cv::Mat_<float>(padded), cv::Mat::zeros(padded.size(), CV_32F)};
  132.     cv::merge(planes, 2, complexI);
  133.     cv::dft(complexI, complexI);
  134.  
  135.     // Swaps quadrants
  136.     utilities::swapQuads(complexI);
  137.  
  138.     // Creates fourier domain amplitude image
  139.     utilities::dft_mag(src, complexI, magI);
  140. }
  141.  
  142. void utilities::dft_filter(cv::Mat &tgt, cv::Mat &magbefore, cv::Mat &magafter, int d0, int d1, int d2, Filter filter){
  143.     // Runs dft on src
  144.     cv::Mat complexI;
  145.     utilities::cvdft(tgt, magbefore, complexI);
  146.  
  147.     // Runs low pass filter mask
  148.     cv::Mat mask = utilities::create_mask(filter, complexI.rows, complexI.cols, d0, d1, d2);
  149.  
  150.     // Applies mask to complexI
  151.     cv::multiply(complexI, mask, complexI);
  152.  
  153.     // Inverse DFT
  154.     utilities::cvidft(tgt, magafter, complexI);
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement