用python写一个图像边界线提取和骨架化提取代码 要求图像批量输入和图像批量输出
时间: 2024-05-15 08:12:05 浏览: 8
以下是使用Python和OpenCV库实现图像边界线提取和骨架化提取的代码:
```python
import cv2
import os
# 图像批量输入路径和输出路径
input_path = "input_folder/"
output_path = "output_folder/"
# 获取文件夹中所有图像文件名
img_files = os.listdir(input_path)
# 遍历所有图像文件
for img_file in img_files:
# 读取图像
img = cv2.imread(input_path + img_file)
# 边界线提取
canny = cv2.Canny(img, 50, 150)
# 骨架化提取
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
skel = cv2.ximgproc.thinning(binary)
# 保存提取结果
cv2.imwrite(output_path + img_file.split('.')[0] + "_canny.jpg", canny)
cv2.imwrite(output_path + img_file.split('.')[0] + "_skel.jpg", skel)
```
以上代码将从 `input_folder` 文件夹中读取所有图像文件,并对每个图像进行边界线提取和骨架化提取。提取结果将保存在 `output_folder` 文件夹中,文件名格式为 `{原图像文件名}_canny.jpg` 和 `{原图像文件名}_skel.jpg`。