open mv单颜色识别例程
# Single Color RGB565 Blob Tracking Example # # This example shows off single color RGB565 tracking using the OpenMV Cam. import sensor, image, time, math threshold_index = 0 # 0 for red, 1 for green, 2 for blue # Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max) # The below thresholds track in general red/green/blue things. You may wish to tune them... thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds (30, 100, -64, -8, -32, 32), # generic_green_thresholds (0, 30, 0, 64, -128, 0)] # generic_blue_thresholds sensor.reset() #初始化感光元件 sensor.set_pixformat(sensor.RGB565) #设置为彩色 sensor.set_framesize(sensor.QVGA) #设置图像的大小为:320x240 sensor.skip_frames(time = 2000) #跳过100张照片,在更改设置后,跳过一些帧,等待感光元件变稳定。 sensor.set_auto_gain(False) # 使用的是颜色追踪,所以要关闭自动增益 sensor.set_auto_whitebal(False) # 使用的是颜色追踪,所以要关闭自动白平衡 clock = time.clock() #返回一个时钟对象。 # Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are # returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the # camera resolution. "merge=True" merges all overlapping blobs in the image. while(True): #while循环 clock.tick() #开始追踪运行时间。 img = sensor.snapshot() #拍摄一张照片,img为一个image对象 for blob in img.find_blobs([thresholds[threshold_index]], pixels_threshold=200, area_threshold=200, merge=True): #for循环,在find.blogs中寻找色块面积大于200,像素大于200,并且合并blog。 # These values depend on the blob not being circular - otherwise they will be shaky. if blob.elongation() > 0.5: #返回一个介于0和1之间的值,该值表示对象的长度(不是圆形)。一条线将是1。 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()) #在图像中画一个矩形框,blob.rect() 返回这个色块的外框——矩形元组(x, y, w, h),可以直接在image.draw_rectangle中使用。 img.draw_cross(blob.cx(), blob.cy()) #image.draw_cross(x, y, size=5, color=White) 在图像中画一个十字,blob.cx() 返回色块的外框的中心x坐标(int),也可以通过blob[5]来获取,blob.cy() 返回色块的外框的中心y坐标(int),也可以通过blob[6]来获取。 # Note - the blob rotation is unique to 0-180 only. img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20) #在图像上绘制关键点对象的关键点。您还可以传递包含(x, y, rotation_angle_in_degrees)三个值元组的列表, 以重新使用此方法来绘制关键点字形,这些关键字形是具有指向特定方向的线的圆。 #color 是用于灰度或RGB565图像的RGB888元组。默认为白色。但是,您也可以传递灰度图像的基础像素值(0-255)或RGB565图像的字节反转RGB565值。 #size 控制特征点的大小。 #thickness 控制线的粗细像素。 #将 fill 设置为True以填充特征点。 #返回图像对象,以便您可以使用 . 表示法调用另一个方法。 print(clock.fps())
merge 合并,如果设置为True,那么合并所有重叠的blob为一个。若是多颜色识别,有两个框将分别框红色和绿色,merge为True则将这两个框合并为一个框。
open mv识别红颜色圆形例程:
open mv颜色和形状同时识别:
statistics.l_mode():返回RGB5656 LAB 中L的众值(0-255) (int)。 statistics.a_mode():返回RGB5656 LAB 中A的众值(0-255) (int)。 statistics.b_mode():返回RGB5656 LAB 中B的众值(0-255) (int)。 LAB三个通道是什么意思?