OpenCV图像处理——轮廓检测

目录

图像的轮廓

查找轮廓

python 复制代码
binary,contours,hierarchy=cv.findContours(img,mode,method)





绘制轮廓

python 复制代码
cv.drawContours(img,coutours,index,color,width)
python 复制代码
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

img = cv2.imread('./汪学长的随堂资料/4/图像操作/contours.png')
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
canny=cv.Canny(img_gray,127,255,0)
contours,hi=cv.findContours(canny,cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)
img=cv.drawContours(img,contours,-1,(0,0,255),2)
plt.imshow(img[:,:,::-1])

轮廓的特征

轮廓面积

python 复制代码
area=cv.contourArea(cnt)

轮廓周长

python 复制代码
perimeter=cv.arcLength(cnt,isclosed)

轮廓近似

python 复制代码
approx=cv.approxPolyDP(cnt,epsilon,isclosed)
python 复制代码
img = cv2.imread('./汪学长的随堂资料/4/图像操作/contours2.png')

img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnt=contours[0]
area=cv.contourArea(cnt)
length=cv.arcLength(cnt,True)
esplion=0.1*length
approx=cv.approxPolyDP(cnt,esplion,True)
img=cv.polylines(img,[approx],True,(0,0,255),2)
plt.imshow(img[:,:,::-1])

凸包

python 复制代码
hull=cv.convexHull(points,clockwise,returnPoints)


python 复制代码
img=cv.imread('./image/star 2.jpeg')
img1=img.copy()
imggray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
canny=cv.canny(imggray,127,255,0)
contours,hi=cv.findContours(canny,cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)
hulls=[]
for cnt in contours:
    hull=cv.convexHull(cnt)
    hulls.append(hull)
img1=cv.drawContours(img1,hulls,-1,(0,255,0),2)
plt.imshow(img1[:,:,::-1])

边界矩形


python 复制代码
img=cv.imread('./image/arrows,jpg')
img_gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
ret,thresh=cv.threshold(img_gray,127,255,0)
contours,hi=cv.findContours(thresh,1,2)
cnt=contours[1]
x,y,w,h=cv.boundingRect(cnt)
imgRect=cv.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)
plt.imshow(imgRect[:,:,::-1])
python 复制代码
s=cv.minAreaRect(cnt)
a=cv.boxPoints(s)
a=np.int0(a)
cv.polylines(imgRect,[a],True,(0,0,255),3)
plt.imshow(imgRect[:,:,::-1])

最小外接圆

python 复制代码
(x,y),r=cv.minEnclosingCircle(cnt)
center=(int(x),int(y))
r=int(r)
imgcircle=cv.circle(img,center,r,(0,255,0),3)
plt.imshow(imgcircle[:,:,::-1])

椭圆拟合

python 复制代码
ellipse=cv.fitEllipse(cnt)
imgellipse=cv.ellipse(img,ellipse,(0,255,255,3))
plt.imshow(imgellipse[:,:,::-1])

直线拟合

python 复制代码
output=cv.fitLine(points,distType,param,aeps)
python 复制代码
[vx,vy,x,y]=cv.fitLine(cnt,cv.DIST_L2,0,0.01,0.01)
rows,cols=img.shape[:2]
lefty=int((-x*vy/vx)+y)
righty=int(((cols-x)*vy/vx)+y)
imgline=cv.line(img,(0,lefty),(cols-1,righty),(0,0,255),3)
plt.imshow(imgline[:,:,::-1])

图像的矩特征

矩的概念

图像中的矩特征


python 复制代码
moments(array,binaryImage=False)
python 复制代码
img=cv.imread('./image/arrows.jpg',0)
imgmn=cv.moments(img)
imghu=cv.HuMoments(imgmn)
ret,thresh=cv.threshold(img,127,255,0)
contours,hi=cv.findContours(thresh,1,2)
cnt=contours[1]
mn=cv.moments(cnt)
hu=cv.HuMoments(mn)
相关推荐
紫雾凌寒2 小时前
计算机视觉应用|自动驾驶的感知革命:多传感器融合架构的技术演进与落地实践
人工智能·机器学习·计算机视觉·架构·自动驾驶·多传感器融合·waymo
sauTCc2 小时前
DataWhale-三月学习任务-大语言模型初探(一、二、五章学习)
人工智能·学习·语言模型
暴力袋鼠哥2 小时前
基于深度学习的中文文本情感分析系统
人工智能·深度学习
视觉语言导航2 小时前
RAG助力机器人场景理解与具身操作!EmbodiedRAG:基于动态三维场景图检索的机器人任务规划
人工智能·深度学习·具身智能
岱宗夫up2 小时前
《加快应急机器人发展的指导意见》中智能化升级的思考——传统应急设备智能化升级路径与落地实践
人工智能·aigc
訾博ZiBo2 小时前
AI日报 - 2025年3月12日
人工智能
龚大龙2 小时前
机器学习(李宏毅)——Auto-Encoder
人工智能·机器学习
snow@li3 小时前
AI问答:transformer 架构 / 模型 / 自注意力机制实现序列数据的并行处理 / AI的底层
人工智能·深度学习·transformer
cv2016_DL3 小时前
siglip2推理教程
人工智能·transformer
小枫小疯3 小时前
Pytorch 转向TFConv过程中的卷积转换
人工智能·pytorch·python