logo

为什么二值化本来弄成了是黑白图,重置之后烧同样的程序变成了黑灰图,原来白色的地方变成了灰色,求助

2020-02-27 16:16
I
agxb
THRESHOLD = (32, 0, -20, 14, -20, 20)# Grayscale threshold for dark things... 用阈值编辑器找出阈值
import sensor, image, time
from pyb import LED
#import car
from pid import PID
rho_pid = PID(p=0.4, i=0)##y=ax+b  为b截距  控制线在视野中的位置
theta_pid = PID(p=0.001, i=0)  ##斜率   最终目的是要使直线在我们视野中央

LED(1).on()
LED(2).on()
LED(3).on()#打开openmv自带的补光灯

sensor.reset()##初始化
#sensor.set_vflip(True)##垂直反转
#sensor.set_hmirror(True)##设置镜像 水平翻转
sensor.set_pixformat(sensor.RGB565)##设置彩图
sensor.set_framesize(sensor.QQQVGA) # 80x60 (4,800 pixels) - O(N^2) max = 2,3040,000.分辨率
#sensor.set_windowing([0,20,80,40]) 
sensor.skip_frames(time = 2000)     # WARNING: If you use QQVGA it may take seconds
clock = time.clock()                # to process a frame sometimes.

while(True):
    clock.tick()
    img = sensor.snapshot().binary([THRESHOLD])   ##binary([THRESHOLD]二值化
    line = img.get_regression([(0,100,0,0,0,0)],area_threshold=10,robust = True)##线性回归  (l_lo,l_hi,a_lo,a_hi,b_lo,b_hi)
    if (line):
        rho_err = abs(line.rho())-img.width()/2
        if line.theta()>90:
            theta_err = line.theta()-180
        else:
            theta_err = line.theta()##调整坐标
        img.draw_line(line.line(), color = 127)##画出直线
        print(rho_err,line.magnitude(),line.theta())##theta代表返回线段的角度, rho代表偏移的距离  rho更重要一些,如果不用theta,只用rho也是可以的。
        if line.magnitude()>8:              ##magnitude值越大证明线性回归越好
            #if -40<b_err<40 and -30<t_err<30:
            rho_output = rho_pid.get_pid(rho_err,1)
            theta_output = theta_pid.get_pid(theta_err,1)
            output = rho_output+theta_output
           # car.run(50+output, 50-output)
       # else:
            #car.run(0,0)
   # else:
       # car.run(50,-50)
        pass
    #print(clock.fps())
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')
2020-02-29 17:40
I
网红小智智 星瞳科技

https://singtown.com/openmv-download/

下载最新的IDE,更新到最新的固件就没问题了。

2020-03-01 08:40
I
agxb

@kidswong999 好的,谢谢