Skip to content
Advertisement

How to find the principal axis of a blob which goes through the centroid of the blob?

I want to find the principal axis of a blob which goes through the centroid of the blob. I was able to find the centroid of the blob, but how can I find the principal axis?

Here’s what I have tried:

JavaScript

These are the images that I used:

Image

Image2

I’m expecting something like this. It should connect with the contour properly: Expected

Advertisement

Answer

You can use cv2.fitEllipse on your detected contour. There’s an OpenCV tutorial on that topic. You get the center of the fitted ellipse, the length of both axes (please have a detailed look at cv2.ellipse), and the rotation angle. From that information, it’s just some math to get the principal axis through the center.

Here’s your code with some modifications and additions:

JavaScript

The output looks like this:

Output

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement