import sensor, image, time
line_threshold = (2, 51, -21, 65, -17, 18)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)
clock = time.clock()
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
while(True):
clock.tick()
img = sensor.snapshot()
blobs = img.find_blobs([line_threshold], x_stride=10)
if blobs:
max_blob=find_max(blobs)
if int(max_blob.w()/max_blob.h())>5 and max_blob.w()>200:
img.draw_rectangle(max_blob[0:4])
img.draw_cross(max_blob[5], max_blob[6])
print(max_blob[5], max_blob[6])
print(clock.fps())
3
3icr 发布的帖子
-
openmv在识别色块中为什么一直出现这种错误
-
openmv在光流的绝对旋转变换例程中,出现完美的夹具,这个夹具具体是指什么
# 光流绝对旋转变换示例 # # 此示例显示使用OpenMV Cam通过将当前图像与先前图像相互比较来测量旋转/缩放。 # 请注意,在此模式下仅处理旋转/缩放 - 而不是X和Y平移。 # 要有效地运行此演示,请将OpenMV安装在稳固的底座上, # 然后 慢慢地 围绕镜头旋转摄像机,并向前/向后移动摄像机以查看数字的变化。 # 即仅改变z方向。 import sensor, image, time, math # 注意!!! 使用find_displacement()时,必须使用2的幂次方分辨率。 # 这是因为该算法由称为相位相关的东西提供动力,该相位相关使用FFT进行图像比较。 # 非2的幂次方分辨率要求填充到2的幂,这降低了算法结果的有用性。 # 请使用像B64X64或B64X32这样的分辨率(快2倍)。 # 您的OpenMV Cam支持2的幂次方分辨率64x32,64x64,128x64和128x128。 # 如果您想要32x32的分辨率,可以通过在64x64图像上执行“img.pool(2,2)”来创建它。 sensor.reset() # Reset and initialize the sensor. sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE) sensor.set_framesize(sensor.B64X64) # Set frame size to 64x64... (or 64x32)... sensor.skip_frames(time = 2000) # Wait for settings take effect. clock = time.clock() # Create a clock object to track the FPS. # 从主帧缓冲区的RAM中取出以分配第二帧缓冲区。 # 帧缓冲区中的RAM比MicroPython堆中的RAM多得多。 # 但是,在执行此操作后,您的某些算法的RAM会少得多...... # 所以,请注意现在摆脱RAM问题要容易得多。 extra_fb = sensor.alloc_extra_fb(sensor.width(), sensor.height(), sensor.RGB565) extra_fb.replace(sensor.snapshot()) while(True): clock.tick() # Track elapsed milliseconds between snapshots(). img = sensor.snapshot() # Take a picture and return the image. # 如果没有完美的夹具,这个算法很难测试......所以,这是一个让它看起来有效的骗子... # 在下面输入一个z_rotation值,你应该看到r的输出等于它。 if(0): expected_rotation = 20.0 img.rotation_corr(z_rotation=expected_rotation) # 如果没有完美的夹具,这个算法很难测试......所以,这是一个让它看起来有效的骗子... # 在下面输入一个z_rotation值,你应该看到r的输出等于它。 if(0): expected_zoom = 0.8 img.rotation_corr(zoom=expected_zoom) # 对于此示例,我们从不更新旧图像以测量绝对变化。 displacement = extra_fb.find_displacement(img, logpolar=True) # 没有滤波,偏移结果是嘈杂的,所以我们降低了一些精度。 rotation_change = int(math.degrees(displacement.rotation()) * 5) / 5.0 zoom_amount = displacement.scale() if(displacement.response() > 0.1): # 低于0.1左右(YMMV),结果只是噪音。 print("{0:+f}r {1:+f}z {2} {3} FPS".format(rotation_change, zoom_amount, \ displacement.response(), clock.fps())) else: print(clock.fps())
-
openmv收数据的问题
当openmv的串口没有收到数据时,为什么print函数一直现实的是一
# Blob Detection and uart transport import sensor, image, time,pyb from pyb import UART import json # For color tracking to work really well you should ideally be in a very, very, # very, controlled enviroment where the lighting is constant... yellow_threshold = (65, 100, -10, 6, 24, 51) # You may need to tweak the above settings for tracking green things... # Select an area in the Framebuffer to copy the color settings. #sensor.reset() # Initialize the camera sensor. #sensor.set_pixformat(sensor.RGB565) # use RGB565. #sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed. #sensor.skip_frames(10) # Let new settings take affect. #sensor.set_auto_whitebal(False) # turn this off. clock = time.clock() # Tracks FPS. led = pyb.LED(3) uart = UART(3, 9600) i=0 while(True): i=uart.readchar() b=int(uart.readchar()) print(b)
-
openmv中云台中pid函数有一点看不懂,请求指点
openmv中云台中pid函数中,有一处“output += error * self._kp ”,self._kp的值一直为零,那么output的值也一直为零,此时pid函数的意义何在?我实在看不懂,请求指点
from pyb import millis from math import pi, isnan class PID: _kp = _ki = _kd = _integrator = _imax = 0 _last_error = _last_derivative = _last_t = 0 #定义私有属性 _RC = 1/(2 * pi * 20) def __init__(self, p=0, i=0, d=0, imax=0): #前后双下划线,魔法属性 self._kp = float(p) self._ki = float(i) self._kd = float(d) self._imax = abs(imax) self._last_derivative = float('nan') #最常见的计算有 无穷大 减 无穷大 结果为 nan def get_pid(self, error, scaler): tnow = millis() #插件重置后,返回毫秒数。 dt = tnow - self._last_t #这次时间与上次时间的差 output = 0 if self._last_t == 0 or dt > 1000: dt = 0 self.reset_I() self._last_t = tnow delta_time = float(dt) / float(1000) output += error * self._kp #self.kp=0,output=0? if abs(self._kd) > 0 and dt > 0: if isnan(self._last_derivative): derivative = 0 self._last_derivative = 0 else: derivative = (error - self._last_error) / delta_time derivative = self._last_derivative + \ ((delta_time / (self._RC + delta_time)) * \ (derivative - self._last_derivative)) self._last_error = error self._last_derivative = derivative output += self._kd * derivative output *= scaler if abs(self._ki) > 0 and dt > 0: self._integrator += (error * self._ki) * scaler * delta_time if self._integrator < -self._imax: self._integrator = -self._imax elif self._integrator > self._imax: self._integrator = self._imax output += self._integrator return output def reset_I(self): self._integrator = 0 self._last_derivative = float('nan') ![0_1562652603774_捕获.PNG](https://fcdn.singtown.com/00f312db-c421-456d-bfe0-726c28b68fdb.PNG)
-
openmv中pid函数一直报错
从GitHub中复制过来的pid函数一直有问题。
from pyb import millis from math import pi, isnan class PID: _kp = _ki = _kd = _integrator = _imax = 0 _last_error = _last_derivative = _last_t = 0 _RC = 1/(2 * pi * 20) def __init__(self, p=0, i=0, d=0, imax=0): self._kp = float(p) self._ki = float(i) self._kd = float(d) self._imax = abs(imax) self._last_derivative = float('nan') def get_pid(self, error, scaler): tnow = millis() dt = tnow - self._last_t output = 0 if self._last_t == 0 or dt > 1000: dt = 0 self.reset_I() self._last_t = tnow delta_time = float(dt) / float(1000) output += error * self._kp if abs(self._kd) > 0 and dt > 0: if isnan(self._last_derivative): derivative = 0 self._last_derivative = 0 else: derivative = (error - self._last_error) / delta_time derivative = self._last_derivative + \ ((delta_time / (self._RC + delta_time)) * \ (derivative - self._last_derivative)) self._last_error = error self._last_derivative = derivative output += self._kd * derivative output *= scaler if abs(self._ki) > 0 and dt > 0: self._integrator += (error * self._ki) * scaler * delta_time if self._integrator < -self._imax: self._integrator = -self._imax elif self._integrator > self._imax: self._integrator = self._imax output += self._integrator return output def reset_I(self): self._integrator = 0 self._last_derivative = float('nan')
-
openmv串口处理数据问题
在openmv中,当外界通过串口向openmv发送‘1’时,openmv会一直开启工作模式,但是在我写的程序中,在串口接收到数据中为什么只有执行一次,为什么不能无限执行
import sensor, image, time from pyb import UART uart = UART(3, 115200) green_threshold = (14, 80, 19, -25, -67, -24) #(35, 75, -7, -22, -47, -20) #(36, 76, -88, 65, -110, -21) #(27, 80, 15, -22, -47, -20) #(18, 74, 53, -91, -78, -24) sensor.reset() # Initialize the camera sensor. sensor.set_pixformat(sensor.RGB565) # use RGB565. sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed. sensor.skip_frames(10) # Let new settings take affect. sensor.set_auto_whitebal(False) # turn this off. #关闭白平衡。白平衡是默认开启的,在颜色识别中,需要关闭白平衡。 def delay1():#对画面进行延迟,不影响ide中的画面流畅度,并对串口发送数据进行延迟 for i in range(45): sensor.snapshot() if i == 42: uart.write('1') def delay2():#对画面进行延迟,不影响ide中的画面流畅度,并对串口发送数据进行延迟 for i in range(45): sensor.snapshot() if i == 42: uart.write('2') def delay3():#对画面进行延迟,不影响ide中的画面流畅度,并对串口发送数据进行延迟 for i in range(45): sensor.snapshot() if i == 42: uart.write('3') def delay4():#对画面进行延迟,不影响ide中的画面流畅度,并对串口发送数据进行延迟 for i in range(45): sensor.snapshot() if i == 42: uart.write('4') def delay5():#对画面进行延迟,不影响ide中的画面流畅度,并对串口发送数据进行延迟 for i in range(45): sensor.snapshot() if i == 42: uart.write('5') clock = time.clock() # Tracks FPS. def find_c(threshold,roi): number=0 max_size=0 blobs = img.find_blobs(threshold,roi=roi,x_stride=10, y_stride=50) for b in blobs: if b[2]*b[3] > max_size: max_size = b[2]*b[3] if max_size>200 and b[2]>20: if b[3]>20 and b[2]>20: max_size=max_size img.draw_rectangle(b[0:4], color = (0, 0, 0)) img.draw_cross(b[5], b[6], color = (0, 0, 0)) number=b[5] return number def read(): a=0 if uart.any(): a=int(uart.read()) print(a) return a def find_max(blobs): max_size=0 max_blob=[] for blob in blobs: if blob[2]*blob[3] > max_size: max_blob=blob max_size = blob[2]*blob[3] return max_blob def max_area(blobs): max_size=0 max_blob=[] for blob in blobs: if blob[2]*blob[3] > max_size: max_blob=blob max_size = blob[2]*blob[3] return max_size t=0 b=0 a=1 while(True): clock.tick() # Track elapsed milliseconds between snapshots(). img = sensor.snapshot() # Take a picture and return the image. blobs = img.find_blobs([green_threshold]) roi1=[0,0,40,120] roi2=[120,0,40,120] blobs3 = img.find_blobs([green_threshold],roi=roi2) blobs2 = img.find_blobs([green_threshold],roi=roi1) #if uart.any(): # a=int(uart.read()) # print(a) #return a if b==0: a=read() if a==1: b=1 elif a==2: b=2 elif a==3: b=3 #if blobs: if b==1 and t==0: #print(111) if blobs: print(111) for b in blobs: max_blob = find_max(blobs) if max_blob[2]*max_blob[3]>1500 : area=max_blob[2]*max_blob[3] img.draw_rectangle(max_blob[0:4]) #print(max_blob[5]) if 70<max_blob[5]<90 : print(max_blob[2]) if max_blob[2]>70: delay1() print(1) t=t+1 else: print(4) delay4() elif max_blob[5]<71 : print(2) delay2() elif max_blob[5]>91: print(3) delay3() if b==2 : print(666) blobs = img.find_blobs([green_threshold]) if blobs: for b in blobs: max_blob = find_max(blobs) if max_blob[2]*max_blob[3]>1500 : print(5) delay5() t=t+1 #print(5) #xdelay5() if b==3 : print(1)
-
RE: openmv串口检测到数据
@kidswong999 def average_zhi(num,n):
sum=0
a=[]
i=0
if num:
a.append(num)
i=i+1
if i==n-1:
a.sort()
for i in a[1,b-1]:
sum=sum+i
c=b-2
sum=int(sum/c)return sum
这个函数为什么一直返回零