Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "tpMorphology.h"
- #include <cmath>
- #include <algorithm>
- #include <tuple>
- #include<vector>
- #include <limits>
- #include "common.h"
- using namespace cv;
- using namespace std;
- bool isInPicture(Mat image, int x, int y)
- {
- if(x >= 0 && y >= 0 && x < image.rows && y < image.cols)
- return true;
- return false;
- }
- /**
- Compute a median filter of the input float image.
- The filter window is a square of (2*size+1)*(2*size+1) pixels.
- Values outside image domain are ignored.
- The median of a list l of n>2 elements is defined as:
- - l[n/2] if n is odd
- - (l[n/2-1]+l[n/2])/2 is n is even
- */
- Mat median(Mat image, int size)
- {
- Mat res = image.clone();
- assert(size>0);
- /********************************************
- YOUR CODE HERE
- *********************************************/
- for(int x = 0; x < image.rows; x++)
- {
- for(int y = 0; y < image.cols; y++)
- {
- int cases = 0;
- vector<float> values;
- for(int i = x - size; i <= x + size; i++)
- {
- for(int j = y - size; j <= y + size; j++)
- {
- if(i >= 0 && j >= 0 && i < image.rows && j < image.cols)
- {
- cases++;
- values.push_back(image.at<float>(i, j));
- }
- }
- }
- std::sort(values.begin(), values.end());
- if(cases % 2)
- {
- res.at<float>(x, y) = values[cases/2];
- } else {
- res.at<float>(x, y) = (values[cases/2 - 1] + values[cases / 2]) / 2;
- }
- }
- }
- /********************************************
- END OF YOUR CODE
- *********************************************/
- return res;
- }
- /**
- Compute the erosion of the input float image by the given structuring element.
- Pixel outside the image are supposed to have value 1.
- */
- Mat erode(Mat image, Mat structuringElement)
- {
- Mat res = image.clone();
- /********************************************
- YOUR CODE HERE
- *********************************************/
- res = 1.0 - dilate(1.0 - res,structuringElement);
- /********************************************
- END OF YOUR CODE
- *********************************************/
- return res;
- }
- /**
- Compute the dilation of the input float image by the given structuring element.
- Pixel outside the image are supposed to have value 0
- */
- Mat dilate(Mat image, Mat structuringElement)
- {
- Mat res = Mat::zeros(image.rows,image.cols,CV_32FC1);
- /********************************************
- YOUR CODE HERE
- hint : 1 line of code is enough
- *********************************************/
- for(int i = 0; i < res.cols;i++){
- for(int j = 0; j < res.rows;j++){
- std::vector<float> points;
- for(int x = 0; x < structuringElement.cols;x++){
- for(int y = 0; y < structuringElement.rows;y++){
- 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)){
- points.push_back(structuringElement.at<float>(y,x)*image.at<float>(j-(structuringElement.rows/2)+y,i-(structuringElement.cols/2)+x));
- }
- }
- }
- res.at<float>(j,i) = *std::max_element(std::begin(points),std::end(points));
- }
- }
- /********************************************
- END OF YOUR CODE
- *********************************************/
- return res;
- }
- /**
- Compute the opening of the input float image by the given structuring element.
- */
- Mat open(Mat image, Mat structuringElement)
- {
- Mat res = image.clone();
- /********************************************
- YOUR CODE HERE
- hint : 1 line of code is enough
- *********************************************/
- res = erode(res , structuringElement );
- res = dilate(res , structuringElement );
- /********************************************
- END OF YOUR CODE
- *********************************************/
- return res;
- }
- /**
- Compute the closing of the input float image by the given structuring element.
- */
- Mat close(Mat image, Mat structuringElement)
- {
- Mat res = image.clone();
- /********************************************
- YOUR CODE HERE
- hint : 1 line of code is enough
- *********************************************/
- res = dilate(res , structuringElement );
- res = erode(res , structuringElement );
- /********************************************
- END OF YOUR CODE
- *********************************************/
- return res;
- }
- /**
- Compute the morphological gradient of the input float image by the given structuring element.
- */
- Mat morphologicalGradient(Mat image, Mat structuringElement)
- {
- Mat res = image.clone();
- /********************************************
- YOUR CODE HERE
- hint : 1 line of code is enough
- *********************************************/
- res = dilate(res , structuringElement )-erode(res , structuringElement );
- /********************************************
- END OF YOUR CODE
- *********************************************/
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement