通过OpenCV对服装进行特征提取的代码示例
时间: 2023-03-19 12:20:52 浏览: 160
基于OpenCV的服装特征提取代码示例应该是这样的: import cv2 # 读取图像
img = cv2.imread('clothes.jpg') # 将图像转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 使用Sobel边缘检测器检测图像边缘
edges = cv2.Canny(gray,50,150,apertureSize = 3) # 将边缘图像转换为二值图像
ret,thresh = cv2.threshold(edges,127,255,0) # 找到图像的轮廓
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) # 遍历轮廓,提取特征
for c in contours:
# 计算轮廓的周长
perimeter = cv2.arcLength(c,True)
# 计算轮廓的凸包
approx = cv2.approxPolyDP(c,0.02*perimeter,True)
# 提取轮廓的面积
area = cv2.contourArea(c)
# 提取轮廓的质心
M = cv2.moments(c)
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
# 提取轮廓的最小外接圆
(x,y),radius = cv2.minEnclosingCircle(c)
center = (int(x),int(y))
radius = int(radius)
# 提取轮廓的最小外接矩形
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
box = np.int0(box)
# 提取轮廓的最小凸包
hull = cv2.convexHull(c)
# 提取轮廓的凹陷点
hull2 = cv2.convexHull(c,returnPoints = False)
defects = cv2.convexityDefects(c,hull2)
# 使用上述特征来检测服装图像
#...
阅读全文