I want to use find a path in an image. The issue is that my "player" requires more than 1 pixel of space to fit through. So I figured out, that I can make obstacles bigger. If my player requires about 15 pixels to move through, I can replace an wall pixel with rectangle of size 30 (15 on left side, 15 on right side). But the issue is that following code take about 8s to run.
img = cv2.imread("playground/radar2.png", cv2.IMREAD_GRAYSCALE)
output = np.ones(img.shape, dtype=np.uint8) * 255
for y in range(img.shape[0]):
for x in range(img.shape[1]):
if img[y, x] == 255:
cv2.rectangle(output, (x-15, y-15), (x+15, y+15), 255, -1)
I created second version using numpy, but it still takes about 100 ms.
img = cv2.imread("playground/radar2.png", cv2.IMREAD_GRAYSCALE)
output = np.ones(img.shape, dtype=np.uint8) * 255
positions = np.where(img == 255)
for x, y in zip(positions[1], positions[0]):
size = 15
output[y-size:y+size, x-size:x+size] = 0
Is there a function in np or cv2 which I'm missing?