• 【OpenMV】AprilTag标记跟踪 NCC模板匹配 测距与测量物体大小


    目录

    AprilTag标记跟踪

    NCC模板匹配

    测距以及测量物体大小

    识别乒乓球的距离与大小 

    红色矩形与蓝色矩形同时识别


    AprilTag标记跟踪

    Tag36h11,Tag25h9,Tag16h5

    Tag36h11信息量更大,更准确

    1. # AprilTags Example
    2. #
    3. # This example shows the power of the OpenMV Cam to detect April Tags
    4. # on the OpenMV Cam M7. The M4 versions cannot detect April Tags.
    5. import sensor, image, time, math
    6. sensor.reset()
    7. sensor.set_pixformat(sensor.RGB565)
    8. sensor.set_framesize(sensor.QQVGA) # we run out of memory if the resolution is much bigger...
    9. sensor.skip_frames(30)
    10. sensor.set_auto_gain(False) # must turn this off to prevent image washout...
    11. sensor.set_auto_whitebal(False) # must turn this off to prevent image washout...关闭白平衡
    12. clock = time.clock()
    13. while(True):
    14. clock.tick()
    15. img = sensor.snapshot()
    16. for tag in img.find_apriltags(): # defaults to TAG36H11 without "families".
    17. img.draw_rectangle(tag.rect(), color = (255, 0, 0)) #画框
    18. img.draw_cross(tag.cx(), tag.cy(), color = (0, 255, 0)) #画十字
    19. degress = 180 * tag.rotation() / math.pi #求April Tags旋转的角度
    20. 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

    1. # Template Matching Example - Normalized Cross Correlation (NCC)
    2. #
    3. # This example shows off how to use the NCC feature of your OpenMV Cam to match
    4. # image patches to parts of an image... expect for extremely controlled enviorments
    5. # NCC is not all to useful.
    6. #
    7. # WARNING: NCC supports needs to be reworked! As of right now this feature needs
    8. # a lot of work to be made into somethin useful. This script will reamin to show
    9. # that the functionality exists, but, in its current state is inadequate.
    10. import time, sensor, image
    11. from image import SEARCH_EX, SEARCH_DS
    12. # Reset sensor
    13. sensor.reset()
    14. # Set sensor settings
    15. sensor.set_contrast(1)
    16. sensor.set_gainceiling(16)
    17. # Max resolution for template matching with SEARCH_EX is QQVGA
    18. sensor.set_framesize(sensor.QQVGA)
    19. # You can set windowing to reduce the search image.
    20. #sensor.set_windowing(((640-80)//2, (480-60)//2, 80, 60))
    21. sensor.set_pixformat(sensor.GRAYSCALE)
    22. # Load template.
    23. # Template should be a small (eg. 32x32 pixels) grayscale image.
    24. template1 = image.Image("/1.pgm")
    25. template2 = image.Image("/2.pgm")
    26. template3 = image.Image("/3.pgm")
    27. clock = time.clock()
    28. # Run template matching
    29. while (True):
    30. clock.tick()
    31. img = sensor.snapshot()
    32. # find_template(template, threshold, [roi, step, search])
    33. # ROI: The region of interest tuple (x, y, w, h).
    34. # Step: The loop step used (y+=step, x+=step) use a bigger step to make it faster.
    35. # Search is either image.SEARCH_EX for exhaustive search or image.SEARCH_DS for diamond search
    36. #
    37. # Note1: ROI has to be smaller than the image and bigger than the template.
    38. # Note2: In diamond search, step and ROI are both ignored.
    39. r1 = img.find_template(template1, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    40. if r1:
    41. img.draw_rectangle(r1, color=(255,0,255))
    42. r2 = img.find_template(template2, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    43. if r2:
    44. img.draw_rectangle(r2)
    45. r3 = img.find_template(template3, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    46. if r3:
    47. img.draw_rectangle(r3)
    48. 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)

    识别乒乓球的距离与大小 

    1. 黄色球形例程代码:
    2. # Measure the distance
    3. #
    4. # This example shows off how to measure the distance through the size in imgage
    5. # This example in particular looks for yellow pingpong ball.
    6. import sensor, image, time
    7. # For color tracking to work really well you should ideally be in a very, very,
    8. # very, controlled enviroment where the lighting is constant...
    9. yellow_threshold = (57, 100, 17, 88, 99, 32) #(37, 92, -5, 52, 89, 37)
    10. # You may need to tweak the above settings for tracking green things...
    11. # Select an area in the Framebuffer to copy the color settings.
    12. sensor.reset() # Initialize the camera sensor.
    13. sensor.set_pixformat(sensor.RGB565) # use RGB565.
    14. sensor.set_framesize(sensor.QVGA) # use QQVGA for speed.
    15. sensor.skip_frames(10) # Let new settings take affect.
    16. sensor.set_auto_whitebal(False) # turn this off. 用颜色识别进行测量,需要关闭白平衡!
    17. clock = time.clock() # Tracks FPS.
    18. K_Distance = 1072 # K = 距离*像素 = 16cm * 67 实际长度和摄像头里的像素成反比
    19. K_size = 0.0522 #实际大小 = K*直径的像素,K=3.5cm/67 实际大小与下摄像头里的像素成正比
    20. while(True):
    21. clock.tick() # Track elapsed milliseconds between snapshots().
    22. img = sensor.snapshot() # Take a picture and return the image.
    23. blobs = img.find_blobs([yellow_threshold])
    24. if len(blobs) == 1:
    25. # Draw a rect around the blob.
    26. b = blobs[0]
    27. img.draw_rectangle(b[0:4]) # rect
    28. img.draw_cross(b[5], b[6]) # cx, cy
    29. Lm = (b[2]+b[3])/2 #(长+宽)/2 = 像素数Lm
    30. Distance = K_Distance / Lm
    31. #print(Lm) # Lm = 67 输出像素数
    32. print(Distance) #输出距离
    33. Size = K_size * Lm
    34. print (Size) #输出大小
    35. #print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while
    36. # connected to your computer. The FPS should increase once disconnected.

    红色矩形与蓝色矩形同时识别

    这里我拿了魔方,拧成上面是6个红色色块,下面三个是蓝色色块,只识别这一面即可

    1. 红色矩形的识别代码
    2. 蓝色矩形的识别代码,返回高度和宽度
    3. # Measure the distance
    4. #
    5. # This example shows off how to measure the distance through the size in imgage
    6. # This example in particular looks for yellow pingpong ball.
    7. import sensor, image, time
    8. # For color tracking to work really well you should ideally be in a very, very,
    9. # very, controlled enviroment where the lighting is constant...
    10. yellow_threshold = (57, 100, 17, 88, 99, 32) #(37, 92, -5, 52, 89, 37)
    11. red_threshold = (15, 61, 48, 116, 53, 19) #红色阈值
    12. blue_threshold = (31, 47, 26, -28, -51, -15) #蓝色阈值
    13. # You may need to tweak the above settings for tracking green things...
    14. # Select an area in the Framebuffer to copy the color settings.
    15. sensor.reset() # Initialize the camera sensor.
    16. sensor.set_pixformat(sensor.RGB565) # use RGB565.
    17. sensor.set_framesize(sensor.QVGA) # use QQVGA for speed.
    18. sensor.skip_frames(10) # Let new settings take affect.
    19. sensor.set_auto_whitebal(False) # turn this off. 用颜色识别进行测量,需要关闭白平衡!
    20. clock = time.clock() # Tracks FPS.
    21. K_Distance = 1072 # K = 距离*像素 = 16cm * 67 实际长度和摄像头里的像素成反比
    22. K_size = 0.0522 #实际大小 = K*直径的像素,K=3.5cm/67 实际大小与下摄像头里的像素成正比
    23. while(True):
    24. clock.tick() # Track elapsed milliseconds between snapshots().
    25. img = sensor.snapshot() # Take a picture and return the image.
    26. blobs = img.find_blobs([yellow_threshold]) #识别黄色
    27. if len(blobs) == 1:
    28. # Draw a rect around the blob.
    29. b = blobs[0]
    30. img.draw_rectangle(b[0:4]) # rect
    31. img.draw_cross(b[5], b[6]) # cx, cy
    32. Lm = (b[2]+b[3])/2 #(长+宽)/2 = 像素数Lm
    33. Distance = K_Distance / Lm
    34. #print(Lm) # Lm = 67 输出像素数
    35. print(Distance) #输出距离
    36. Size = K_size * Lm
    37. print (Size) #输出大小
    38. Bolb2 = img.find_blobs([red_threshold]) #识别红色
    39. if len(Bolb2) == 1:
    40. # Draw a rect around the blob.
    41. blocation = Bolb2[0]
    42. img.draw_rectangle(blocation[0:4]) # rect
    43. img.draw_cross(blocation[5], blocation[6]) # cx, cy
    44. h = K_size * blocation[3]
    45. w = K_size * blocation[2]
    46. print("红色高:%s cm, 红色宽:%s cm " %(h,w))
    47. Bolb3 = img.find_blobs([blue_threshold]) #识别蓝色
    48. if len(Bolb3) == 1:
    49. # Draw a rect around the blob.
    50. local_blue = Bolb3[0]
    51. img.draw_rectangle(local_blue[0:4]) # rect
    52. img.draw_cross(local_blue[5], local_blue[6]) # cx, cy
    53. h = K_size * local_blue[3]
    54. w = K_size * local_blue[2]
    55. print("蓝色高:%s cm, 蓝色宽:%s cm " %(h,w))
    56. #print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while
    57. # connected to your computer. The FPS should increase once disconnected.

    这样就可以同时识别红色和蓝色色块了

  • 相关阅读:
    网络原理---拿捏网络层:IP协议
    React之一些函数或者方法的扩展
    闭包的常见问题
    Python并发编程之Queue队列
    js input 正则保留2位小数中文拼音输入问题 + 限制输入整数的方案
    〖Python 数据库开发实战 - MySQL篇㉔〗- 数据插入操作 - NSERT语句
    <C++>深度学习继承
    风控人千万不能错过的这种经典的策略规则组合
    SQLite 入门教程
    Java抽象类
  • 原文地址:https://blog.csdn.net/weixin_63135906/article/details/131493825