企業(yè)網站建設 論文百度智能建站系統(tǒng)
目錄
- 圖像的輪廓
- 查找輪廓
- 繪制輪廓
- 輪廓的特征
- 輪廓面積
- 輪廓周長
- 輪廓近似
- 凸包
- 邊界矩形
- 最小外接圓
- 橢圓擬合
- 直線擬合
- 圖像的矩特征
- 矩的概念
- 圖像中的矩特征
圖像的輪廓
查找輪廓
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 pltimg = 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)