opencv轮廓平滑
时间: 2024-01-10 18:21:45 浏览: 99
根据提供的引用内容,当使用OpenCV进行轮廓平滑时,需要根据源图像的深度(src.depth())选择合适的目标深度(ddepth)。下面是两个示例:
1. 如果源图像的深度为CV_8U,可以选择以下目标深度之一:-1、CV_16S、CV_32F、CV_64F。示例代码如下:
```python
import cv2
src = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
contours, hierarchy = cv2.findContours(src, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 平滑轮廓
smoothed_contours = []
for contour in contours:
smoothed_contour = cv2.approxPolyDP(contour, epsilon, closed)
smoothed_contours.append(smoothed_contour)
# 绘制平滑后的轮廓
smoothed_image = cv2.drawContours(src, smoothed_contours, -1, (0, 255, 0), 2)
cv2.imshow('Smoothed Image', smoothed_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
2. 如果源图像的深度为CV_16U或CV_16S,可以选择以下目标深度之一:-1、CV_32F、CV_64F。示例代码如下:
```python
import cv2
src = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
contours, hierarchy = cv2.findContours(src, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 平滑轮廓
smoothed_contours = []
for contour in contours:
smoothed_contour = cv2.approxPolyDP(contour, epsilon, closed)
smoothed_contours.append(smoothed_contour)
# 绘制平滑后的轮廓
smoothed_image = cv2.drawContours(src, smoothed_contours, -1, (0, 255, 0), 2)
cv2.imshow('Smoothed Image', smoothed_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文