def matches_exist(template, image, tolerance=1): # just taken from convolution def expected = numpy.count_nonzero(template) ih, iw = image.shape th, tw = template.shape # Pad image to even dimensions if ih % 2 or iw % 2: if ih % 2: ih += 1 if iw % 2: iw += 1 bin_image = pad_bin_image_to_shape(image, (ih, iw)) if expected == 0: return [] # Calculate the convolution of the FFT's of the image & template convolution_freqs = rfft2(image) * rfft2(template[::-1, ::-1], image.shape) convolution_image = irfft2(convolution_freqs) found_bitmap = convolution_image > (expected - tolerance) if True in found_bitmap: return True else: return False
def test_same_size(self): image = np.array([[0, 0, 0, 0, 0], [0, 0, 0, 1, 0], [0, 0, 1, 0, 0], [0, 1, 0, 0, 0], [0, 0, 0, 0, 0]]) expected = image actual = pad_bin_image_to_shape(image, image.shape) assert_array_equal(expected, actual)
def test_padding(self): image = np.array([[0, 0, 0, 0, 0], [0, 0, 0, 1, 0], [0, 0, 1, 0, 0], [0, 1, 0, 0, 0], [0, 0, 0, 0, 0]]) expected = np.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]) actual = pad_bin_image_to_shape(image, (6, 8)) assert_array_equal(expected, actual)