如何找到穿过 blob 质心的 blob 的主轴?
How to find the principal axis of a blob which goes through the centroid of the blob?
我想找到穿过 blob 质心的 blob 的主轴。我能够找到斑点的质心,但如何找到主轴?
这是我尝试过的方法:
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)
这些是我使用的图像:
我期待这样的事情。它应该与轮廓正确连接:
Expected
可以使用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
), 和旋转角度。根据这些信息,通过中心获得主轴只是一些数学运算。
这是您的代码,其中包含一些修改和添加:
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)
输出如下所示:
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.8.5
NumPy: 1.19.5
OpenCV: 4.5.1
----------------------------------------
我想找到穿过 blob 质心的 blob 的主轴。我能够找到斑点的质心,但如何找到主轴?
这是我尝试过的方法:
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)
这些是我使用的图像:
我期待这样的事情。它应该与轮廓正确连接: Expected
可以使用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
), 和旋转角度。根据这些信息,通过中心获得主轴只是一些数学运算。
这是您的代码,其中包含一些修改和添加:
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)
输出如下所示:
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.8.5
NumPy: 1.19.5
OpenCV: 4.5.1
----------------------------------------