opencv 细化算法实现
时间: 2023-07-25 11:45:12 浏览: 123
细化算法是一种图像处理算法,用于将二值图像中的线条变细,常用于数字识别、指纹识别等应用中。OpenCV提供了多种细化算法的实现,以下是其中一种基于Zhang-Suen算法的实现代码示例:
```python
import cv2
def thinning(img):
size = np.array(img.shape)
skel = np.zeros(size, np.uint8)
ret, img = cv2.threshold(img, 127, 255, 0)
element = cv2.getStructuringElement(cv2.MORPH_CROSS, (3,3))
done = False
while not done:
eroded = cv2.erode(img, element)
temp = cv2.dilate(eroded, element)
temp = cv2.subtract(img, temp)
skel = cv2.bitwise_or(skel, temp)
img = eroded.copy()
zeros = size[0]*size[1] - cv2.countNonZero(img)
if zeros == size[0]*size[1]:
done = True
return skel
```
这个函数接受一个二值化图像,返回一个细化后的图像。其中,使用cv2.erode和cv2.dilate函数实现了腐蚀和膨胀操作,使用cv2.subtract函数实现了减法操作,最后使用cv2.bitwise_or函数进行逻辑或操作得到细化后的图像。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)