0

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

I've made a masked 2D array based on the top answer in this post: https://stackoverflow.com/a/38194016/9700646

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
SaroyanDynamite
  • 157
  • 1
  • 8
  • Could you a sample input and the expected output? Also some code would be nice! – Dani Mesejo Oct 22 '18 at 16:10
  • @DanielMesejo I've added some code, I hope that helps. The 2D arrays are rather large for a pi processor (320x180). But basically it is a matrix of 1s and 0s (mostly 0s). I just want to work with the 1s, but I need to remember where they are positioned in the matrix – SaroyanDynamite Oct 22 '18 at 16:38
  • input is a .jpg image – SaroyanDynamite Oct 22 '18 at 16:47
  • I feel like this could help? But I don't understand what is going on here: https://stackoverflow.com/questions/28772573/python-parallelize-a-python-loop-for-2d-masked-array maybe somebody could explain what the code is doing? – SaroyanDynamite Oct 22 '18 at 17:32

0 Answers0