opencv 轮廓特征

1.距
cv.moments()

import numpy as np
import cv2 as cv
img = cv.imread('star.jpg',0)
ret,thresh = cv.threshold(img,127,255,0)
im2,contours,hierarchy = cv.findContours(thresh, 1, 2)
cnt = contours[0]
M = cv.moments(cnt)
print( M )

2.面积

area = cv.contourArea(cnt)

3.周长

perimeter = cv.arcLength(cnt,True)

4.轮廓近似
基于道格拉斯-peucker算法实现
假设要找一个正方形,但是又是不规则的正方形,则可以使用轮廓近似

epsilon = 0.1*cv.arcLength(cnt,True)
approx = cv.approxPolyDP(cnt,epsilon,True)

5.凸

hull = cv.convexHull(points[, hull[, clockwise[, returnPoints]]

6.检查凸面

k = cv.isContourConvex(cnt)

7.矩形
直矩形

x,y,w,h = cv.boundingRect(cnt)
cv.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)

旋转矩形

rect = cv.minAreaRect(cnt)
box = cv.boxPoints(rect)
box = np.int0(box)
cv.drawContours(img,[box],0,(0,0,255),2)



8.圆

(x,y),radius = cv.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int(radius)
cv.circle(img,center,radius,(0,255,0),2)

9.椭圆

ellipse = cv.fitEllipse(cnt)
cv.ellipse(img,ellipse,(0,255,0),2)
  1. 线
rows,cols = img.shape[:2]
[vx,vy,x,y] = cv.fitLine(cnt, cv.DIST_L2,0,0.01,0.01)
lefty = int((-x*vy/vx) + y)
righty = int(((cols-x)*vy/vx)+y)
cv.line(img,(cols-1,righty),(0,lefty),(0,255,0),2)




参考文献:
https://docs.opencv.org/3.4.3/dd/d49/tutorial_py_contour_features.html
https://blog.csdn.net/qq_41905045/article/details/81430893

你可能感兴趣的:(图像识别)