I need to scan an image and see if the values in the 3x3 window of each pixel match with a certain pattern. I use the following code
import numpy as np
import cv2
im = cv2.imread("image.png")
h, w = im.shape[:2]
for i in range(1, h-1):
for j in range(1, w-1):
p2 = im[i-1, j]
p3 = im[i-1, j+1]
p4 = im[i, j+1]
p5 = im[i+1, j+1]
p6 = im[i+1, j]
p7 = im[i+1, j-1]
p8 = im[i, j-1]
p9 = im[i-1, j-1]
# code for checking the pattern looks something like this:
if (p2 + p3 + p9) == 1 and p4 == 0 and p5 == 1:
val = True
But the code above takes forever to finish. I'm new to Python and numpy, how to effectively scan 2d numpy array?
Actually I'm trying to port this thinning code from C++ to Python.