How to perform cross-correlation

I really need help with cross-correlation. From pytorch docs i saw that conv2d layer can be used for cross-correlation, but when i tried to do it i keep on getting errors and cant figure out how to use conv2d layers for cross-correlation to find a template object in a search region. Could someone help me with the code.

Could you post the code snippet you’ve already written so that we can assist in debugging?
F.conv2d should work with a custom weight parameter as your kernel.


here is the code:
import torch
import torch.nn as nn
import torch.nn.functional as F
import cv2
import numpy as np
import matplotlib.pyplot as plt

IMG_SIZE = 100

image = cv2.imread(“C:\Users\Dutchfoundation\Desktop\AI\Torch\image_01.jpg”,cv2.IMREAD_GRAYSCALE)
template = cv2.imread(“C:\Users\Dutchfoundation\Desktop\AI\Torch\template_01.jpg”,cv2.IMREAD_GRAYSCALE)

image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))
template = cv2.resize(template, (IMG_SIZE, IMG_SIZE))


image_tensor = torch.Tensor(image).view(-1,100,100)
template_tensor = torch.Tensor(template).view(-1,100,100)
print(“Tensor Image:”,image_tensor.shape)
print(“Tensor Template:”,template_tensor.shape)
image_final = image_tensor.view(-1,1,100,100)
template_final = template_tensor.view(-1,1,100,100)
print(“Final Image:”,image_final.shape)
print(“Final Template:”,template_final.shape)

corr_map = F.conv2d(image_final, template_final)


The output shape of the corr_map is [1,1,1,1] which does’nt make sense

You are using an image and filter the same shape (100x100), which will create a single pixel output.
This is expected in a cross-correlation as well as convolution.
If you want a bigger output shape, use a smaller kernel or a larger image input.

1 Like

Thanks a lot for the help