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)
相关推荐
开MINI的工科男27 分钟前
【笔记】自动驾驶预测与决策规划_Part3_路径与轨迹规划
人工智能·笔记·自动驾驶·预测与决策
xuanyu223 小时前
Linux常用指令
linux·运维·人工智能
凡人的AI工具箱4 小时前
AI教你学Python 第11天 : 局部变量与全局变量
开发语言·人工智能·后端·python
晓星航4 小时前
Docker本地部署Chatbot Ollama搭建AI聊天机器人并实现远程交互
人工智能·docker·机器人
Kenneth風车4 小时前
【机器学习(五)】分类和回归任务-AdaBoost算法-Sentosa_DSML社区版
人工智能·算法·低代码·机器学习·数据分析
AI小白龙*4 小时前
大模型团队招人(校招):阿里巴巴智能信息,2025届春招来了!
人工智能·langchain·大模型·llm·transformer
空指针异常Null_Point_Ex5 小时前
大模型LLM之SpringAI:Web+AI(一)
人工智能·chatgpt·nlp
Alluxio5 小时前
选择Alluxio来解决AI模型训练场景数据访问的五大理由
大数据·人工智能·分布式·ai·语言模型
pzx_0015 小时前
【内积】内积计算公式及物理意义
数据结构·python·opencv·算法·线性回归
AIPaPerPass写论文5 小时前
写论文去哪个网站?2024最佳五款AI毕业论文学术网站
人工智能·深度学习·chatgpt·powerpoint·ai写作