Razor Robotics
Razor Robotics

Reputation: 193

Detecting the centre of a curved shape with opencv

I've been trying for a while to find the centre of a curved shape (for example a banana). I can do all the basics, such as creating a binary image, and locating the contour. However, the centroid function correctly finds a point outside of the contour. The point I require must be inside the contour. I've attached an image which should explain things better.

If anyone has any ideas, or has seen something similar I would really appreciate some help.

enter image description here

Upvotes: 7

Views: 3564

Answers (2)

user3614271
user3614271

Reputation: 11

A simple algorithm that I use:

void findCenter(std::vector<float> x, std::vector<float> y, float& cx, float& cy) {
  cx = 0;
  cy = 0;
  float minDist = 1000000;
  int half = x.size()/2;
  for (int i = 0; i < half; i++) {
    float dist = distance(x[i], y[i], x[i+half], y[i+half]);
    if (dist < minDist) {
      cx = (x[i] + x[i+half]) / 2;
      cy = (y[i] + y[i+half]) / 2;
      minDist = dist;
    }
  }
}

Upvotes: 0

gunshi
gunshi

Reputation: 66

You could look at this answer, What is the fastest way to find the "visual" center of an irregularly shaped polygon?

Basically skeletonisation algorithms should help (in terms of efficiency and accuracy as compared to continuous erosion, which would fail in some cases), since they narrow down the set of possible valid points to a set of line segments, which you can then do some sort of conditional processing on.

Upvotes: 3

Related Questions