最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Python+OpenCV图像处理之实现轮廓发现代码示例
时间:2020-10-24 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下Python+OpenCV图像处理之实现轮廓发现代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
轮廓发现是基于图像边缘提取的基础寻找对象轮廓的方法,所以边缘提取的阈值选定会影响最终轮廓发现结果。
代码如下:
import cv2 as cv import numpy as np def contours_demo(image): dst = cv.GaussianBlur(image, (3, 3), 0) #高斯模糊去噪 gray = cv.cvtColor(dst, cv.COLOR_RGB2GRAY) ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) #用大律法、全局自适应阈值方法进行图像二值化 cv.imshow("binary image", binary) cloneTmage, contours, heriachy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) for i, contour in enumerate(contours): cv.drawContours(image, contours, i, (0, 0, 255), 2) print(i) cv.imshow("contours", image) for i, contour in enumerate(contours): cv.drawContours(image, contours, i, (0, 0, 255), -1) cv.imshow("pcontours", image) src = cv.imread('E:/imageload/coins.jpg') cv.namedWindow('input_image', cv.WINDOW_NORMAL) #设置为WINDOW_NORMAL可以任意缩放 cv.imshow('input_image', src) contours_demo(src) cv.waitKey(0) cv.destroyAllWindows()
运行结果:
相关文章
- Golang ProtoBuf的基本语法详解 10-20
- Python识别MySQL中的冗余索引解析 10-20
- Python+Pygame绘制小球代码展示 10-18
- Python中的数据精度问题介绍 10-18
- Python随机值生成的常用方法介绍 10-18
- python3解压缩.gz文件分析 09-27