openmv代码先进行颜色识别后进行特征点检测,但是颜色识别后,特征点检测进不去。
import sensor, image, time
from pyb import UART
import ustruct
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)
clock = time.clock()
green_threshold = (32, 91, -51, -19, 26, 60)
size_threshold = 2000
def find_max(blobs):
max_size=0
for blob in blobs:
if blob[2]*blob[3] > max_size:
max_blob=blob
max_size = blob[2]*blob[3]
return max_blob
def sending_data(cx,cy):
global uart;
data = ustruct.pack("<bbhhb",
0x2C,
0x12,
int(cx),
int(ck),
0x5B)
uart.write(data);
uart = UART(3,115200)
uart.init(115200,bits=8,parity=None,stop=1)
Flag = 0
while(True):
if Flag == 0:
clock.tick()
img = sensor.snapshot()
blobs = img.find_blobs([green_threshold])
if blobs:
max_blob = find_max(blobs)
img.draw_rectangle(max_blob[0:4])
img.draw_cross(max_blob[5], max_blob[6])
cx = max_blob[5]
ck = 1
FH = bytearray([0x2C,0x12,cx,ck,0x5B])
uart.write(FH)
Flag = 1
else:
ck =0
cx=138
FH =bytearray([0x2C,0x12,cx,ck,0x5B])
uart.write(FH)
elif(Flag == 1):
clock.tick()
kpts1 = image.load_descriptor("/desc.orb")#引入待测物体特征带点
img = sensor.snapshot()
img = img.to_grayscale()
kpts2 = img.find_keypoints(max_keypoints=150, threshold=10, normalized=True)
if(kpts2):
#匹配当前找到的特征和最初的目标特征的相似度
match = image.match_descriptor(kpts1, kpts2, threshold=85)
#image.match_descriptor(descritor0, descriptor1, threshold=70, filter_outliers=False)。本函数返回kptmatch对象。
#threshold阈值设置匹配的准确度,用来过滤掉有歧义的匹配。这个值越小,准确度越高。阈值范围0~100,默认70
#filter_outliers默认关闭。
#match.count()是kpt1和kpt2的匹配的近似特征点数目。
#如果大于10,证明两个特征相似,匹配成功。
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)
Flag = 0
print(kpts2, "matched:%d dt:%d"%(match.count(), match.theta()))