Ask Your Question
1

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

asked 2018-12-17 11:41:26 -0600

Mert Guler gravatar image

updated 2018-12-17 11:45:52 -0600

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];
}
edit retag flag offensive close merge delete

Comments

2

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

LBerger gravatar imageLBerger ( 2018-12-17 15:43:02 -0600 )edit

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

berak gravatar imageberak ( 2018-12-20 10:55:10 -0600 )edit

1 answer

Sort by » oldest newest most voted
2

answered 2018-12-20 10:30:03 -0600

berak gravatar image

updated 2018-12-20 12:26:52 -0600

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;
      }
  }
edit flag offensive delete link more

Comments

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

Mert Guler gravatar imageMert Guler ( 2018-12-20 14:09:51 -0600 )edit

ah, nice update, thanks !

berak gravatar imageberak ( 2018-12-21 00:32:22 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-12-17 11:41:26 -0600

Seen: 401 times

Last updated: Dec 20 '18