目录
AprilTag标记跟踪
NCC模板匹配
测距以及测量物体大小
识别乒乓球的距离与大小
红色矩形与蓝色矩形同时识别
AprilTag标记跟踪
Tag36h11,Tag25h9,Tag16h5
Tag36h11信息量更大,更准确
# AprilTags Example
#
# This example shows the power of the OpenMV Cam to detect April Tags
# on the OpenMV Cam M7. The M4 versions cannot detect April Tags.
import sensor, image, time, math
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA) # we run out of memory if the resolution is much bigger...
sensor.skip_frames(30)
sensor.set_auto_gain(False) # must turn this off to prevent image washout...
sensor.set_auto_whitebal(False) # must turn this off to prevent image washout...关闭白平衡
clock = time.clock()
while(True):
clock.tick()
img = sensor.snapshot()
for tag in img.find_apriltags(): # defaults to TAG36H11 without "families".
img.draw_rectangle(tag.rect(), color = (255, 0, 0)) #画框
img.draw_cross(tag.cx(), tag.cy(), color = (0, 255, 0)) #画十字
degress = 180 * tag.rotation() / math.pi #求April Tags旋转的角度
print(tag.id(),degress)
NCC模板匹配
使用方法:
模板匹配需要插内存卡(方向:摄像头对准自己,看到内存卡背面)
用helloworld.py例程拍照
将1 2 3 4 5 6 数字的图片拍下来,就是保留住画面,然后终止脚本
用鼠标框起来所要识别的数字,右键框框,保存到PC,选择一个位置,保存图像的格式是bmp格式
百度搜一个bmp转pgm图像格式的网站,将图像转换过去,网站如下:
https://onlineconvertfree.com/zh/convert-format/bmp-to-pgm/
将图片导出,保存到SD卡中
修改29~30行代码
template1 = image.Image("/1.pgm")
template2 = image.Image("/2.pgm")
template3 = image.Image("/3.pgm")
修改图像的名称
最后
多循环几次
r1
r2
r3
# Template Matching Example - Normalized Cross Correlation (NCC)
#
# This example shows off how to use the NCC feature of your OpenMV Cam to match
# image patches to parts of an image... expect for extremely controlled enviorments
# NCC is not all to useful.
#
# WARNING: NCC supports needs to be reworked! As of right now this feature needs
# a lot of work to be made into somethin useful. This script will reamin to show
# that the functionality exists, but, in its current state is inadequate.
import time, sensor, image
from image import SEARCH_EX, SEARCH_DS
# Reset sensor
sensor.reset()
# Set sensor settings
sensor.set_contrast(1)
sensor.set_gainceiling(16)
# Max resolution for template matching with SEARCH_EX is QQVGA
sensor.set_framesize(sensor.QQVGA)
# You can set windowing to reduce the search image.
#sensor.set_windowing(((640-80)//2, (480-60)//2, 80, 60))
sensor.set_pixformat(sensor.GRAYSCALE)
# Load template.
# Template should be a small (eg. 32x32 pixels) grayscale image.
template1 = image.Image("/1.pgm")
template2 = image.Image("/2.pgm")
template3 = image.Image("/3.pgm")
clock = time.clock()
# Run template matching
while (True):
clock.tick()
img = sensor.snapshot()
# find_template(template, threshold, [roi, step, search])
# ROI: The region of interest tuple (x, y, w, h).
# Step: The loop step used (y+=step, x+=step) use a bigger step to make it faster.
# Search is either image.SEARCH_EX for exhaustive search or image.SEARCH_DS for diamond search
#
# Note1: ROI has to be smaller than the image and bigger than the template.
# Note2: In diamond search, step and ROI are both ignored.
r1 = img.find_template(template1, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
if r1:
img.draw_rectangle(r1, color=(255,0,255))
r2 = img.find_template(template2, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
if r2:
img.draw_rectangle(r2)
r3 = img.find_template(template3, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
if r3:
img.draw_rectangle(r3)
print(clock.fps())
学习视频链接:
OpenMV使用NCC模板匹配 | 星瞳科技 (singtown.com)
OpenMV使用AprilTag标记追踪 | 星瞳科技 (singtown.com)
测距以及测量物体大小
根据测量的像素点数,求出比例K,进而计算出距离或者物体大小
例程:
距离 = 一个常数K / 直径的像素Lm
先用这个公式求出K:
具体操作:
print(Lm) # Lm = 67 输出像素数
print 输出 Lm=67 距离=16,所以K=16cm*67=1072
定义:(写在while(True):上面)
K_Distance = 1072
然后计算距离,输出距离:
Distance = K_Distance / Lm
print(Distance)
测量物体大小,距离摄像头越近,像素数越大,是正比关系,所以 实际大小 = K*直径的像素
定义:
K_size = 0.143 #实际大小 = K*直径的像素,K=3.5cm/67 实际大小与下摄像头里的像素成正比
添加代码:
Size = K_size * Lm
print (Size)
识别乒乓球的距离与大小
黄色球形例程代码:
# Measure the distance
#
# This example shows off how to measure the distance through the size in imgage
# This example in particular looks for yellow pingpong ball.
import sensor, image, time
# For color tracking to work really well you should ideally be in a very, very,
# very, controlled enviroment where the lighting is constant...
yellow_threshold = (57, 100, 17, 88, 99, 32) #(37, 92, -5, 52, 89, 37)
# You may need to tweak the above settings for tracking green things...
# Select an area in the Framebuffer to copy the color settings.
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QVGA) # use QQVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off. 用颜色识别进行测量,需要关闭白平衡!
clock = time.clock() # Tracks FPS.
K_Distance = 1072 # K = 距离*像素 = 16cm * 67 实际长度和摄像头里的像素成反比
K_size = 0.0522 #实际大小 = K*直径的像素,K=3.5cm/67 实际大小与下摄像头里的像素成正比
while(True):
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image.
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 #(长+宽)/2 = 像素数Lm
Distance = K_Distance / Lm
#print(Lm) # Lm = 67 输出像素数
print(Distance) #输出距离
Size = K_size * Lm
print (Size) #输出大小
#print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while
# connected to your computer. The FPS should increase once disconnected.
红色矩形与蓝色矩形同时识别
这里我拿了魔方,拧成上面是6个红色色块,下面三个是蓝色色块,只识别这一面即可
红色矩形的识别代码
蓝色矩形的识别代码,返回高度和宽度
# Measure the distance
#
# This example shows off how to measure the distance through the size in imgage
# This example in particular looks for yellow pingpong ball.
import sensor, image, time
# For color tracking to work really well you should ideally be in a very, very,
# very, controlled enviroment where the lighting is constant...
yellow_threshold = (57, 100, 17, 88, 99, 32) #(37, 92, -5, 52, 89, 37)
red_threshold = (15, 61, 48, 116, 53, 19) #红色阈值
blue_threshold = (31, 47, 26, -28, -51, -15) #蓝色阈值
# You may need to tweak the above settings for tracking green things...
# Select an area in the Framebuffer to copy the color settings.
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QVGA) # use QQVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off. 用颜色识别进行测量,需要关闭白平衡!
clock = time.clock() # Tracks FPS.
K_Distance = 1072 # K = 距离*像素 = 16cm * 67 实际长度和摄像头里的像素成反比
K_size = 0.0522 #实际大小 = K*直径的像素,K=3.5cm/67 实际大小与下摄像头里的像素成正比
while(True):
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image.
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 #(长+宽)/2 = 像素数Lm
Distance = K_Distance / Lm
#print(Lm) # Lm = 67 输出像素数
print(Distance) #输出距离
Size = K_size * Lm
print (Size) #输出大小
Bolb2 = img.find_blobs([red_threshold]) #识别红色
if len(Bolb2) == 1:
# Draw a rect around the blob.
blocation = Bolb2[0]
img.draw_rectangle(blocation[0:4]) # rect
img.draw_cross(blocation[5], blocation[6]) # cx, cy
h = K_size * blocation[3]
w = K_size * blocation[2]
print("红色高:%s cm, 红色宽:%s cm " %(h,w))
Bolb3 = img.find_blobs([blue_threshold]) #识别蓝色
if len(Bolb3) == 1:
# Draw a rect around the blob.
local_blue = Bolb3[0]
img.draw_rectangle(local_blue[0:4]) # rect
img.draw_cross(local_blue[5], local_blue[6]) # cx, cy
h = K_size * local_blue[3]
w = K_size * local_blue[2]
print("蓝色高:%s cm, 蓝色宽:%s cm " %(h,w))
#print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while
# connected to your computer. The FPS should increase once disconnected.
这样就可以同时识别红色和蓝色色块了