
上图红色框分别是识别到的Apritag,及他输出的x,感觉x和实际Apritag在图像中的位置偏差很大啊
如果涉及代码,需要报错提示与全部代码文本,请注意不要贴代码图片
import sensor, image, time,mjpeg, math,pyb,tv
from struct import pack , unpack
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
tv.init()
tv.channel(8)
clock = time.clock()
uart = pyb.UART(3, 115200, timeout_char = 1000)
packet_sequence = 0
flag = False
n = 0;
def checksum(data, extra):
output = 0xFFFF
for i in range(len(data)):
tmp = data[i] ^ (output & 0xFF)
tmp = (tmp ^ (tmp << 4)) & 0xFF
output = ((output >> 8) ^ (tmp << 8) ^ (tmp << 3) ^ (tmp >> 4)) & 0xFFFF
tmp = extra ^ (output & 0xFF)
tmp = (tmp ^ (tmp << 4)) & 0xFF
output = ((output >> 8) ^ (tmp << 8) ^ (tmp << 3) ^ (tmp >> 4)) & 0xFFFF
return output
def set_position_target_local_ned_send(time_boot_ms, target_system, target_component, coordinate_frame, type_mask, x, y, z, vx, vy, vz, afx, afy, afz, yaw, yaw_rate, force_mavlink1=False):
seq = 0xb
global packet_sequence
tmp=pack("<5BIfffffffffffHBBB",0x35,packet_sequence,target_system, target_component,0x54,time_boot_ms,x,y,z,vx,vy,vz,afx, afy, afz, yaw, yaw_rate,type_mask,0, 0, coordinate_frame)
crc = checksum(tmp,143)
tmp= pack("<b58sh",0xfe,tmp,crc)
packet_sequence += 1
uart.write(tmp)
def command_long_send( target_system, target_component, yaw):
sign = 1
if yaw<0:
sign=-1
yaw=-yaw
seq = 0xe
global packet_sequence
tmp=pack("<5BfffffffHBBB",0x21,seq,target_system, target_component,0x4c,yaw,0,sign,1,0,0,0,0x73,0, 0)
crc = checksum(tmp,152)
tmp= pack("<b38sh",0xfe,tmp,crc)
packet_sequence += 1
uart.write(tmp)
def data_encode(x,y,z,roll,pitch,yaw):
global flag
flag = not flag
if flag:
set_position_target_local_ned_send(
0,
0xff,
0,
8,
0xdf8,
x,y,z,
roll,pitch,yaw,
0, 0, 0,
0, 0.9)
else:
if yaw>180:
yaw=yaw-360
if yaw >5:
yaw = 5
elif yaw<-5:
yaw = -5
else:
yaw = 0
command_long_send( 0xff, 0, yaw)
f_x = (2.8 / 3.6736) * 160
f_y = (2.8 / 2.7384) * 120
c_x = 160 * 0.5
c_y = 120 * 0.5
Depth = 10000000
tag_families = 0
tag_families |= image.TAG36H11
m = mjpeg.Mjpeg("example.mjpeg")
def family_name(tag):
if(tag.family() == image.TAG36H11):
return 112
valid_tag_ids = {
0 : 432,
1 : 144,
2 : 48,
}
def degrees(radians):
return (180 * radians) / math.pi
while(True):
clock.tick()
img = sensor.snapshot()
n = n + 1
apriltagInfor = img.find_apriltags(families=tag_families,fx=f_x, fy=f_y, cx=c_x, cy=c_y)
x = 0
y = 0
z = 0
xdegrees = 0
ydegreess = 0
zdegreess = 0
num = len(apriltagInfor)
idMax = 1000
iSigned = 0
DepthMin = 100000
if num > 0:
for tagMax in apriltagInfor:
ID = tagMax.id()
if ID == 0:
Depth0 = tagMax.z_translation()*(-512.4)
if DepthMin > Depth0:
DepthMin = Depth0
idMax = 0
elif ID == 1:
Depth1 = tagMax.z_translation() * (-165.5)
#print("D=%f"%Depth1)
if DepthMin > Depth1:
DepthMin = Depth1
idMax = 1
elif ID == 2:
Depth2 = tagMax.z_translation() * (-53.3)
#Depth3 = tagMax.x_translation() * (1)
#Depth4 = tagMax.y_translation() * (1)
#print("x=%f"%Depth3)
#print("y=%f"%Depth4)
if DepthMin > Depth2:
DepthMin = Depth2
idMax = 2
if (DepthMin - Depth > 200 or Depth > DepthMin):
Depth = DepthMin
if num > 0:
for tag in apriltagInfor:
ID = tag.id()
if Depth > 3000:
if ID == 0:
img.draw_rectangle(tag.rect(), color = (255, 0, 0))
img.draw_cross(tag.cx(), tag.cy(), color = (0, 255, 0))
x = tag.x_translation() * (-104.9)#-180
y = tag.y_translation() * (-108.7)#-190.3
z = tag.z_translation() * (-512.4)#-259.9
xdegrees = degrees(tag.x_rotation())
ydegreess = degrees(tag.y_rotation())
zdegreess = degrees(tag.z_rotation())
data_encode(x,y,z,xdegrees,ydegreess,zdegreess)
print_args = (x, y, z, \
degrees(tag.x_rotation()), degrees(tag.y_rotation()), degrees(tag.z_rotation()))
else:
img.draw_rectangle(tag.rect(), color = (0, 255, 0))
img.draw_cross(tag.cx(), tag.cy(), color = (0, 0, 255))
elif(Depth <= 3000) and (Depth > 800):
if ID == 1:
img.draw_rectangle(tag.rect(), color = (255, 0, 0))
img.draw_cross(tag.cx(), tag.cy(), color = (0, 255, 0))
x = tag.x_translation() * (-33.4)#-180
y = tag.y_translation() * (-36.5)#-86.6
z = tag.z_translation() * (-165.5)#104
xdegrees = degrees(tag.x_rotation())
ydegreess = degrees(tag.y_rotation())
zdegreess = degrees(tag.z_rotation())
data_encode(x,y,z,xdegrees,ydegreess,zdegreess)
print_args = (x, y, z, \
degrees(tag.x_rotation()), degrees(tag.y_rotation()), degrees(tag.z_rotation()))
else:
img.draw_rectangle(tag.rect(), color = (0, 255, 0))
img.draw_cross(tag.cx(), tag.cy(), color = (0, 0, 255))
elif(Depth <= 800):
if ID == 2:
img.draw_rectangle(tag.rect(), color = (255, 0, 0))
img.draw_cross(tag.cx(), tag.cy(), color = (0, 255, 0))
x = tag.x_translation() * (-11.6)#-180
y = tag.y_translation() * (-11.8)#-190.3
z = tag.z_translation() * (-53.3)#-28.9
xdegrees = degrees(tag.x_rotation())
ydegreess = degrees(tag.y_rotation())
zdegreess = degrees(tag.z_rotation())
data_encode(x,y,z,xdegrees,ydegreess,zdegreess)
print_args = (x, y, z, \
degrees(tag.x_rotation()), degrees(tag.y_rotation()), degrees(tag.z_rotation()))
else:
img.draw_rectangle(tag.rect(), color = (0, 255, 0))
img.draw_cross(tag.cx(), tag.cy(), color = (0, 0, 255))
tv.display(img)
if n <=10000:
m.add_frame(img)
if n == 10000:
m.close(clock.fps())
#print("n = %d"%n)
#print(clock.fps())
输出的位置信息和实际的位置信息偏差很大有没有测试步骤?你的代码我不能直接看到输出。
或者提供最小的可测试的代码。
@kidswong999

apritag明显不在中间,输出的x的值约等于0,这偏差这么大
什么原因,我买了几块板子都是这样。
第72行和73行明显不对,你分辨率设置的是320*240,怎么c_x和c_y是160 * 0.5和120 * 0.5?
应该是160 * 0.5和120 * 0.5
而且如果代码有问题,应该先测试例子,对比就知道自己的代码哪里有问题了。
https://book.openmv.cc/example/16-Codes/find-apriltags-3d-pose.html
@kidswong999 改成320* 0.5和240 * 0.5,那70和71行是不是也要改啊?
@fr5c 要改的。