标签:point radius psi 最小 import ret rect circle 输入
1 cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]]) → image, contours, hierarchy
1 cv2.contourArea(contour[, oriented]) → retval
1 cv2.moments(array[, binaryImage]) → retval
1 import cv2 2 import numpy as np 3 4 img = cv2.imread(‘star.jpg‘,0) 5 ret,thresh = cv2.threshold(img,127,255,0) 6 contours,hierarchy = cv2.findContours(thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 7 8 cnt = contours[0] 9 M = cv2.moments(cnt) 10 print(cv2.contourArea(cnt)) 11 print(M) 12 13 #得到{‘m00‘: 0.5, ‘m10‘: 53.83333333333333, ‘m01‘: 367.3333333333333, ‘m20‘: 5796.083333333333, ‘m11‘: 39549.541666666664, ‘m02‘: 269867.5833333333, ‘m30‘: 624050.9500000001, ‘m21‘: 4258186.233333333, ‘m12‘: 29055722.733333334, ‘m03‘: 198262758.70000002, ‘mu20‘: 0.027777777778283053, ‘mu11‘: -0.01388888888322981, ‘mu02‘: 0.027777777810115367, ‘mu30‘: -0.003703703638166189, ‘mu21‘: 0.0018518519221615293, ‘mu12‘: 0.001851847569924292, ‘mu03‘: -0.0037037134170532227, ‘nu20‘: 0.11111111111313221, ‘nu11‘: -0.05555555553291924, ‘nu02‘: 0.11111111124046147, ‘nu30‘: -0.020951311664420796, ‘nu21‘: 0.01047565641531008, ‘nu12‘: 0.010475631795338369, ‘nu03‘: -0.020951366982159467} 14 15 #我们利用这个可以得到重心 16 cx = int(M[‘m10‘]/M[‘m00‘]) 17 cy = int(M[‘m01‘]/M[‘m00‘]) 18 #contourArea的结果和m00的结果一致 19
1 perimeter = cv2.arcLength(cnt,True)
1 cv2.approxPolyDP(curve, epsilon, closed[, approxCurve]) → approxCurve
1 cv2.polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]]) → img
1 import cv2 as cv 2 import numpy as np 3 img = cv.imread("test.jpg",0) 4 _,contours,_ = cv.findContours(img,2,1) 5 cnt = contours[0] 6 epsilon = 0.01 * cv.arcLength(cnt,True)#这里用arcLength得到轮廓周长或者曲线长度 7 approx = cv.approxPolyDP(cnt,epsilon,True) 8 out_img = cv.polylines(img,[approx],True,(0,0,255),2) 9 cv.imshow("image",out_img) 10 k = cv.waitKey(1) & 0xFF 11 if k== 27: 12 cv.destroyAllWindows()
1 cv2.convexHull(points[, hull[, clockwise[, returnPoints]]]) → hull
1 import cv2 as cv 2 import numpy as np 3 img = cv.imread("test.jpg",0) 4 _,contours,_ = cv.findContours(img,2,1) 5 cnt = contours[1] 6 hull = cv.convexHull(cnt) 7 out_img = cv.polylines(img,[hull],True,(0,255,255),2) 8 cv.imshow("image",out_img) 9 cv.waitKey(0)
1 cv2.boundingRect(points) → retval
1 cv2.minAreaRect(points) → retval
import cv2 as cv import numpy as np img = cv.imread("test.jpg",0) _,contours,_ = cv.findContours(img,cv.RETR_LIST,cv.CHAIN_APPROX_SIMPLE) cnt = contours[0] rect_vertical = cv.boundingRect(cnt) rect = cv.minAreaRect(cnt)#这里得到的是旋转矩形 box = cv.cv.BoxPoints(rect)#得到端点 box = np.int0(box)#向下取整
1 cv2.minEnclosingCircle(points) → center, radius
1 cv2.fitEllipse(points) → retval
1 import cv2 as cv 2 import numpy as np 3 img = cv.imread("test.jpg",0) 4 _,contours,_ = cv.findContours(img,cv.RETR_LIST,cv.CHAIN_APPROX_SIMPLE) 5 cnt = contours[0] 6 circle = cv.minEnclosingCircle(cnt) 7 ellipse = cv.fitEllipse(cnt) 8 out_1 = cv.circle(img,circle,(0,255,255),2) 9 out_2 =cv.ellipse(img,ellipse,(0,255,255),2) 10 cv.imshow("img1",out_1) 11 cv.imshow("img2",out_2) 12 cv.waitKey(0)
标签:point radius psi 最小 import ret rect circle 输入
原文地址:https://www.cnblogs.com/aoru45/p/9765262.html