logo

如何获取模板匹配物体中心坐标?

2018-04-25 11:10
H
Musk

OpenMV获取模板匹配物体中心的坐标,然后把坐标信息串口通信发送给单片机...

2018-04-25 20:15
I
网红小智智 星瞳科技

<https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.image.html?highlight=find#image.find_template>

返回用于匹配位置的一个边界框元组(x, y, w, h)

所以中心坐标就是(x+w/2, y+h/2)

2018-04-26 09:24
H
Musk

emmm...不好意思小小智,似懂非懂,模板匹配里本就涉及到了img.find_template,这么说,是只需要在后头print(x+w/2,y+h/2)吗?

2018-04-26 09:49
H
Musk

@kidswong999 原相关程序如下:

r = img.find_template(template, 0.70, step=4,search=SEARCH_EX) #, roi=(10, 0, 60, 60))

if r:

img.draw_rectangle(r)

2018-04-26 11:48
I
网红小智智 星瞳科技
r = img.find_template(template, 0.70, step=4,search=SEARCH_EX) #, roi=(10, 0, 60, 60))
if r:
    img.draw_rectangle(r)
    x = r[0]
    y = r[1]
    w = r[2]
    h = r[3]
2018-05-04 13:31
T
Mr_Zty

@kidswong999 @yuan 如何将匹配到物体的中心坐标打印出来

2018-05-04 15:34
X
Yuan
print(r[0]+r[2]/2, r[1]+r[3]/2)
2018-05-04 17:06
T
Mr_Zty

@yuan @kidswong999 ```

import time, sensor, image

from image import SEARCH_EX, SEARCH_DS#从imgae模块引入SEARCH_EX和SEARCH_DS。

#使用from import仅仅引入SEARCH_EX, SEARCH_DS两个需要的部分,而不把image模块全部引入。

sensor.reset()# 重置传感器

sensor.set_contrast(1)#设置传感器

sensor.set_gainceiling(16)#与SEARCH_EX的模板匹配的最大分辨率是QQVGA。

sensor.set_framesize(sensor.QQVGA)#你可以设置窗口来减少搜索图像。#sensor.set_windowing(((640-80)//2, (480-60)//2, 80, 60))

sensor.set_pixformat(sensor.GRAYSCALE)

#加载模板。

#模板应该是小的(如。为32 x32像素)灰度图像。

template = image.Image("/make.pgm")

#加载模板图片

clock = time.clock()

while (True):

clock.tick()

img = sensor.snapshot()

img.lens_corr(strength=1.8, zoom=1.0)

# find_template(template, threshold, [roi, step, search])

# Step: The loop step used (y+=step, x+=step) use a bigger step to make it faster.

# 步骤:使用的循环步骤(y+= Step, x+= Step)使用更大的步骤使其更快。

# Search is either image.SEARCH_EX for exhaustive search or image.SEARCH_DS for diamond search

# Note1: ROI必须小于图像,大于模板(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, 100, 80))

#threshold中的0.70是相似度阈值,roi是进行匹配的区域(左上顶点为(10,0),长80宽60的矩形),

# 注意roi的大小要比模板图片大,比frambuffer小。

#把匹配到的图像标记出来

if r:

img.draw_rectangle(r,color=0)

print(r[0]+r[2]/2, r[1]+r[3]/2)

2018-05-04 17:14
I
网红小智智 星瞳科技
if r:
    img.draw_rectangle(r,color=0)
    print(r[0]+r[2]/2, r[1]+r[3]/2)
2018-05-04 17:20
T
Mr_Zty

报错显示:IndentationError: unindent does not match any outer indentation level

import time, sensor, image
from image import SEARCH_EX, SEARCH_DS#从imgae模块引入SEARCH_EX和SEARCH_DS。
#使用from import仅仅引入SEARCH_EX, SEARCH_DS两个需要的部分,而不把image模块全部引入。

sensor.reset()# 重置传感器

sensor.set_contrast(1)#设置传感器
sensor.set_gainceiling(16)#与SEARCH_EX的模板匹配的最大分辨率是QQVGA。
sensor.set_framesize(sensor.QQVGA)#你可以设置窗口来减少搜索图像。#sensor.set_windowing(((640-80)//2, (480-60)//2, 80, 60))
sensor.set_pixformat(sensor.GRAYSCALE)

#加载模板。
#模板应该是小的(如。为32 x32像素)灰度图像。
template = image.Image("/make.pgm")
#加载模板图片

clock = time.clock()

while (True):
    clock.tick()
    img = sensor.snapshot()
    img.lens_corr(strength=1.8, zoom=1.0)

    # find_template(template, threshold, [roi, step, search])
    # Step: The loop step used (y+=step, x+=step) use a bigger step to make it faster.
    # 步骤:使用的循环步骤(y+= Step, x+= Step)使用更大的步骤使其更快。
    # Search is either image.SEARCH_EX for exhaustive search or image.SEARCH_DS for diamond search
    # Note1: ROI必须小于图像,大于模板(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, 100, 80))
    #threshold中的0.70是相似度阈值,roi是进行匹配的区域(左上顶点为(10,0),长80宽60的矩形),
    #                           注意roi的大小要比模板图片大,比frambuffer小。
    #把匹配到的图像标记出来
    if r:
         img.draw_rectangle(r,color=0)

     print(r[0]+r[2]/2, r[1]+r[3]/2)
I
网红小智智 星瞳科技

看缩进,看我的代码,print要和img对齐

T
Mr_Zty

@kidswong999 谢谢你

2020-01-15 10:38
L
陳沀沅臻23489

@kidswong999 你好image.find_template(template, threshold[, roi[, step=2[, search=image.SEARCH_EX]]])这个函数我没看明白。我对Python不熟悉。

(1)里边threshold[,roi...] 这里边逗号和[之间是表示省略一个元素吗?

(2)这个函数的括号里边对各个变量的理解和c语言一样吗?

从上边例程看img.find_template(template, 0.70, step=4,search=SEARCH_EX) 里边有template、0.7、step=4、search=SEARCH_EX这样四个量。这四个量和函数里边的threshold、roi等是怎么对应的,没看明白。

2020-01-15 18:18
I
网红小智智 星瞳科技

@mhad 新的问题单独发帖子。