问题
前几天有个人问了我一个问题,问题是这样的,他有如下的一张二值图像:
怎么得到白色Blob中心线,他希望的效果如下:
显然OpenCV中常见的轮廓分析无法获得上面的中心红色线段,本质上这个问题是如何提取二值对象的骨架,提取骨架的方法在OpenCV的扩展模块中,另外skimage包也支持图像的骨架提取。这里就分别基于OpenCV扩展模块与skimage包来完成骨架提取,得到上述图示的中心线。
01
安装skimage与opencv扩展包
Python环境下安装skimage图像处理包与opencv计算机视觉包,只需要分别执行下面两行命令:
代码语言:javascript复制pip install opencv-contrib-python
pip install skimage
导入使用
代码语言:javascript复制from skimage import morphology
import cv2 as cv
02
使用skimage实现骨架提取
有两个相关的函数实现二值图像的骨架提取,一个是基于距离变换实现的medial_axis方法;另外一个是基于thin的skeletonize骨架提取方法。两个方法的代码实现分别如下:
代码语言:javascript复制def skeleton_demo(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
binary[binary == 255] = 1
skeleton0 = morphology.skeletonize(binary)
skeleton = skeleton0.astype(np.uint8) * 255
cv.imshow("skeleton", skeleton)
cv.waitKey(0)
cv.destroyAllWindows()
def medial_axis_demo(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
binary[binary == 255] = 1
skel, distance = morphology.medial_axis(binary, return_distance=True)
dist_on_skel = distance * skel
skel_img = dist_on_skel.astype(np.uint8)*255
contours, hireachy = cv.findContours(skel_img, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
cv.drawContours(image, contours, -1, (0, 0, 255), 1, 8)
cv.imshow("result", image)
cv.waitKey(0)
cv.destroyAllWindows()
03
使用OpenCV实现骨架提取
OpenCV的图像细化的骨架提取方法在扩展模块中,因此需要直接安装opencv-python的扩展包。此外还可以通过形态学的膨胀与腐蚀来实现二值图像的骨架提取,下面的代码实现就是分别演示了基于OpenCV的两种骨架提取方法。代码分别如下:
代码语言:javascript复制def morph_find(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
kernel = cv.getStructuringElement(cv.MORPH_CROSS, (3, 3))
finished = False
size = np.size(binary)
skeleton = np.zeros(binary.shape, np.uint8)
while (not finished):
eroded = cv.erode(binary, kernel)
temp = cv.dilate(eroded, kernel)
temp = cv.subtract(binary, temp)
skeleton = cv.bitwise_or(skeleton, temp)
binary = eroded.copy()
zeros = size - cv.countNonZero(binary)
if zeros == size:
finished = True
contours, hireachy = cv.findContours(skeleton, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
cv.drawContours(image, contours, -1, (0, 0, 255), 1, 8)
cv.imshow("skeleton", image)
cv.waitKey(0)
cv.destroyAllWindows()
def thin_demo(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
thinned = cv.ximgproc.thinning(binary)
contours, hireachy = cv.findContours(thinned, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
cv.drawContours(image, contours, -1, (0, 0, 255), 1, 8)
cv.imshow("thin", image)
cv.waitKey(0)
cv.destroyAllWindows()
运行结果如下: