Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.58 KB | None | 0 0
  1. #include "tpMorphology.h"
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <tuple>
  5. #include<vector>
  6. #include <limits>
  7. #include "common.h"
  8. using namespace cv;
  9. using namespace std;
  10.  
  11.  
  12. bool isInPicture(Mat image, int x, int y)
  13. {
  14.     if(x >= 0 && y >= 0 && x < image.rows && y < image.cols)
  15.         return true;
  16.     return false;
  17. }
  18. /**
  19.     Compute a median filter of the input float image.
  20.     The filter window is a square of (2*size+1)*(2*size+1) pixels.
  21.  
  22.     Values outside image domain are ignored.
  23.  
  24.     The median of a list l of n>2 elements is defined as:
  25.      - l[n/2] if n is odd
  26.      - (l[n/2-1]+l[n/2])/2 is n is even
  27. */
  28. Mat median(Mat image, int size)
  29. {
  30.     Mat res = image.clone();
  31.     assert(size>0);
  32.     /********************************************
  33.                 YOUR CODE HERE
  34.     *********************************************/
  35.  
  36.  
  37.     for(int x = 0; x < image.rows; x++)
  38.     {
  39.         for(int y = 0; y < image.cols; y++)
  40.         {
  41.             int cases = 0;
  42.             vector<float> values;
  43.             for(int i = x - size; i <= x + size; i++)
  44.             {
  45.                 for(int j = y - size; j <= y + size; j++)
  46.                 {
  47.                     if(i >= 0 && j >= 0 && i < image.rows && j < image.cols)
  48.                     {
  49.                         cases++;
  50.                         values.push_back(image.at<float>(i, j));
  51.                     }
  52.  
  53.                 }
  54.             }
  55.  
  56.             std::sort(values.begin(), values.end());
  57.                
  58.             if(cases % 2)
  59.             {
  60.                 res.at<float>(x, y) = values[cases/2];
  61.             } else {
  62.                 res.at<float>(x, y) = (values[cases/2 - 1] + values[cases / 2]) / 2;
  63.  
  64.             }
  65.         }
  66.     }
  67.     /********************************************
  68.                 END OF YOUR CODE
  69.     *********************************************/
  70.     return res;
  71. }
  72.  
  73. /**
  74.     Compute the erosion of the input float image by the given structuring element.
  75.     Pixel outside the image are supposed to have value 1.
  76. */
  77. Mat erode(Mat image, Mat structuringElement)
  78. {
  79.     Mat res = image.clone();
  80.     /********************************************
  81.                 YOUR CODE HERE
  82.     *********************************************/
  83.     res = 1.0 - dilate(1.0 - res,structuringElement);
  84.    
  85.     /********************************************
  86.                 END OF YOUR CODE
  87.     *********************************************/
  88.     return res;
  89. }
  90.  
  91.  
  92. /**
  93.     Compute the dilation of the input float image by the given structuring element.
  94.      Pixel outside the image are supposed to have value 0
  95. */
  96. Mat dilate(Mat image, Mat structuringElement)
  97. {
  98.     Mat res = Mat::zeros(image.rows,image.cols,CV_32FC1);
  99.     /********************************************
  100.                 YOUR CODE HERE
  101.         hint : 1 line of code is enough
  102.     *********************************************/
  103.     for(int i = 0; i < res.cols;i++){
  104.        for(int j = 0; j < res.rows;j++){
  105.             std::vector<float> points;
  106.             for(int x = 0; x < structuringElement.cols;x++){
  107.                 for(int y = 0; y < structuringElement.rows;y++){
  108.                     if((0<=(i-(structuringElement.cols/2)+x)) && ((i-(structuringElement.cols/2)+x)<res.cols) && (0<=(j-(structuringElement.rows/2)+y)) && ((j-(structuringElement.rows/2)+y)<res.rows)){
  109.                         points.push_back(structuringElement.at<float>(y,x)*image.at<float>(j-(structuringElement.rows/2)+y,i-(structuringElement.cols/2)+x));
  110.                     }
  111.                 }
  112.            }
  113.        
  114.            res.at<float>(j,i) = *std::max_element(std::begin(points),std::end(points));
  115.        }
  116.     }
  117.     /********************************************
  118.                 END OF YOUR CODE
  119.     *********************************************/
  120.     return res;
  121. }
  122.  
  123.  
  124. /**
  125.     Compute the opening of the input float image by the given structuring element.
  126. */
  127. Mat open(Mat image, Mat structuringElement)
  128. {
  129.  
  130.    Mat res = image.clone();
  131.     /********************************************
  132.                 YOUR CODE HERE
  133.         hint : 1 line of code is enough
  134.     *********************************************/
  135.     res = erode(res , structuringElement );
  136.     res = dilate(res , structuringElement );
  137.     /********************************************
  138.                 END OF YOUR CODE
  139.     *********************************************/
  140.     return res;
  141. }
  142.  
  143.  
  144. /**
  145.     Compute the closing of the input float image by the given structuring element.
  146. */
  147. Mat close(Mat image, Mat structuringElement)
  148. {
  149.  
  150.     Mat res = image.clone();
  151.     /********************************************
  152.                 YOUR CODE HERE
  153.         hint : 1 line of code is enough
  154.     *********************************************/
  155.     res = dilate(res , structuringElement );
  156.     res = erode(res , structuringElement );
  157.     /********************************************
  158.                 END OF YOUR CODE
  159.     *********************************************/
  160.     return res;
  161. }
  162.  
  163.  
  164. /**
  165.     Compute the morphological gradient of the input float image by the given structuring element.
  166. */
  167. Mat morphologicalGradient(Mat image, Mat structuringElement)
  168. {
  169.  
  170.     Mat res = image.clone();
  171.     /********************************************
  172.                 YOUR CODE HERE
  173.         hint : 1 line of code is enough
  174.     *********************************************/
  175.     res = dilate(res , structuringElement )-erode(res , structuringElement );
  176.     /********************************************
  177.                 END OF YOUR CODE
  178.     *********************************************/
  179.     return res;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement