
需要提供全部的代码,我才能测试。
# 利用特征点检测特定物体例程。
# 向相机显示一个对象,然后运行该脚本。 一组关键点将被提取一次,然后
# 在以下帧中进行跟踪。 如果您想要一组新的关键点,请重新运行该脚本。
# 注意:请参阅文档以调整find_keypoints和match_keypoints。
import sensor, time, image
yellow_threshold = (23, 97, 62, -5, 10, 94)
# Reset sensor
sensor.reset()
# Sensor settings
sensor.set_pixformat(sensor.RGB565) # 格式为 RGB565.
sensor.set_framesize(sensor.QVGA) # 使用 QQVGA 速度快一些
sensor.skip_frames(time = 2000) # 跳过2000s,使新设置生效,并自动调节白平衡
sensor.set_auto_gain(False) # 关闭自动自动增益。默认开启的,在颜色识别中,一定要关闭白平衡。
sensor.set_auto_whitebal(False)
#画出特征点
def draw_keypoints(img, kpts):
if kpts:
print(kpts)
img.draw_keypoints(kpts)
# img = sensor.snapshot()
# time.sleep(1000)
def find_max(blobs):
max_size=0
for blob in blobs:
if blob.pixels() > max_size:
max_blob=blob
max_size = blob.pixels()
return max_blob
kpts1 = None
kpts2 = None
kpts3 = None
r = [0,0,0,0]
#kpts1保存目标物体的特征,可以从文件导入特征,但是不建议这么做。
#kpts1 = image.load_descriptor("/desc.orb")
#img = sensor.snapshot()
#draw_keypoints(img, kpts1)
clock = time.clock()
while (True):
clock.tick()
img = sensor.snapshot()
if (kpts1 == None):
img = img.to_grayscale()
#如果是刚开始运行程序,提取最开始的图像作为目标物体特征,kpts1保存目标物体的特征
#默认会匹配目标特征的多种比例大小,而不仅仅是保存目标特征时的大小,比模版匹配灵活。
# NOTE: By default find_keypoints returns multi-scale keypoints extracted from an image pyramid.
kpts1 = img.find_keypoints(max_keypoints=150, threshold=1, scale_factor=1.2)
draw_keypoints(img, kpts1)
elif (kpts2 == None):
img = sensor.snapshot()
img = img.to_grayscale()
kpst2 = img.find_keypoints(max_keypoints=150, threshold=1, scale_factor=1.2)
draw_keypoints(img, kpts2)
#image.find_keypoints(roi=Auto, threshold=20, normalized=False, scale_factor=1.5, max_keypoints=100, corner_detector=CORNER_AGAST)
#roi表示识别的区域,是一个元组(x,y,w,h),默认与framsesize大小一致。
#threshold是0~255的一个阈值,用来控制特征点检测的角点数量。用默认的AGAST特征点检测,这个阈值大概是20。用FAST特征点检测,这个阈值大概是60~80。阈值越低,获得的角点越多。
#normalized是一个布尔数值,默认是False,可以匹配目标特征的多种大小(比ncc模版匹配效果灵活)。如果设置为True,关闭特征点检测的多比例结果,仅匹配目标特征的一种大小(类似于模版匹配),但是运算速度会更快一些。
#scale_factor是一个大于1.0的浮点数。这个数值越高,检测速度越快,但是匹配准确率会下降。一般在1.35~1.5左右最佳。
#max_keypoints是一个物体可提取的特征点的最大数量。如果一个物体的特征点太多导致RAM内存爆掉,减小这个数值。
#corner_detector是特征点检测采取的算法,默认是AGAST算法。FAST算法会更快但是准确率会下降。
#画出此时的目标特征
elif (kpts1!= None and kpts2!= None):
#当与最开始的目标特征进行匹配时,默认设置normalized=True,只匹配目标特征的一种大小。
# NOTE: When extracting keypoints to match the first descriptor, we use normalized=True to extract
# keypoints from the first scale only, which will match one of the scales in the first descriptor.
kpts3 = img.find_keypoints(max_keypoints=150, threshold=1, scale_factor=1.2)
#如果检测到特征物体
if (kpts3):
#匹配当前找到的特征和最初的目标特征的相似度
match = image.match_descriptor(kpts1, kpts3, threshold=60)
#image.match_descriptor(descritor0, descriptor1, threshold=70, filter_outliers=False)。本函数返回kptmatch对象。
#threshold阈值设置匹配的准确度,用来过滤掉有歧义的匹配。这个值越小,准确度越高。阈值范围0~100,默认70
#filter_outliers默认关闭。
#match.count()是kpt1和kpt2的匹配的近似特征点数目。
print("%d"%(match.count()))#如果大于10,证明两个特征相似,匹配成功。
if (match.count()>2):
r = match.rect()
flag = 1
flag1 = 1
print(r)
img = img.to_rgb565()
img = sensor.snapshot()
blobs = img.find_blobs([yellow_threshold],roi = r)
if blobs:
max_blob=find_max(blobs)
img.draw_cross(max_blob.cx(), max_blob.cy())
print(max_blob.cx())
print(max_blob.cy())
else:
match = image.match_descriptor(kpts2, kpts3, threshold=60)
if (match.count()>2):
r = match.rect()
flag = 1
flag1 = 1
print(r)
img = img.to_rgb565()
img = sensor.snapshot()
blobs = img.find_blobs([yellow_threshold],roi = r)
if blobs:
max_blob=find_max(blobs)
# img.draw_rectangle(max_blob.rect())
img.draw_cross(max_blob.cx(), max_blob.cy())
print(max_blob.cx())
print(max_blob.cy())你的一个变量是kpts2,一个是kpst2
来找找不同