How to best loop through masked 2d array

The big goal here is image processing: Detecting edges and color in a given jpg image.

so I have the following, which should mask all 0's in the 2D array as masked:

``````color_matrix[~np.array(0)]
``````

I'm just not sure of how to loop through this. Does somebody have an idea?

Eventually I need to develop a third matrix by looping through two 2D masked arrays.

Here's some code to give you an idea of what I have so far:

This first block is how I make an 'edge_matrix.' I also mask it at the end. A 'color_matrix' is made in a similar process.

``````def edge_matrix(canny_pic):
#edge_matrix = [[0 for x in range(RESOLUTION_WIDTH)] for y in range(RESOLUTION_HEIGHT)]
edge_matrix = np.zeros((RESOLUTION_HEIGHT,RESOLUTION_WIDTH,1),dtype  = "uint8")
# get Canny pic
img = canny_pic #the first element of canny_pic is the picture, the others are the upper and lowers

# make matrix of edges
for i in range(RESOLUTION_HEIGHT):
for j in range(RESOLUTION_WIDTH):
#print(i,j)
color = img[i,j]
edge_matrix[i,j]=(color[0] or color[1] or color[2] ) and 1

return edge_matrix
``````

Then I want to combine these matrices by going through the edge matrix and saying: whenever there is an edge (a 1) look at the 10 spaces around that pixel in the color_matrix and see if color was detected. if so, mark it as a 1 in the combined_matrix.

``````def reduce_problem(color_mat, edge_mat):
#this function will help reduce the number of location the object may be in by combining the colors and edges
#create a matrix to store the possible locations of the correct object.

#combined_matrix = [[0 for x in range(RESOLUTION_WIDTH)] for y in range(RESOLUTION_HEIGHT)] #same size as image. 1 = a spot with both edge and color; 0 = a spot without both
combined_matrix = np.zeros((RESOLUTION_HEIGHT,RESOLUTION_WIDTH, 1), dtype = "float32")

for i in range(10,RESOLUTION_HEIGHT-11): #loop through color matrix
for j in range(10,RESOLUTION_WIDTH-11):
if  edge_mat[i,j] == 1:
#if this edge matrix location has an edge(1), go to the color matrix and check the 10 spaces surrounding it in each direction for any color(1) value (in respect to the corresponding space)

for k in range(i-10, i+10):
#if k >= 0 and k < RESOLUTION_HEIGHT:
for m in range(j-10, j+10):
#if m >= 0 and m < RESOLUTION_WIDTH:
if color_mat[k,m] ==1:
#print(k, m)
combined_matrix[i,j] += 1/441
``````

This works for detecting color and edges. But it is very slow, and way too slow for real-time detection of objects. I was hoping that using masked arrays to iterate would help speed up the process.