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=660#the value should be measured
yellow_threshold = (9, 62, 9, 65, -80, -7)
def find_max(blobs):
max_size=0
for blob in blobs:
if blob.pixels() > max_size:
max_blob=blob
max_size = blob.pixels()
return max_blob
while(True):
clock.tick()
img = sensor.snapshot()
sensor.set_hmirror(False) #水平方向翻转
sensor.set_vflip(False) #垂直方向翻转
blobs = img.find_blobs([yellow_threshold])
#if not blobs:
for r in img.find_rects(threshold = 30000):
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))
#将非红色的圆用白色的矩形框出来
if blobs :#and len(blobs)==1 :就是这里应该怎么改!!!!!!!!!!!!!!
# Draw a rect around the blob.
max_blob=find_max(blobs)
img.draw_rectangle(max_blob.rect())
img.draw_cross(max_blob.cx(), max_blob.cy())
output_str="[%d,%d]\n" % (max_blob.cx(),max_blob.cy()) #方式1
#output_str=json.dumps([max_blob.cx(),max_blob.cy()]) #方式2
print('you send:',output_str)
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)怎么设置找到我要的颜色阈值并且该物体是矩形,就是一个物体需要满足,是矩形和是我设置的颜色阈值这两个条件才会被识别出来,所以if语句应该怎么写
https://book.openmv.cc/image/blob_circle.html