多颜色识别的时候,将各颜色矩形框X坐标定义成一个整型,最后输出整型。但是在开始时摄像头视野里必须有这三种颜色,不然会显示某一整型defined.
color_c=blob.cx()
print("红:",color_a,"绿:",color_b,"蓝:",color_c)
我不知道你的问题是什么?
就是运行程序的时候,视野里必须有三种颜色的存在,程序才能正常运行,不然会显示某一个颜色defined.
你的现象很正常。变量未定义。
如果你想解决问题,请提供你想要做什么?
就是在做颜色识别排序,我把各种颜色的矩形框中心坐标分别赋值给a,b,c.然后运行程序的时候就会出现,当前摄像头视野里如果没有这几种颜色,他就说我未定义,比如我b赋值的是绿色阈值的块中心X坐标,然后我画面里没有绿色,程序就会报未定义的错。
肯定是你的逻辑有问题导致的。
如果涉及代码,需要报错提示与全部代码文本,请注意不要贴代码图片
import sensor, image, time, math
# 颜色跟踪阈值(L Min, L Max, A Min, A Max, B Min, B Max)
# 下面的阈值跟踪一般红色/绿色的东西。你不妨调整他们...
red_threshold=(45, 79, 10, 127, -128, 127) # generic_red_thresholds
green_threshold=(38, 75, -128, -35, -128, 127) # generic_green_thresholds
blue_threshold=(9, 54, -128, 20, -120, 1) # generic_blue_thresholds
# 您最多可以传递16个阈值。
# 但是,在颜色阈值开始重叠之前,实际上不可能使用16个阈值对任何场景进行分段。
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()
# 只有比“pixel_threshold”多的像素和多于“area_threshold”的区域才被
# 下面的“find_blobs”返回。 如果更改相机分辨率,
# 请更改“pixels_threshold”和“area_threshold”。 “merge = True”合并图像中所有重叠的色块。
while(True):
clock.tick()
img = sensor.snapshot()
for code in img.find_qrcodes():
print(code)
for blob in img.find_blobs([red_threshold], pixels_threshold=500, area_threshold=200):
# These values depend on the blob not being circular - otherwise they will be shaky.
if blob.elongation() > 0.5:
img.draw_edges(blob.min_corners(), color=(255,0,0))
img.draw_line(blob.major_axis_line(), color=(0,255,0))
img.draw_line(blob.minor_axis_line(), color=(0,0,255))
# These values are stable all the time.
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
a=blob.cx()
# Note - the blob rotation is unique to 0-180 only.
img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)
for blob in img.find_blobs([green_threshold], pixels_threshold=500, area_threshold=200):
# These values depend on the blob not being circular - otherwise they will be shaky.
if blob.elongation() > 0.5:
img.draw_edges(blob.min_corners(), color=(255,0,0))
img.draw_line(blob.major_axis_line(), color=(0,255,0))
img.draw_line(blob.minor_axis_line(), color=(0,0,255))
# These values are stable all the time.
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
b=blob.cx()
# Note - the blob rotation is unique to 0-180 only.
img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)
for blob in img.find_blobs([blue_threshold], pixels_threshold=500, area_threshold=200):
# These values depend on the blob not being circular - otherwise they will be shaky.
if blob.elongation() > 0.5:
img.draw_edges(blob.min_corners(), color=(255,0,0))
img.draw_line(blob.major_axis_line(), color=(0,255,0))
img.draw_line(blob.minor_axis_line(), color=(0,0,255))
# These values are stable all the time.
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
c=blob.cx()
# Note - the blob rotation is unique to 0-180 only.
img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)
print("红:",a,"绿:",b,"蓝:",c)
import time
from pyb import UART
uart = UART(3, 19200)
while(True):
uart.write(code.payload())
maxn=max(a,b,c)
minn=min(a,b,c)
mn=a+b+c-maxn-minn
if maxn == a and minn == b:
uart.write("123")
elif maxn == a and minn == c:
uart.write ("132")
elif maxn == b and minn == a:
uart.write("231")
elif maxn == b and minn == c:
uart.write ("213")
elif maxn == c and minn == a:
uart.write("321")
elif maxn == c and minn == b:
uart.write ("312")
time.sleep(1000)新手哈哈,有点菜的代码。
两个死循环,后面的永远不会运行。
代码实在太渣。
看一下视频:https://singtown.com/learn/50029/
@kidswong999 主要是为什么会先去图像里找颜色再进行输出。
@kidswong999 代码我把没用的删了,求指导
import sensor, image, time, math
from pyb import UART
red_threshold=(0, 58, 42, 75, 21, 127)
green_threshold=(44, 100, -17, -128, 6, 127)
blue_threshold=(0, 37, 5, 33, -63, -17)
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)
clock = time.clock()
while(True):
clock.tick()
img = sensor.snapshot().lens_corr(strength = 1.8, zoom = 1.0)
for code in img.find_qrcodes():
print(code)
for blob in img.find_blobs([red_threshold], pixels_threshold=100, area_threshold=100):
if blob.elongation() > 0.5:
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
a=blob.cx()
for blob in img.find_blobs([green_threshold], pixels_threshold=100, area_threshold=100):
if blob.elongation() > 0.5:
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
b=blob.cx()
for blob in img.find_blobs([blue_threshold], pixels_threshold=100, area_threshold=100):
if blob.elongation() > 0.5:
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
c=blob.cx()
print("红:",a,"绿:",b,"蓝:",c)
uart = UART(3, 19200)
while(True):
uart.write(code.payload())
maxn=max(a,b,c)
minn=min(a,b,c)
mn=a+b+c-maxn-minn
if maxn == a and minn == b:
uart.write("123")
elif maxn == a and minn == c:
uart.write ("132")
elif maxn == b and minn == a:
uart.write("231")
elif maxn == b and minn == c:
uart.write ("213")
elif maxn == c and minn == a:
uart.write("321")
elif maxn == c and minn == b:
uart.write ("312")
time.sleep(1000)@kidswong999 救救孩子。