当前位置:   article > 正文

2021全国电设(F题)openmv的图像识别之数字识别_openmv数字识别

openmv数字识别

基于openmv的图像识别

通过参加全国电子设计大赛F题总结出openmv4的数字识别(其它版本暂时没试过,欢迎交流!)

openmv简介

  OpenMV是一个开源,低成本,功能强大的机器视觉模块,以STM32F427CPU为核心,集成了OV7725摄像头芯片,在小巧的硬件模块上,用C语言高效地实现了核心机器视觉算法,提供Python编程接口 。同时 OpenMV也是一个可编程的摄像头,通过Python语言可实现你想要的逻辑。而且摄像头本身也内置了一些图像处理的算法,使用起来也更加的方便,仅需要写一些简单的Python代码,即可轻松的完成各种机器视觉相关的任务。

  openmv4运行内存只有1MB,而openmv4plus运行内存有32MB。

一、打开OpenMV IDE软件

 选择Cancel(不升级即可)!

二、openmv4的数字识别

openmv4的数字识别的基础是需要配置使用NCC模板匹配。通过NCC模板的匹配可把

需要识别的数字模板图片保存到SD卡中,然后可进行下一步的识别。

1、我们通过打开模板匹配的历程来直接打开代码使用

点击文件——示例——openmv——feature detection——template_matching.py

下面就是例程。

2、如果运行出现这个窗口那就说明你没有保存模板图片。

 这时我们就需要创建一个模板图片。

3、首先要打开一个helloworld历程文件

点击file——examples——01-basics——helloworld.py

下面就是helloworld的例程。

 

4、在helloworld历程文件中进行匹配0~9这样的数字

  对这些数字进行一一截取(打开摄像头,图像中出现数字之和关闭摄像头,在图像中截取数字,右键选择将图像选择保存到PC),用它们来作为我们的模板图片(此时图片格式后缀为bmp格式)。

在右边的Frame Buffer框中进行截取,注意:不要点Zoom,因为Zoom展示的是放大后的效果,在识别时可能会导致失帧。

 例如:本人截取的一张图片(目前后缀还是bmp)

注意:模板图片的格式一定要是pgm的格式!!!

BMP轉PGM轉換器。在线自由 — Convertio这个网站可以直接进行图片格式转换。

点击选择文件(刚刚截图的模板图片)

 打开所选的图片,点击转换(注意是不是转换到PGM格式,如果不是请换到PGm格式)

 转换完成之后点击下载即可,此时图片后缀为PGM格式。

5、将转换的数字图片(PGM)进行保存,一定要保存到OpenMV4的SD卡中,名称自定义

 

 6、把template.pgm改为你命名的模板图片(PGM)的名称

 例如:我命名的为11

7、改完即可运行 

官方数字识别源代码,此代码为源代码,可在此基础上进行改动。

  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. template = image.Image("/11.pgm")
  25. clock = time.clock()
  26. # Run template matching
  27. while (True):
  28. clock.tick()
  29. img = sensor.snapshot()
  30. # find_template(template, threshold, [roi, step, search])
  31. # ROI: The region of interest tuple (x, y, w, h).
  32. # Step: The loop step used (y+=step, x+=step) use a bigger step to make it faster.
  33. # Search is either image.SEARCH_EX for exhaustive search or image.SEARCH_DS for diamond search
  34. #
  35. # Note1: ROI has to be smaller than the image and bigger than the template.
  36. # Note2: In diamond search, step and ROI are both ignored.
  37. r = img.find_template(template, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
  38. if r:
  39. img.draw_rectangle(r)
  40. print(clock.fps())

如需数字1——9的代码请联系我!(本文如有错误请指出,欢迎交流!)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/952912
推荐阅读
相关标签
  

闽ICP备14008679号