请写出python opencv 画出二值化图片百分之一的轮廓点具体代码
时间: 2023-09-10 21:05:22 浏览: 68
### 回答1:
import cv2# 读取图像
image = cv2.imread("test.jpg")
# 灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 二值化
ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 找轮廓
contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 画出百分之一的轮廓点
count = int(len(contours) * 0.01)
for i in range(count):
cv2.drawContours(image, contours, i, (0, 255, 0), 3)
# 显示图像
cv2.imshow("image", image)
cv2.waitKey(0)
### 回答2:
import cv2
# 读取原始图像
image = cv2.imread('original_image.jpg')
# 将图像转为灰度图像
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 对图像进行二值化处理
ret, threshold = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# 轮廓检测
contours, hierarchy = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 计算需要取出的点数
total_points = len(contours[0]) // 100
# 提取百分之一的轮廓点
contour_points = contours[0][:total_points]
# 在原始图像上标记轮廓点
for point in contour_points:
cv2.circle(image, tuple(point[0]), 2, (0, 0, 255), -1)
# 显示结果图像
cv2.imshow("Contour Points", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
### 回答3:
import cv2
# 加载图像
img = cv2.imread("image.jpg", 0)
# 二值化处理
ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
# 基于轮廓查找
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 计算轮廓点数量
total_points = sum([len(contour) for contour in contours])
# 找出百分之一的轮廓点数量
target_points = total_points // 100
# 提取百分之一的轮廓点
points = []
current_points = 0
for contour in contours:
for point in contour:
points.append(point)
current_points += 1
if current_points >= target_points:
break
if current_points >= target_points:
break
# 绘制轮廓点
for point in points:
cv2.circle(img, (point[0][0], point[0][1]), 1, (0, 255, 0), -1)
# 显示结果
cv2.imshow("Contours", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
阅读全文