我想要实现的功能是:小车+云台 追踪特定的物体
我的方法是:利用特征点识别提前将特征点保存好,然后再用特征值检测来匹配这个特定物体
我提前保存了三张图片,但是识别的时候,我移动物体,也就是换了背景,openmv就识别不到物体了,因此也追踪不了
请问有什么办法可以解决吗?
你提供一下具体的图片。
我保存了三张这个物体的图

然后运行的时候根本识别不到咧

这是代码 拜托帮忙看一下
# Blob Detection Example
#
# This example shows off how to use the find_blobs function to find color
# blobs in the image. This example in particular looks for dark green objects.
import sensor, image, time
import car
from pid import PID
from pyb import Servo
pan_servo=Servo(1)
tilt_servo=Servo(2)
pan_pid = PID(p=0.06, i=0.1, imax=90) #脱机运行或者禁用图像传输,使用这个PID #识别左右。位于下方的舵机
tilt_pid = PID(p=0.08, i=0.05, imax=90) #脱机运行或者禁用图像传输,使用这个PID
#pan_pid = PID(p=0.1,d=0.01, i=0.02, imax=100)#在线调试使用这个PID
#tilt_pid = PID(p=0.1, d=0.01,i=0.02, imax=75)#在线调试使用这个PID
sensor.reset()# 感光元件设置
# HQVGA and GRAYSCALE are the best for face tracking.
# HQVGA和灰度对于人脸识别效果最好
sensor.set_contrast(3)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.VGA)
sensor.set_windowing((320, 240))
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False, value=100)
#画出特征点
def draw_keypoints(img, kpts):
if kpts:
print(kpts)
img.draw_keypoints(kpts)
img = sensor.snapshot()
time.sleep(2000)
#kpts1 = None #运行程序的时候,最开始识别到的物体的特征,将会被保存到kpts1中
#kpts1保存目标物体的特征,可以从文件导入特征,但是不建议这么做。建议检测的时候实时提取特征
kpts1 = image.load_descriptor("/desc.orb")
kpts3= image.load_descriptor("/desc1.orb")
kpts4= image.load_descriptor("/desc2.orb")
clock = time.clock()
#如果摄像头检测到的小球的色块面积像素
#点数大于2000的话,就说明我们的小车距
#离我们的小球非常近,大于2000就让小车后退一点,
#小于2000就设置openmv去追这个小球
size_threshold = 2000
x_pid = PID(p=0.5, i=1, imax=100) #控制电机的方向(拐角)
h_pid = PID(p=0.02, i=0.1, imax=50) #控制小车的速度
def find_max(blobs): #追踪距离最近的小球
max_size=0
for blob in blobs:
if blob[4]*blob[5] > max_size: #blob[2]是矩形的宽、blob[3]是矩形的高度
max_blob=blob
max_size = blob[4]*blob[5]
return max_blob
while(True):
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # 截取一张图片
if (kpts1 == None):
kpts1 = img.find_keypoints(max_keypoints=150, threshold=10, scale_factor=1.35)
draw_keypoints(img, kpts1)
else:
kpts2 = img.find_keypoints(max_keypoints=150, threshold=10, normalized=True) #用来检测视野中是否存在特征,将检测到的特征保留在kpts2中
if (kpts2):
match = image.match_descriptor(kpts1, kpts2, threshold=85)
if (match.count()>10):
# If we have at least n "good matches"
# Draw bounding rectangle and cross.
#在匹配到的目标特征中心画十字和矩形框。
img.draw_rectangle(match.rect())
img.draw_cross(match.cx(), match.cy(), size=10)
print(kpts2, "matched:%d dt:%d"%(match.count(), match.theta()))
pan_error = match.cx()-img.width()/2
tilt_error = match.cy()-img.height()/2
print("pan_error: ", pan_error)
x_output=x_pid.get_pid(match.cx(),1)
h_output=h_pid.get_pid(match.cy(),1) #调用pid的函数,get_pid是pid里面的函数,来得到一个最终的结果,来控制小车运动
car.run(-h_output+x_output,-h_output+x_output)
pan_output=pan_pid.get_pid(pan_error,1)
tilt_output=tilt_pid.get_pid(tilt_error,1)
pan_servo.angle(pan_servo.angle()+pan_output)
tilt_servo.angle(tilt_servo.angle()+tilt_output)
else:
car.run(18,-18) #小车在原地以非常慢的速度在旋转
match2 = image.match_descriptor(kpts3, kpts2, threshold=85)
if (match2.count()>10):
# If we have at least n "good matches"
# Draw bounding rectangle and cross.
#在匹配到的目标特征中心画十字和矩形框。
img.draw_rectangle(match2.rect())
img.draw_cross(match2.cx(), match2.cy(), size=10)
print(kpts2, "matched:%d dt:%d"%(match2.count(), match2.theta()))
pan_error = match2.cx()-img.width()/2
tilt_error = match2.cy()-img.height()/2
print("pan_error: ", pan_error)
x_output=x_pid.get_pid(match2.cx(),1)
h_output=h_pid.get_pid(match2.cy(),1) #调用pid的函数,get_pid是pid里面的函数,来得到一个最终的结果,来控制小车运动
car.run(-h_output+x_output,-h_output+x_output)
pan_output=pan_pid.get_pid(pan_error,1)
tilt_output=tilt_pid.get_pid(tilt_error,1)
pan_servo.angle(pan_servo.angle()+pan_output)
tilt_servo.angle(tilt_servo.angle()+tilt_output)
else:
car.run(18,-18) #小车在原地以非常慢的速度在旋转
match3 = image.match_descriptor(kpts4, kpts2, threshold=85)
if (match3.count()>10):
# If we have at least n "good matches"
# Draw bounding rectangle and cross.
#在匹配到的目标特征中心画十字和矩形框。
img.draw_rectangle(match3.rect())
img.draw_cross(match3.cx(), match3.cy(), size=10)
print(kpts2, "matched:%d dt:%d"%(match3.count(), match3.theta()))
pan_error = match3.cx()-img.width()/2
tilt_error = match3.cy()-img.height()/2
print("pan_error: ", pan_error)
x_output=x_pid.get_pid(match3.cx(),1)
h_output=h_pid.get_pid(match3.cy(),1) #调用pid的函数,get_pid是pid里面的函数,来得到一个最终的结果,来控制小车运动
car.run(-h_output+x_output,-h_output+x_output)
pan_output=pan_pid.get_pid(pan_error,1)
tilt_output=tilt_pid.get_pid(tilt_error,1)
pan_servo.angle(pan_servo.angle()+pan_output)
tilt_servo.angle(tilt_servo.angle()+tilt_output)
else:
car.run(18,-18) #小车在原地以非常慢的速度在旋转https://singtown.com/learn/50021/
先按照视频步骤,测试一下换了背景后是否能识别到。
@kidswong999 试过了呢,这些视频我都看了好几遍了的 识别度非常不灵敏 或者就算是识别到 也没办法跟随 所以才想问有没有更好的解决方法
没什么好办法。。。