openmv数字识别之模板匹配和训练集

官网资料:https://book.openmv.cc/

模板匹配

视频教学:
https://singtown.com/learn/49598/
模板一定是pgm格式的
代码:
import time, sensor, image
from image import SEARCH_EX, SEARCH_DS
#从imgae模块引入SEARCH_EX和SEARCH_DS。使用from import仅仅引入SEARCH_EX,
sensor.reset()
sensor.set_contrast(1)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.QQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)
template = image.Image(“/template.pgm”)

#templates = [“/0.pgm”, “/1.pgm”, “/2.pgm”, “/6.pgm”] #保存多个模板

#加载模板图片
clock = time.clock()
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.
r = img.find_template(template, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
#find_template(template, threshold, [roi, step, search]),threshold中
#的0.7是相似度阈值,roi是进行匹配的区域(左上顶点为(10,0),长80宽60的矩形),
#注意roi的大小要比模板图片大,比frambuffer小。
#把匹配到的图像标记出来
if r:
img.draw_rectangle®
print(clock.fps())

缺点:
识别时需要和模板拍照时一模一样的情况,也就是拍的时候是什么样的,识别的时候就得是什么样,最好就是先固定再拍模板
优点:
可以把数字框出来,然后可以知道数字大概在哪个位置,从而进行更好的判断(此判断按个人需要)

训练集

视频教学:
https://www.bilibili.com/video/BV1G8411w72w?p=34&vd_source=14e75d1a1de1aedbe132b83a9f7b7779
训练模型:https://github.com/SingTown/openmv_tensorflow_training_scripts/tree/main/mnist
代码:
import sensor, image, time, os, tf

sensor.reset() # Reset and initialize the sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240)
sensor.set_windowing((240, 240)) # Set 240x240 window.
sensor.skip_frames(time=2000) # Let the camera adjust.

clock = time.clock()
while(True):
clock.tick()
img = sensor.snapshot().binary([(0,64)])
for obj in tf.classify(“trained.tflite”, img, min_scale=1.0, scale_mul=0.5, x_overlap=0.0, y_overlap=0.0):
output = obj.output()
number = output.index(max(output))
print(number)
print(clock.fps(), “fps”)

缺点:
不可以把数字框出来,不知道数字大概在哪个位置,从而不能进行更好的判断(此判断按个人需要),还会受光线的影响
优点:
可以多方位的识别数字,并不需要有固定的高度和距离

你可能感兴趣的:(开发语言,python,图像处理)