机器人对物体进行移动跟随
1.物体识别2.移动跟随
1.物体识别
本案例实现对特殊颜色物体的识别,并实现根据物体位置的改变进行控制跟随。
import cv2 as cv
# 定义结构元素
kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
# print kernel
capture = cv.VideoCapture(0)
print capture.isOpened()
ok, frame = capture.read()
lower_b = (65, 43, 46)
upper_b = (110, 255, 255)
height, width = frame.shape[0:2]
screen_center = width / 2
offset = 50
while ok:
# 将图像转成HSV颜色空间
hsv_frame = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
# 基于颜色的物体提取
mask = cv.inRange(hsv_frame, lower_b, upper_b)
mask2 = cv.morphologyEx(mask, cv.MORPH_OPEN, kernel)
mask3 = cv.morphologyEx(mask2, cv.MORPH_CLOSE, kernel)
# 找出面积最大的区域
_, contours, _ = cv.findContours(mask3, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
maxArea = 0
maxIndex = 0
for i, c in enumerate(contours):
area = cv.contourArea(c)
if area > maxArea:
maxArea = area
maxIndex = i
# 绘制
cv.drawContours(frame, contours, maxIndex, (255, 255, 0), 2)
# 获取外切矩形
x, y, w, h = cv.boundingRect(contours[maxIndex])
cv.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
# 获取中心像素点
center_x = int(x + w/2)
center_y = int(y + h/2)
cv.circle(frame, (center_x, center_y), 5, (0, 0, 255), -1)
# 简单的打印反馈数据,之后补充运动控制
if center_x |