Search code examples
pythonscipycomputer-vision

Why does correlate2d return noise?


I'm currently trying to create a bot for Minesweeper with computer vision. However using scipy.signal.correlate2d only yields noise. My test code is down below, why is the output just noise and not the heatmap I would expect?

from scipy import signal
import numpy as np
from cv2 import cv2
from PIL import Image

image = cv2.imread('MinesweeperTest.png',0)
template = cv2.imread('Mine2.png',0)

corr = signal.correlate2d(image,template,mode="same")

Image.fromarray(corr).save("correlation.png")

All the images involved can be found here:

MinesweeperTest.png: https://i.sstatic.net/Gxqtq.jpg

Mine2.png: https://i.sstatic.net/atV7T.jpg

Correlation.png: https://i.sstatic.net/aelY6.jpg


Solution

  • Preprocessing the images so that their mean value is 0 before invoking correlate2d should help get a more meaningful 2D cross-correlation:

    image = image - image.mean()
    template = template - template.mean()
    

    A reproducible example reads:

    from imageio.v3 import imread
    from matplotlib import pyplot as plt
    from scipy import signal
    import numpy as np
    
    image = imread('https://i.imgur.com/PpLLOW7.png', pilmode='L')
    template = imread('https://i.imgur.com/ApIIs1Z.png', pilmode='L')
    
    # fixed these
    image = image - image.mean()
    template = template - template.mean()
    
    corr = signal.correlate2d(image, template, mode="same")
    plt.imshow(corr, cmap='hot')