一下是我根据例程结合的 但是实现不了功能
import sensor, image, time
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()
K=5000#the value should be measured
yellow_threshold = ( 56, 83, 5, 57, 63, 80)
while(True):
clock.tick()
img = sensor.snapshot().lens_corr(1.8)
for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,
r_min = 2, r_max = 100, r_step = 2):
area = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r())
#area为识别到的圆的区域,即圆的外接矩形框
statistics = img.get_statistics(roi=area)#像素颜色统计
#print(statistics)
#(94, 100, -128, 127, -128, 127)是红色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的圆。
#l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。
if 94<statistics.l_mode()<100 and -128<statistics.a_mode()<127 and -128<statistics.b_mode()<127:#if the circle is red
img.draw_rectangle(area, color = (255, 255, 255))
#将非红色的圆用白色的矩形框出来
blobs = img.find_blobs([yellow_threshold])
if len(blobs) == 1:
# Draw a rect around the blob.
b = blobs[0]
img.draw_rectangle(b[0:4]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
Lm = (b[2]+b[3])/2
length = K/Lm
print(Lm)什么叫做实现不了功能?你想做什么?代码有什么错误?
就是我想要同时识别到颜色和形状 然后串口终端返回与物块的距离
现在是识别不到物块 Lm的值也没有返回
代码有问题,如果不只有一个色块,底下的就不会执行。
起码你要加一个find_max之类的。
那我现在应该怎么修改呢?
import sensor, image, time
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
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()
K=5000#the value should be measured
yellow_threshold = (94, 100, -128, 127, -128, 127)
while(True):
clock.tick()
img = sensor.snapshot().lens_corr(1.8)
sensor.set_hmirror(True) #水平方向翻转
sensor.set_vflip(True) #垂直方向翻转
for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,
r_min = 2, r_max = 100, r_step = 2):
area = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r())
#area为识别到的圆的区域,即圆的外接矩形框
statistics = img.get_statistics(roi=area)#像素颜色统计
#print(statistics)
#(94, 100, -128, 127, -128, 127)是红色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的圆。
#l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。
if 94<statistics.l_mode()<100 and -128<statistics.a_mode()<127 and -128<statistics.b_mode()<127:#if the circle is red
img.draw_rectangle(area, color = (255, 255, 255))
#将非红色的圆用白色的矩形框出来
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
blobs = img.find_blobs([yellow_threshold])
if len(blobs) == 1:
# Draw a rect around the blob.
b = blobs[0]
img.draw_rectangle(b[0:4]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
Lm = (b[2]+b[3])/2
length = K/Lm
print(Lm)这是我重新修改的 还是实现不了 麻烦您指教
@4oqs 你这纯复制粘贴。。。。咋会有用呢,k的值没改,距离是不对的,而且输出的是像素,不是距离
另外这个是识别圆形区域的
@egxs emmm……那想问问您 方形的识别该怎么写?能教一下我吗回复
@kidswong999 想请教您 我现在该怎么去修改 或者是重新去写?
@4oqs
import sensor, image, time
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
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()
K=500#the value should be measured
yellow_threshold = (56, 79, 35, 127, -128, 127)
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().lens_corr(1.8)
sensor.set_hmirror(False) #水平方向翻转
sensor.set_vflip(False) #垂直方向翻转
for r in img.find_rects(threshold = 20000):
img.draw_rectangle(r.rect(), color = (255, 0, 0))
area = (r.x(),r.y(),r.w(),r.h())
for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0))
# for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,
# r_min = 2, r_max = 100, r_step = 2):
print (r)
#area为识别到的圆的区域,即圆的外接矩形框
statistics = img.get_statistics(roi=area)#像素颜色统计
#print(statistics)
#(94, 100, -128, 127, -128, 127)是红色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的圆。
#l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。
if 56<statistics.l_mode()<79 and 35<statistics.a_mode()<127 and -128<statistics.b_mode()<127:#if the circle is red
img.draw_rectangle(area, color = (255, 255, 255))
#将非红色的圆用白色的矩形框出来
blobs = img.find_blobs([yellow_threshold])
if len(blobs) == 1:
# Draw a rect around the blob.
max_blob=find_max(blobs)
b = blobs[0]
img.draw_rectangle(b[0:4]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
Lm = (b[2]+b[3])/2
length = K/Lm
#print(Lm)
print(length)你试试看,我这里用的是红色的阈值
@egxs 感谢您!
但是识别起来还是有点飘 会框到其他的地方
@4oqs 在 [我想要将颜色识别、形状识别、测距三种融合在一起可以实现吗?](/post/12649) 中说:
>
> 此回复已被删除!
k值都不改,真的是纯复制粘贴,原理都不没搞清楚,哈哈哈
你好,我想问你的这个问题解决了吗??