模板与Tensorflow 2D卷积匹配

时间:2018-04-20 09:27:55

标签: python tensorflow convolution

我正在尝试使用Tensorflow 2D卷积实现模板匹配。输入是一个图像,模板较小。我翻转模板并将其插入卷积。

不知何故,我没有在非常简单的图像上获得匹配模板的精确坐标。

我的代码中出错了什么?

def templateMatch(image, _template):

    I_placeholder = tf.placeholder(tf.float32, shape=[None,None])
    I = tf.expand_dims(tf.expand_dims(I_placeholder, 0), 3)

    templateFlipped = tf.constant(np.fliplr(np.flipud(_template)), tf.float32)
    template = tf.reshape(templateFlipped, [_template.shape[0], _template.shape[1], 1, 1])

    conv = tf.nn.conv2d(I,template,strides=[1, 1, 1, 1], padding='SAME')
    templateMatchFlatten = tf.contrib.layers.flatten(conv)
    templateMatch = tf.reshape(templateMatchFlatten,[image.shape[0],image.shape[1]])


    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        return sess.run(templateMatch, feed_dict={I_placeholder: image})


#function call

lx = 400 #template coordinates
rx = 450
ly = 400
ry = 450

_image = mpimg.imread('./images/smiley.png')
image = _image[:,:,0].astype(np.float32) #select channel R of RGB image
template = image[lx:rx,ly:ry] #get template

templatedTensor = templateMatch(image, template)

position = unravel_index(templatedTensor.argmax(), templatedTensor.shape)

结果是here(蓝色十字是模板位置(tounge),红色十字是最佳模板匹配(英尺),还显示单个通道中的RGB图像和模板)

0 个答案:

没有答案
相关问题