请写出python opencv 画出二值化图片百分之一的轮廓点具体代码
时间: 2023-09-15 20:05:41 浏览: 56
### 回答1:
import cv2img = cv2.imread('image.jpg', 0) # 读取图片
ret,thresh = cv2.threshold(img,127,255,0) # 二值化
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # 找出轮廓# 画出百分之一的轮廓点
epsilon = 0.01*cv2.arcLength(contours[0],True)
approx = cv2.approxPolyDP(contours[0],epsilon,True)cv2.drawContours(img, [approx], 0, (0,255,0), 5) # 画出轮廓
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
### 回答2:
import cv2
# 读取原始图片
image = cv2.imread('image.jpg', 0)
# 对图像进行二值化处理
ret, binary_image = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
# 查找轮廓
contours, hierarchy = cv2.findContours(binary_image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# 计算轮廓的点数
total_points = 0
for contour in contours:
total_points += contour.shape[0]
# 提取百分之一的轮廓点数
required_points = total_points // 100
# 绘制百分之一的轮廓点
drawn_points = 0
for contour in contours:
points = contour.shape[0]
if drawn_points + points <= required_points:
for point in contour:
cv2.circle(image, tuple(point[0]), 1, (0, 255, 0), -1)
drawn_points += points
else:
remaining_points = required_points - drawn_points
for i in range(remaining_points):
cv2.circle(image, tuple(contour[i][0]), 1, (0, 255, 0), -1)
break
# 显示带有百分之一轮廓点的图片
cv2.imshow('Contours', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
### 回答3:
import cv2
# 加载图像
image = cv2.imread('input_image.jpg', 0)
# 二值化处理
_, thresh = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
# 查找轮廓
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 计算出需要绘制的点数
total_points = int(len(contours[0]) * 0.01)
# 提取并绘制指定数量的随机轮廓点
import random
random_points = random.sample(contours[0].tolist(), total_points)
for point in random_points:
x, y = point[0]
cv2.circle(image, (x, y), 2, (0, 0, 255), -1)
# 显示结果图像
cv2.imshow('Result', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)