pythonopencvblobaxiscentroid

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:

import cv2
import numpy as np

img = cv2.imread('skin6.jpg')

imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh2 = cv2.threshold(imgray, 155, 255, cv2.THRESH_BINARY_INV)

#find the maximum contour
contours, heir = cv2.findContours(thresh2, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
c = max(contours, key = cv2.contourArea)

tmpArea = np.zeros(img.shape)
cv2.drawContours(tmpArea,[c],0,(255, 255, 255),cv2.FILLED)

#centroid
M = cv2.moments(c)
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])

cv2.circle(tmpArea, (cx, cy), 5, (0, 0, 255), -1)

cv2.imshow("tmpArea", tmpArea)
cv2.waitKey(0)

These are the images that I used:

Image

Image2

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


Solution

  • 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:

    import cv2
    import numpy as np
    
    # Images
    img = cv2.imread('images/1KXQA.jpg')
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    thresh2 = cv2.threshold(img_gray, 155, 255, cv2.THRESH_BINARY_INV)[1]
    tmpArea = np.zeros(img.shape)
    
    # Contours
    contours = cv2.findContours(thresh2, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[0]
    c = max(contours, key=cv2.contourArea)
    cv2.drawContours(tmpArea,[c],0,(255, 255, 255),cv2.FILLED)
    
    # Centroid
    M = cv2.moments(c)
    cx = int(M['m10']/M['m00'])
    cy = int(M['m01']/M['m00'])
    cv2.circle(tmpArea, (cx, cy), 5, (0, 0, 255), -1)
    
    # Ellipse
    e = cv2.fitEllipse(c)
    cv2.ellipse(tmpArea, e, (0, 255, 0), 2)
    
    # Principal axis
    x1 = int(np.round(cx + e[1][1] / 2 * np.cos((e[2] + 90) * np.pi / 180.0)))
    y1 = int(np.round(cy + e[1][1] / 2 * np.sin((e[2] + 90) * np.pi / 180.0)))
    x2 = int(np.round(cx + e[1][1] / 2 * np.cos((e[2] - 90) * np.pi / 180.0)))
    y2 = int(np.round(cy + e[1][1] / 2 * np.sin((e[2] - 90) * np.pi / 180.0)))
    cv2.line(tmpArea, (x1, y1), (x2, y2), (255, 255, 0), 2)
    
    # Output
    cv2.imshow('tmpArea', tmpArea)
    cv2.waitKey(0)
    

    The output looks like this:

    Output

    ----------------------------------------
    System information
    ----------------------------------------
    Platform:      Windows-10-10.0.16299-SP0
    Python:        3.8.5
    NumPy:         1.19.5
    OpenCV:        4.5.1
    ----------------------------------------