I want to get the index where the target is located in background image using numpy. the background color of each image is actually varible so except for the color of the sqaure, other colors (in this case, black painted) including the inside of the square will vary. i have no idea how to approach this at all as im not familiar with numpy.
import numpy as np
#changing to grayscale to have 2d array
output = cv2.imread('backgroundimage.png', cv2.IMREAD_GRAYSCALE)
output1 = cv2.imread('target.png', cv2.IMREAD_GRAYSCALE)
i tried to change the images to 2d array because i thought it might be easier to approach.
a = np.where(output==output1)
apparently this doesnt work for 2d or 3d. my desired output will be something like this
desired output = (108, 23) (x and y coordination of where its found)
so how would i able to do what i want?
CodePudding user response:
You have to use a sliding window approach, and make sure that all pixels are equal for each window you compare with the target. You can do that using sliding_window_view
and all
. You can mask out any values inside the target that you do not want to match by setting them to True:
import cv2
import numpy as np
output = cv2.imread('backgroundimage.png', cv2.IMREAD_GRAYSCALE)
output1 = cv2.imread('target.png', cv2.IMREAD_GRAYSCALE)
# Apply sliding window view
windows = np.lib.stride_tricks.sliding_window_view(output, output1.shape)
# Only check the 1 pixel border by making a mask
mask = np.full_like(target, False)
mask[1:-1, 1:-1] = True
# Apply mask and check match everywhere
masked = (windows == output1) | mask
matches = masked.all(axis=(2, 3))
locations = np.where(matches)
locations:
(array([24], dtype=int64), array([108], dtype=int64))