Opencv - Contours 属性及操作 Python API

Opencv - Contours 属性及操作 Python API

Contours, 轮廓

  • 定义 - Contours,explained simply as a curve joining all the continuous points (along the boundary), having same color or intensity.
  • 用处 - shape analysis,object detection and recognition

    • 采用二值图(binary images), 故,在寻找轮廓前,采用阈值或 canny 边缘检测.
    • cv2.findContours 函数在原图进行修改
    • Opencv中, cv2.findContours 类似于从黑色背景中寻找白色物体,因此,二值图中待寻找的物体应为白色,背景应为黑色

示例 - 寻找二值图的轮廓

import numpy as np
import cv2

im = cv2.imread('test.jpg')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray,127,255,0)
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

cv2.findContours函数输入有三个参数:
- thresh: source image
- cv2.RETR_TREE: 轮廓检索模式
- cv2.CHAIN_APPROX_SIMPLE: 轮廓逼近方法
输出三个结果:
- contours: 图像中所有的轮廓,python列表的形式保存. 每个单独的contour是包括物体边界点的(x,y)坐标的Numpy 数组.

示例 - 画出轮廓

img = cv2.drawContours(img, contours, -1, (0,255,0), 3) # 画出Image中的所有轮廓

img = cv2.drawContours(img, contours, 3, (0,255,0), 3) # 画出Image中的某个轮廓,比如第四个

# 一般也采用下面的方法画出某个轮廓,比如第四个
cnt = contours[4]
img = cv2.drawContours(img, [cnt], 0, (0,255,0), 3)

轮廓特征

主要有轮廓面积、周长、质心、边界框等.

1.Moments

Image Moments 用于计算物体质心、物体面积等.

import cv2
import numpy as np

img = cv2.imread('star.jpg',0)
ret,thresh = cv2.threshold(img,127,255,0)
contours,hierarchy = cv2.findContours(thresh, 1, 2)

cnt = contours[0]
M = cv2.moments(cnt) # 以dict形式输出所有moment值
print M

根据计算得到的 moments值,可以提取很多信息,比如面积、质心等.

2.Contour Area 轮廓面积

area = cv2.contourArea(cnt)

3.Contour Perimeter 轮廓周长

perimeter = cv2.arcLength(cnt,True)

4.Checking Convexity 轮廓凸性

k = cv2.isContourConvex(cnt)

5.Bounding Rectangle 边界框

5.1 Straight Bounding Rectangle 直矩形框
x,y,w,h = cv2.boundingRect(cnt) # (x,y)是rectangle的左上角坐标, (w,h)是width和height
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
5.2 Rotated Rectangle 旋转矩形框
rect = cv2.minAreaRect(cnt) # 返回 Box2D结构,包括了左上角坐标(x,y),(width, height)和旋转角度
box = cv2.boxPoints(rect) # Rectangle的四个角
box = np.int0(box)
im = cv2.drawContours(im,[box],0,(0,0,255),2)

6.Minimum Enclosing Circle 最小包围圆

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

7.Fitting an Ellipse 椭圆拟合

ellipse = cv2.fitEllipse(cnt)
im = cv2.ellipse(im,ellipse,(0,255,0),2)

8.Fitting a Line 直线拟合

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

9.Aspect Ratio 宽高比

物体边界框的宽高比.

x,y,w,h = cv2.boundingRect(cnt)
aspect_ratio = float(w)/h

10.Extent 轮廓物体面积与边界框面积比

area = cv2.contourArea(cnt)
x,y,w,h = cv2.boundingRect(cnt)
rect_area = w*h
extent = float(area)/rect_area

11.Solidity 轮廓物体面积和convex hull 面积比

area = cv2.contourArea(cnt)
hull = cv2.convexHull(cnt)
hull_area = cv2.contourArea(hull)
solidity = float(area)/hull_area

12.Equivalent Diameter 与轮廓物体面积相等的圆的直径

area = cv2.contourArea(cnt)
equi_diameter = np.sqrt(4*area/np.pi)

13.Mask and Pixel Points 物体的所有像素点

mask = np.zeros(imgray.shape,np.uint8)
cv2.drawContours(mask,[cnt],0,255,-1)
pixelpoints = np.transpose(np.nonzero(mask))
#pixelpoints = cv2.findNonZero(mask)

14.Maximum Value, Minimum Value and their locations

mask image 的最大值,最小值和其位置

min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(imgray,mask = mask)

15.Extreme Points 极值点

物体的最上、下、左、右的点.

leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])
rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])
topmost = tuple(cnt[cnt[:,:,1].argmin()][0])
bottommost = tuple(cnt[cnt[:,:,1].argmax()][0])

Reference

  • 4
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
以下是一个基于OpenCVPython 2代码示例,用于识别并跟随黑线: ```python import cv2 import numpy as np # 初始化NAO机器人 # 定义黑线的HSV颜色范围 black_lower = np.array([0, 0, 0], np.uint8) black_upper = np.array([180, 255, 30], np.uint8) # 打开摄像头 cap = cv2.VideoCapture(0) while True: # 读取摄像头捕获的图像 ret, frame = cap.read() # 将图像转换为HSV颜色空间 hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # 根据定义的颜色范围过滤黑线 mask = cv2.inRange(hsv, black_lower, black_upper) # 使用形态学操作对图像进行处理 kernel = np.ones((3, 3), np.uint8) mask = cv2.erode(mask, kernel, iterations=1) mask = cv2.dilate(mask, kernel, iterations=2) mask = cv2.erode(mask, kernel, iterations=1) # 在图像中寻找轮廓 contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # 如果找到了轮廓 if len(contours) > 0: # 寻找最大的轮廓 c = max(contours, key=cv2.contourArea) # 计算轮廓的中心点 M = cv2.moments(c) cx = int(M['m10'] / M['m00']) cy = int(M['m01'] / M['m00']) # 在图像中绘制轮廓 cv2.drawContours(frame, [c], -1, (0, 255, 0), 2) # 如果中心点在左侧,则向左转 if cx < 320: # 向左转的NAO机器人代码 # 如果中心点在右侧,则向右转 else: # 向右转的NAO机器人代码 # 显示图像 cv2.imshow('frame', frame) # 如果按下q键,则退出循环 if cv2.waitKey(1) & 0xFF == ord('q'): break # 关闭摄像头 cap.release() # 关闭所有窗口 cv2.destroyAllWindows() ``` 请注意,上述代码中的NAO机器人控制代码需要根据您的具体机器人和API进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值