Ask Your Question
1

How to translate opencv C++ code ,containing pointers to JAVA?

asked Dec 17 '18

Mert Guler gravatar image

updated Dec 17 '18

berak gravatar image

.

cv::Mat getEyeball(cv::Mat &eye, cv::Mat &circles)
{
  std::vector<int> sums(circles.size(), 0);
  for (int y = 0; y < eye.rows; y++)
  {
  uchar *ptr = eye.ptr<uchar>(y); 
      for (int x = 0; x < eye.cols; x++)
      {
          int value = static_cast<int>(*ptr);
          for (int i = 0; i < circles.size(); i++)
          {
              cv::Point center((int)std::round(circles[i][0]), (int)std::round(circles[i][1]));
              int radius = (int)std::round(circles[i][2]);
              if (std::pow(x - center.x, 2) + std::pow(y - center.y, 2) < std::pow(radius, 2))
              {
                  sums[i] += value;
              }
          }
          ++ptr;
      }
  }
  int smallestSum = 9999999;
  int smallestSumIndex = -1;
  for (int i = 0; i < circles.size(); i++)
  {
      if (sums[i] < smallestSum)
      {
          smallestSum = sums[i];
          smallestSumIndex = i;
      }
  }
  return circles[smallestSumIndex];
}
Preview: (hide)

Comments

2

I think you can find an answer in this tutorial : same code for C++ python and java

LBerger gravatar imageLBerger (Dec 17 '18)edit

the current algorithm also weights smaller circles better (irrespective of the value), due to usage of sum()

berak gravatar imageberak (Dec 20 '18)edit

1 answer

Sort by » oldest newest most voted
2

answered Dec 20 '18

berak gravatar image

updated Dec 20 '18

apart from iterating over pixels, consider this idea, - instead: make a circle mask image, and decide on the mean value from the masked out image:

  int best = -1;
  double bestval = 999999.0;
  for (int i = 0; i < circles.size(); i++)
  {
      // TODO: filter circles for size & position

      // start with a black image
      Mat mask(eye.size(), eye.type(), Scalar.all(0));

      cv::Point center((int)std::round(circles[i][0]), (int)std::round(circles[i][1]));
      int radius = (int)std::round(circles[i][2]);

      // draw a filled, white circle
      Imgproc.circle(mask, center, radius, Scalar.all(255), -1);

      // mean value of the circle region only
      Scalar mean = Core.mean(eye,mask);

      // retain best         
      if (mean.val[0] < bestVal)
      {
          bestVal = mean.val[0];
          best = i;
      }
  }
Preview: (hide)

Comments

thanks Mr.Türkmen(Teşekkür ederim Süleyman abi dediğini deneyeceğim.)

Mert Guler gravatar imageMert Guler (Dec 20 '18)edit

ah, nice update, thanks !

berak gravatar imageberak (Dec 21 '18)edit

Question Tools

1 follower

Stats

Asked: Dec 17 '18

Seen: 403 times

Last updated: Dec 20 '18