近似轮廓寻找
import cv2
img = cv2.imread("./images/26.jpg",cv2.IMREAD_COLOR)
img2gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(img2gray,127,255,cv2.THRESH_OTSU)
contours,_ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# 近似的误差
epsion =40
approx = cv2.approxPolyDP(contours[0],epsion,True)
img_contour = cv2.drawContours(img,[approx],contourIdx=-1,color=(0,0,255),thickness=3)
cv2.imshow("img_contour",img_contour)
cv2.waitKey(0)
边界矩形绘制
import cv2
import numpy as np
img = cv2.imread("./images/16.jpg")
img2gary = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(img2gary,127,255,cv2.THRESH_OTSU)
contour,_ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# 边界矩形
x,y,w,h = cv2.boundingRect(contour[0])
img_coutour = cv2.rectangle(img,(x,y),(x+w,y+h),color=(0,255,0),thickness=2)
cv2.imshow("img_contour",img_coutour)
cv2.waitKey(0)
最小外切矩形绘制
import cv2
import numpy as np
img = cv2.imread("./images/16.jpg")
img2gary = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(img2gary,127,255,cv2.THRESH_OTSU)
contour,_ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# # 最小矩形
rect = cv2.minAreaRect(contour[0])
box = cv2.boxPoints(rect)
box = 0(box)
img_coutour = cv2.drawContours(img,[box],contourIdx=0,color=(0,255,0),thickness=3)
cv2.imshow("img_contour",img_coutour)
cv2.waitKey(0)
最小外切圆
import cv2
import numpy as np
img = cv2.imread("./images/16.jpg")
img2gary = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(img2gary,127,255,cv2.THRESH_OTSU)
contour,_ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# 最小外切圆
(x,y),radious = cv2.minEnclosingCircle(contour[0])
center = (int(x),int(y))
radious = int(radious)
img_coutour = cv2.circle(img,center,radious,(255,0,0),thickness=2)
cv2.imshow("img_contour",img_coutour)
cv2.waitKey(0)
凸包检测
import cv2
img = cv2.imread("./images/16.jpg",cv2.IMREAD_COLOR)
img2gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(img2gray,127,255,cv2.THRESH_OTSU)
contours,_ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
hull = cv2.convexHull(contours[0])
print(cv2.isContourConvex(contours[0]),cv2.isContourConvex(hull))
#False,True
img_contour= cv2.drawContours(img,[hull],-1,(0,0,255),3)
cv2.imshow("img_contour",img_contour)
cv2.waitKey(0)
检测重心,面积,周长
import cv2
img =cv2.imread("./images/16.jpg",cv2.IMREAD_COLOR)
img2gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(img2gray,127,255,cv2.THRESH_OTSU)
contours,_ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# 矩形
M = cv2.moments(contours[0])
cx,cy = int(M["m10"]/M["m00"]),int(M["m01"]/M["m00"])
print("重心:",cx,cy)
area = cv2.contourArea(contours[0])
print("面积:",area)
perimeter = cv2.arcLength(contours[0],True)
print("周长:",perimeter)
img_contour= cv2.drawContours(img,contours,-1,(0,0,255),3)
cv2.imshow("img_contour",img_contour)
cv2.waitKey(0)
**
**
import cv2
import numpy as np
img = cv2.imread("./images/16.jpg")
img2gary = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(img2gary,127,255,cv2.THRESH_OTSU)
contours,_ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# 椭圆拟合
ellipse = cv2.fitEllipse(contours[0])
# cv2.ellipse(contours[0])
cv2.ellipse(img,ellipse,(255,0,0),thickness=2)
# 直线拟合
h,w, _ = img.shape
[vx,vy,x,y] = cv2.fitLine(contours[0],cv2.DIST_L2,0,0.01,0.01)
lefty = int((-x*vy/vx)+y)
righty = int((w-x)*vy/vx+y)
cv2.line(img,(w-1,righty),(0,lefty),(0,0,255),thickness=2)
cv2.imshow("img_contour",img)
cv2.waitKey(0)
轮廓匹配
import cv2
img1 = cv2.imread('./images/16.jpg', 0)
img2 = cv2.imread('./images/17.jpg', 0)
ret, thresh = cv2.threshold(img1, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnt1 = contours[0]
ret, thresh2 = cv2.threshold(img2, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnt2 = contours[0]
ret = cv2.matchShapes(cnt1, cnt2, cv2.CONTOURS_MATCH_I2, 0.0)
print(ret)