对于放射线扫描,我已经能够获得轮廓。
我有兴趣找到中心轴。我怎么能在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()
答案 0 :(得分:2)
我可能通过回答一个" gimme工作的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)
结果:
另一块骨头怎么样?很难看到线条,但仍然有效: