Advertisement
jack06215

[OpenCV] gradient

Jul 7th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. std::pair<cv::Mat, cv::Mat> gradient(cv::Mat & img, float spaceX, float spaceY)
  2. {
  3.  
  4.     cv::Mat gradY = gradientY(img, spaceY);
  5.     cv::Mat gradX = gradientX(img, spaceX);
  6.     std::pair<cv::Mat, cv::Mat> retValue(gradX, gradY);
  7.     return retValue;
  8. }
  9.  
  10. static cv::Mat gradientX(cv::Mat & mat, float spacing)
  11. {
  12.     cv::Mat grad = cv::Mat::zeros(mat.rows, mat.cols, CV_32F);
  13.  
  14.     /*  last row */
  15.     int maxCols = mat.cols;
  16.     int maxRows = mat.rows;
  17.  
  18.     /* get gradients in each border */
  19.     /* first row */
  20.     cv::Mat col = (-mat.col(0) + mat.col(1)) / (float)spacing;
  21.     col.copyTo(grad(cv::Rect(0, 0, 1, maxRows)));
  22.  
  23.     col = (-mat.col(maxCols - 2) + mat.col(maxCols - 1)) / (float)spacing;
  24.     col.copyTo(grad(cv::Rect(maxCols - 1, 0, 1, maxRows)));
  25.  
  26.     /* centered elements */
  27.     cv::Mat centeredMat = mat(cv::Rect(0, 0, maxCols - 2, maxRows));
  28.     cv::Mat offsetMat = mat(cv::Rect(2, 0, maxCols - 2, maxRows));
  29.     cv::Mat resultCenteredMat = (-centeredMat + offsetMat) / (((float)spacing)*2.0);
  30.  
  31.     resultCenteredMat.copyTo(grad(cv::Rect(1, 0, maxCols - 2, maxRows)));
  32.     return grad;
  33. }
  34.  
  35. static cv::Mat gradientY(cv::Mat & mat, float spacing)
  36. {
  37.     cv::Mat grad = cv::Mat::zeros(mat.rows, mat.cols, CV_32F);
  38.  
  39.     /*  last row */
  40.     const int maxWidth = mat.cols;
  41.     const int maxHeight = mat.rows;
  42.  
  43.     /* get gradients in each border */
  44.     /* first row */
  45.     cv::Mat row = (-mat.row(0) + mat.row(1)) / (float)spacing;
  46.     row.copyTo(grad(cv::Rect(0, 0, maxWidth, 1)));
  47.  
  48.     row = (-mat.row(maxHeight - 2) + mat.row(maxHeight - 1)) / (float)spacing;
  49.     row.copyTo(grad(cv::Rect(0, maxHeight - 1, maxWidth, 1)));
  50.  
  51.     /* centered elements */
  52.     cv::Mat centeredMat = mat(cv::Rect(0, 0, maxWidth, maxHeight - 2));
  53.     cv::Mat offsetMat = mat(cv::Rect(0, 2, maxWidth, maxHeight - 2));
  54.     cv::Mat resultCenteredMat = (-centeredMat + offsetMat) / (((float)spacing)*2.0);
  55.  
  56.     resultCenteredMat.copyTo(grad(cv::Rect(0, 1, maxWidth, maxHeight - 2)));
  57.     return grad;
  58. }
  59.  
  60. int main(void)
  61. {
  62.     cv::Mat imgMat = cv::imread("sample.jpg", CV_LOAD_IMAGE_COLOR);
  63.     cv::Mat imgMat_bw = cv::Mat::zeros(imgMat.rows, imgMat.cols, CV_32FC(1));
  64.     cv::cvtColor(imgMat, imgMat_bw, cv::COLOR_RGB2GRAY);
  65.     std::pair<cv::Mat, cv::Mat> grad;
  66.     grad = gradient(imgMat_bw, 1.0f, 1.0f);
  67.     cv::Mat_<float> dX = grad.first;
  68.     cv::Mat_<float> dY = grad.second;
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement