图像轮廓坐标轴

3

对于一次放射性扫描,我已经能够获取轮廓。

我想找到中心轴。我该如何在Python中实现?

enter image description here

这是我的轮廓代码:

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()

请提供您的输入图像,以便他人尝试。同时定义如何放置这些线条。 - Piglet
1
主成分分析(PCA)是首先想到的事情:http://docs.opencv.org/3.1.0/d1/dee/tutorial_introduction_to_pca.html - Headcrab
@Piglet,我刚刚添加了xray。谢谢。 - Adam Merckx
@Headcrab,能否提供Python代码? - Adam Merckx
1个回答

7
我很可能通过回答“给我Python代码”的问题来使这个世界变得更糟,但是我自己有时需要使用PCA,却无法记住正确的使用方法,因此这可能作为一个小备忘录。假设我们有一张黑白图像,显示了一个分离的脚趾骨轮廓:

enter image description here

让我们使用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)

结果如下:

enter image description here

换一个不同的骨头如何?虽然线条不太清晰,但仍然可用:

enter image description here


再次感谢您的解释,我肯定能够找到解决这个问题的方法。非常感谢! - Adam Merckx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接