python opencv 画出二值化图片百分之一的轮廓点具体代码

时间: 2023-03-08 13:05:29 浏览: 62
可以使用以下代码来画出二值化图片百分之一的轮廓点:import cv2 img = cv2.imread('input.png', 0) ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) contours, hierarchy = cv2.findContours(thresh, 1, 2) # loop over the contours for c in contours: # compute the center of the contour M = cv2.moments(c) cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) # draw the contour and center of the shape on the image cv2.drawContours(img, [c], -1, (0, 255, 0), 1) cv2.circle(img, (cX, cY), 1, (255, 255, 255), -1) # show the image cv2.imshow('image', img) cv2.waitKey(0)
相关问题

python opencv 画出二值化图片百分之一的轮廓点轮廓具体代码

### 回答1: 可以使用以下代码:# 加载图片 image = cv2.imread('image.jpg') # 转换为灰度图 gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 使用Otsu法计算二值化阈值 threshold, _ = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) # 使用计算的阈值进行二值化 binary_image = cv2.threshold(gray_image, threshold, 255, cv2.THRESH_BINARY)[1] # 计算图像的轮廓 contours, _ = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 仅取轮廓点的1% contours_1_percent = contours[::len(contours)//100] # 画出轮廓点 cv2.drawContours(image, contours_1_percent, -1, (0,255,0), 3) ### 回答2: import cv2 # 读取原始图片 image = cv2.imread('image.jpg', 0) # 对图像进行二值化处理 _, binary_image = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY) # 寻找二值化图像的轮廓 contours, hierarchy = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # 计算需要绘制的轮廓点的数量 total_points = int(len(contours[0]) * 0.01) # 创建一个空白的黑色图像,与原始图像大小相同 contour_image = np.zeros_like(image) # 绘制轮廓点 cv2.drawContours(contour_image, contours, 0, (255, 255, 255), total_points) # 展示结果图像 cv2.imshow('Contour Image', contour_image) cv2.waitKey(0) cv2.destroyAllWindows()

请写出python opencv 画出二值化图片百分之一的轮廓点具体代码

### 回答1: import cv2 import numpy as np # 读取图片 img = cv2.imread('binary_image.jpg') # 灰度化 grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 二值化 ret,thresh = cv2.threshold(grayImage,127,255,cv2.THRESH_BINARY) # 轮廓检测 image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) # 画出轮廓点,百分之一 sample_contour = np.vstack([contours[i] for i in range(0,len(contours),int(len(contours)/100))]) # 画图 cv2.drawContours(img, [sample_contour], 0, (0,255,0), 3) # 显示 cv2.imshow('img', img) cv2.waitKey(0) cv2.destroyAllWindows() ### 回答2: 以下是使用Python和OpenCV画出二值化图片百分之一的轮廓点的代码: ```python import cv2 import numpy as np # 读取二值化图像 image = cv2.imread("binary_image.png", 0) # 检测轮廓 contours, _ = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 计算百分之一的轮廓点数目 total_points = sum(len(contour) for contour in contours) points_threshold = int(0.01 * total_points) # 画出百分之一的轮廓点 for contour in contours: # 如果轮廓点数小于阈值,则忽略该轮廓 if len(contour) < points_threshold: continue # 随机选择颜色 color = np.random.randint(0, 255, size=3).tolist() # 画出轮廓点 for point in contour: x, y = point[0] cv2.circle(image, (x, y), 1, color, -1) # 显示画出轮廓点的图像 cv2.imshow("Contour Points", image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 请注意,在代码中的`"binary_image.png"`处需要替换为实际的二值化图像的文件路径。此代码会读取二值化图像,找到轮廓,计算出百分之一的轮廓点数目,然后随机选择颜色,在原始图像上画出百分之一的轮廓点,并显示结果图像。 ### 回答3: import cv2 # 读取图片 image = cv2.imread("image.jpg", 0) # 进行二值化处理 _, binary = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY) # 查找轮廓 contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 计算轮廓点数量 contour_count = len(contours[0]) # 获取所需数量的轮廓点 sample_count = int(contour_count * 0.01) sample_points = contours[0][:sample_count] # 绘制轮廓点 for point in sample_points: x, y = point[0] cv2.circle(image, (x, y), 1, (255, 0, 0), -1) # 显示结果 cv2.imshow("Contours", image) cv2.waitKey(0) cv2.destroyAllWindows()

相关推荐

最新推荐

recommend-type

python-opencv获取二值图像轮廓及中心点坐标的代码

今天小编就为大家分享一篇python-opencv获取二值图像轮廓及中心点坐标的代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

opencv python如何实现图像二值化

这篇文章主要介绍了opencv python如何实现图像二值化,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码如下 import cv2 as cv import numpy as np import ...
recommend-type

python+opencv轮廓检测代码解析

主要介绍了python+opencv轮廓检测代码解析,本文实例实现对图片的简单处理,比如图片的读取,灰度显示等相关内容,具有一定借鉴价值,需要的朋友可以参考下
recommend-type

python opencv把一张图片嵌入(叠加)到另一张图片上的实现代码

主要介绍了python opencv把一张图片嵌入(叠加)到另一张图片上,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

Python Opencv实现图像轮廓识别功能

主要为大家详细介绍了Python Opencv实现图像轮廓识别功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用matlab绘制高斯色噪声情况下的频率估计CRLB,其中w(n)是零均值高斯色噪声,w(n)=0.8*w(n-1)+e(n),e(n)服从零均值方差为se的高斯分布

以下是用matlab绘制高斯色噪声情况下频率估计CRLB的代码: ```matlab % 参数设置 N = 100; % 信号长度 se = 0.5; % 噪声方差 w = zeros(N,1); % 高斯色噪声 w(1) = randn(1)*sqrt(se); for n = 2:N w(n) = 0.8*w(n-1) + randn(1)*sqrt(se); end % 计算频率估计CRLB fs = 1; % 采样频率 df = 0.01; % 频率分辨率 f = 0:df:fs/2; % 频率范围 M = length(f); CRLB = zeros(M,1); for
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。