源图片:

实验1.直接findContours:
import cv2
img = cv2.imread('lala.png', 0)
contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
print(len(contours))
colored_img = cv2.imread('lala.png')
for i in range(len(contours)):
cv2.drawContours(colored_img, contours, i, (255, 255, 0), 5)
cv2.imshow('contour-%d'%i, colored_img)
cv2.waitKey()
colored_img = cv2.imread('lala.png')
cv2.destroyAllWindows()
结果如下图所示,值得注意的是整张图片的框也算进轮廓里:

实验2.截取部分图片findContours(取消上述代码的注释)
结果图示,基本同实验1:

实验3.先使用Canny算法得到边缘图,找边缘图中的轮廓。
import cv2
img = cv2.imread('lala.png', 0)
img = cv2.Canny(img, 10, 20)
cv2.imshow('canny', img)
contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
print(len(contours))
colored_img = cv2.imread('lala.png')
for i in range(len(contours)):
cv2.drawContours(colored_img, contours, i, (255, 255, 0), 5)
cv2.imshow('contour-%d'%i, colored_img)
cv2.waitKey()
colored_img = cv2.imread('lala.png')
cv2.destroyAllWindows()
结果如图所示,我认为2和3边缘并不密封,有一些小缺口导致其内轮廓和外轮廓连成一条轮廓,于是不像1那样分别展示的是内轮廓和外轮廓,另由于Canny提取的边缘很细,内轮廓和外轮廓基本上是一条线。

实验4.1的内轮廓和外轮廓是否基本是一条线
import numpy as np
print(len(contours[3]), len(contours[4]), len(np.unique(np.concatenate((contours[3], contours[4])), axis=0)))
结果:209 205 217
209+205-217=197可见内轮廓和外轮廓的大部分点都是重合的。
实验5.Canny提取部分图像的边缘
结果如图所示:

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!