opencv 对象骨架化

本文介绍了如何利用OpenCV获取二进制图像中对象的骨架。主要通过距离变换、拉普拉斯运算和阈值处理等步骤实现。首先将图像转换为灰度,然后进行二值化。接着应用距离变换,再计算距离变换图像的拉普拉斯,最后通过阈值处理得到对象的骨架。需要注意在距离变换前可能需要进行形态学操作以去除噪声。
摘要由CSDN通过智能技术生成

欢迎关注微信公众号——计算机视觉:

 

 

How to get the skeleton of an object using OPENCV

http://pinkjade8848.spaces.live.com/blog/cns!E4159959CD42C507!195.entry

 

Skeletonization is a process for reducing foreground regions in a binary image to a skeletal remnant that largely preserves the extent and connectivity of the original region while throwing away most of the original foreground pixels. Generally speaking, there are 2 methods to obtain the skeleton.(I am not sure there are other methods.)

thinning

distance transform

 

OpenCV doesnot provide functions for skeletonization, but provides some functions on distance transform, so I use distance transform to complete this task.

 

Distance transform is an operator

根据提供的引用内容,似乎是在讨论图像处理中的骨架提取问题。下面是使用OpenCV进行骨架提取的方法: 1. 读取图像并将其转换为灰度图像 ```python import cv2 img = cv2.imread('image.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ``` 2. 对灰度图像进行二值处理 ```python ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) ``` 3. 对二值图像进行反色处理 ```python thresh = cv2.bitwise_not(thresh) ``` 4. 对反色后的图像进行距离变换 ```python dist_transform = cv2.distanceTransform(thresh, cv2.DIST_L2, 5) ``` 5. 对距离变换后的图像进行二值处理 ```python ret, sure_fg = cv2.threshold(dist_transform, 0.7*dist_transform.max(), 255, 0) ``` 6. 对二值后的图像进行膨胀操作 ```python kernel = np.ones((3,3),np.uint8) sure_fg = cv2.dilate(sure_fg,kernel,iterations=3) ``` 7. 对膨胀后的图像进行距离变换 ```python dist_transform = cv2.distanceTransform(sure_fg, cv2.DIST_L2, 5) ``` 8. 对距离变换后的图像进行二值处理 ```python ret, sure_fg = cv2.threshold(dist_transform, 0.7*dist_transform.max(), 255, 0) ``` 9. 对二值后的图像进行反色处理 ```python sure_fg = cv2.bitwise_not(sure_fg) ``` 10. 对反色后的图像进行骨架提取 ```python size = np.size(sure_fg) skel = np.zeros(sure_fg.shape,np.uint8) element = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3)) done = False while(not done): eroded = cv2.erode(sure_fg,element) temp = cv2.dilate(eroded,element) temp = cv2.subtract(sure_fg,temp) skel = cv2.bitwise_or(skel,temp) sure_fg = eroded.copy() zeros = size - cv2.countNonZero(sure_fg) if zeros==size: done = True ``` 以上就是使用OpenCV进行骨架提取的方法。需要注意的是,这里的骨架提取是基于二值图像的,因此需要先对图像进行二值处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值