Detecting the centre of a curved shape with opencv
Asked Answered
C

2

6

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

Chiropody answered 21/2, 2016 at 0:4 Comment(5)
Visit this pyimagesearch.com/2016/02/01/opencv-center-of-contourNations
Hello @Nations thank you for the reply. The examples in that article are only using basic polygons and circles. The shape I am using, results in the centroid being outside of the contour. As is shown in the image. Have you seen an example using a curved shape?Chiropody
#12328291Nations
how you mathematically represent such point?? Like the point you put is just by some assumption 1 thing is that you want the point to be inside the contour apart from that where exactly you want the point ?Clementeclementi
Hello @ArijitMukherjee That is a good question. I'm not sure how I would define the point mathematically, but essentially what I want is a point in the centre of the contour, where the mass on either side of that point is equal. I'm working at the moment on a thinning algorithm, which will erode the contour to a 1 pixel line, I will then find the centre of that line. I really would like to find a faster method though, if you have any ideas?Chiropody
P
3

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.

Puppet answered 21/4, 2016 at 11:38 Comment(0)
A
0

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;
    }
  }
}
Antineutrino answered 26/1 at 1:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.