2

对于射线照相扫描,我已经能够获得轮廓。

我有兴趣找到中心轴。我怎么能在python中做到这一点?

在此处输入图像描述

这是我的轮廓代码:

import cv2


img = cv2.imread("A.png")


imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(img,60,200)

contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

hierarchy = hierarchy[0]


cv2.drawContours(img, contours, -1, (255,0,0), 3)

cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
4

1 回答 1

5

通过回答“给我工作的 Python 代码”类型的“问题”,我可能会让这个世界变得更糟,但话又说回来,我自己需要不时使用 PCA,而且永远记不起正确的使用方法,所以这可以作为一个小备忘录。

假设我们有一个单独的脚趾骨轮廓的黑白图像:

在此处输入图像描述

让我们用 PCA 找到骨骼方向:

import cv2
import numpy as np

#loading our BW image
img = cv2.imread("test_images/toe.bmp", 0)
h, w = img.shape

#From a matrix of pixels to a matrix of coordinates of non-black points.
#(note: mind the col/row order, pixels are accessed as [row, col]
#but when we draw, it's (x, y), so have to swap here or there)
mat = np.argwhere(img != 0)
mat[:, [0, 1]] = mat[:, [1, 0]]
mat = np.array(mat).astype(np.float32) #have to convert type for PCA

#mean (e. g. the geometrical center) 
#and eigenvectors (e. g. directions of principal components)
m, e = cv2.PCACompute(mat, mean = np.array([]))

#now to draw: let's scale our primary axis by 100, 
#and the secondary by 50
center = tuple(m[0])
endpoint1 = tuple(m[0] + e[0]*100)
endpoint2 = tuple(m[0] + e[1]*50)

cv2.circle(img, center, 5, 255)
cv2.line(img, center, endpoint1, 255)
cv2.line(img, center, endpoint2, 255)
cv2.imwrite("out.bmp", img)

结果:

在此处输入图像描述

不同的骨头怎么样?很难看到线条,但仍然有效:

在此处输入图像描述

于 2017-05-10T04:44:48.437 回答