OpenCV图像处理——轮廓检测

图像的轮廓

在这里插入图片描述

查找轮廓

binary,contours,hierarchy=cv.findContours(img,mode,method)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

绘制轮廓

cv.drawContours(img,coutours,index,color,width)

在这里插入图片描述

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])

在这里插入图片描述

轮廓的特征

在这里插入图片描述

轮廓面积

area=cv.contourArea(cnt)

轮廓周长

perimeter=cv.arcLength(cnt,isclosed)

在这里插入图片描述

轮廓近似

在这里插入图片描述

approx=cv.approxPolyDP(cnt,epsilon,isclosed)

在这里插入图片描述

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])

在这里插入图片描述

凸包

在这里插入图片描述

hull=cv.convexHull(points,clockwise,returnPoints)

在这里插入图片描述
在这里插入图片描述

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])

在这里插入图片描述

边界矩形

在这里插入图片描述
在这里插入图片描述

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])

在这里插入图片描述

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])

在这里插入图片描述

最小外接圆

在这里插入图片描述

(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])

在这里插入图片描述

椭圆拟合

在这里插入图片描述

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

在这里插入图片描述

直线拟合

在这里插入图片描述

output=cv.fitLine(points,distType,param,aeps)

在这里插入图片描述

[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])

在这里插入图片描述

图像的矩特征

在这里插入图片描述

矩的概念

在这里插入图片描述

图像中的矩特征

在这里插入图片描述
在这里插入图片描述

moments(array,binaryImage=False)

在这里插入图片描述

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)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

醋酸洋红就是我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值