Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include "ActiveContour.hpp"
  2.  
  3. void computeExternalEnergyImage(Mat input, Mat& energyImage)
  4. {
  5.     energyImage.create(input.rows, input.cols, CV_64FC1);
  6.  
  7.     //declaring a mat for the gaussian blur
  8.     Mat blurredIMG;
  9.     //decaring sobel and abs mats
  10.     Mat sobelXImage;
  11.     Mat sobelYImage;
  12.     Mat ABSX;
  13.     Mat ABSY;
  14.     //declaring gradient mats
  15.     Mat gradient;
  16.     Mat LargeGradient;
  17.     Mat SmallGradient;
  18.     Mat MedGradient;
  19.  
  20.     GaussianBlur(input,blurredIMG,Size(7,7),0);
  21.  
  22.     Sobel(blurredIMG,sobelXImage,CV_64F,1,0,1,1,0,BORDER_DEFAULT);
  23.  
  24.     Sobel(blurredIMG, sobelYImage, CV_64F, 0, 1, 1, 1, 0, BORDER_DEFAULT);
  25.  
  26.  
  27.     ABSY = cv::abs(sobelYImage);
  28.     ABSX = cv::abs(sobelXImage);
  29.  
  30.     gradient = ABSY;
  31.     gradient += ABSX;
  32.  
  33.     GaussianBlur(gradient,LargeGradient,Size(301,301),0);
  34.     GaussianBlur(gradient,MedGradient,Size(41,41),0);
  35.     GaussianBlur(gradient,SmallGradient,Size(13,13),0);
  36.  
  37.     energyImage = SmallGradient + MedGradient + LargeGradient + gradient;
  38.  
  39. }
  40. double computeEnergyForCurve(vector<Point>& allPoints, Mat& energyImage, double iaw, double ibw, double ew)
  41. {
  42.     //ew is the external energy weight, iaw is the interal first - derivative weight, and ibw is the internal second - derivative weight.
  43.     //Compute the internal energy as the ((first derivative) * iaw) + ((second derivative) * ibw)
  44.     double tmp = 0.0, exEn = 0.0, inEn = 0.0;
  45.     double fdiv = 0.0, sdiv = 0.0;
  46.     for (int i = 0; i < allPoints.size(); i++) {
  47.         exEn += energyImage.at<double>(allPoints.at(i));
  48.     }
  49.    
  50.     exEn = (exEn * (-1)) * ew;
  51.     //cout << exEn << "got the EWEWEWE" << endl;
  52.    
  53.     double p0, p1, p2;
  54.     double length;
  55.  
  56.     for (int i = 1; i < allPoints.size(); i++) {
  57.         p1 = energyImage.at<double>(allPoints.at((int)i - 1));
  58.         p2 = energyImage.at<double>(allPoints.at(i));
  59.         fdiv = cv::norm(p1 - p2);
  60.  
  61.         if (i > 1)
  62.         {
  63.             p0 = energyImage.at<double>(allPoints.at((int)i - 2));
  64.             sdiv = cv::norm(p0 - 2.0 * p1 + p2);
  65.         }
  66.     }
  67.     inEn = (fdiv * iaw) + (sdiv * ibw);
  68.     tmp = inEn + exEn;
  69.     return tmp;
  70. }
  71. bool updateCurve(vector<Point>& allPoints, Mat& energyImage, double iaw, double ibw, double ew)
  72. {
  73.     bool updated = false;
  74.     Point tmp;
  75.     double inten;
  76.     int x, y, n = 0;
  77.  
  78.     for (int i = 0; i < allPoints.size(); i++) {
  79.         inten = energyImage.at<double>(allPoints.at(i));
  80.         tmp = allPoints.at(i);
  81.         for (int j = 0; j < DIR_CNT; j++) {
  82.             x = dirs[j].x;
  83.             y = dirs[j].y;
  84.             if (allPoints[i].x+x < energyImage.rows && allPoints[i].y+y < energyImage.cols)
  85.             {
  86.                 if (inten < energyImage.at<double>(tmp.x + x, tmp.y + y))
  87.                 {
  88.                     inten = energyImage.at<double>(tmp.x + x, tmp.y + y);
  89.                     n++;
  90.                     updated = true;
  91.                 }
  92.                 else
  93.                 {
  94.                     updated = false;
  95.                 }
  96.             }
  97.         }
  98.         if (updated)
  99.         {
  100.             allPoints[i].x += dirs[n].x;
  101.             allPoints[i].y += dirs[n].y;
  102.         }
  103.         n = 0;
  104.     }
  105.  
  106.     return updated;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement