本章主要介绍基本操作及基本的颜色识别部分。
先用openmv入门视觉,openmv教程较多此文章根据下面相关网站学习所记录。
在星瞳中安装IDE将openmv用安卓数据线与电脑相连(如果供电,注意不能用左侧3V3给openmv供电,供电用右侧的VIN和GND,使用时不能放在电脑上,金属外壳易短接)
在官网安装完成IDE后打开IDE
最右侧那一列居中的箭头点击可以拉出图像,需按左下连接运行才有图像。
将代码14行里的"QVGA"改为“VGA”可以提高摄像头分辨率。
刚使用时需要手动对焦(并不是最里面对焦最好)将摄像头拧出缓慢拧入实时看图像效果,图象最清晰时,将中间的大圈拧紧固定,对焦完成。
脱机运行程序
在tool选择将打开的脚本保存到OpenMV Cam(只能有一个main.py)
供电用右侧的VIN和GND 3.7-3V电压供电(推荐5v)左侧的3V3是openMV给其他模块供电的。
Single Color RGB565 Blob Tracking Example
单颜色彩色识别
thresholds阈值
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
tool-机器视觉-阈值编辑器-帧缓冲区,调节LAB获得最佳的LAB阈值copy到阈值
sensor.reset()//重置光感元件(摄像机)
sensor.set_pixformat(sensor.RGB565)//颜色格式设置为RGB565
sensor.set_framesize(sensor.QVGA)//图片大小为QVGA
颜色识别完整示例代码
# This work is licensed under the MIT license.
# Copyright (c) 2013-2023 OpenMV LLC. All rights reserved.
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# Single Color RGB565 Blob Tracking Example
#
# This example shows off single color RGB565 tracking using the OpenMV Cam.
import sensor
import time
import 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 = [
(34, 49, -112, -16, -128, 100), # 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)
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()
# 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:
clock.tick()
img = sensor.snapshot()
for blob in img.find_blobs(
[thresholds[threshold_index]],
pixels_threshold=200,
area_threshold=200,
merge=True,
):
# 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())
# 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(clock.fps())