简体   繁体   中英

how to detect boxes in a matrix

I am having many different 6By6 matrices. Each matrix contains different values. Those values represent how the layout will be divided. Each matrix should have consistent rectangles as follows (There should be continous rectangles, the colors represent the separate consistent rectangles): 由plt.imshow显示的矩阵

So my problem, is how to detect successfully those boxes (rectangles). I want as output a list of arrays. Each array should refer to ith index, the j th index and the value of that rectangles.

For example, I have as input this matrix [[35. 11. 11. 11. 11. 0.],[10. 10. 10. 10. 10. 0.],[ 10. 10. 10. 10. 10. 0.],[ 34. 34. 34. 34. 34. 0.],[ 34. 34. 34. 34. 34. 0.],[ 0. 0. 0. 0. 0. 0.]]

So I want as output [[0,0,35],[0,4,11],[1,4,10],[2,4,10],[3,4,34],[4,4,34],[0,0,0],[1,0,0],[5,5,0]]

My trial for detecting the rectangles is in this code:

#Detect the rectangles in the matrices
def detect_rectangle(T):
i = 0
j = 0
elem = T[0,0]
rectanglesList = []
n,m = T.shape
while (i < n) and (j<m):
    #print('i,j, elem',i,j,elem)      
    if (i == n-1 and j == m-1): # if we reached the end of the matrix
        rectanglesList.append([i,j,elem])
        break;
    if (j == m-1): #in case we reached the end of columns, we reeinitialize the columns
        if (i != n -1):
            i += 1
            elem = T[i,j]
        else:
            rectanglesList.append([i,j,T[i,j]])
            j = 0
            break;
    elif T[i,j] == T[i,j+1]: #in case the element in the next column is equal, continue and check further, store it as elem
        j +=1
        elem = T[i,j]
    elif T[i,j] != T[i,j+1] :
        rectanglesList.append([i,j,T[i,j]])
        j += 1
        elem = T[i,j]
    if (i == n-1): #in case we reached the end of rows
        if j != n -1 :
            j += 1
            elem = T[i,j]
        else:
            rectanglesList.append([i,j,elem])
            i = 0
            break
    else:
        if (T[i,j] == T[i+1,j]) and (elem == T[i,j]): #in case the element in the next row is equal
            i += 1
        elif (T[i,j] == T[i+1,j]) and (elem != T[i,j]): #in case the element in the next row is equal
            elem = T[i,j]
            i+= 1
        elif ((T[i,j] != T[i+1,j] and elem == T[i,j])): #in case it is not equal to neither the element in the next row nor the element in the next column
            rectanglesList.append([i,j,elem])
            #j +=1
            elem = T[i,j]
        elif T[i,j] != T[i+1,j] :
            i += 1
            elem = T[i,j]


return rectanglesList

So the code that I wrote is detecting the rectangles but in more separate way. I always have as output arrays that refer to a value that has just one row and one column as indexes.

I think this should work:

x=np.array([[35,11,11,11,11,0],[10,10,10,10,10,0],
            [10,10,10,10,10,0],[34,34,34,34,34,0],
            [34,34,34,34,34,0], [0,0,12,12,12,0]])
outputs=[]
for i, row in enumerate(x):
    last_ele=row[0]
    for j, val in enumerate(row[1:]):
        if val == last_ele:
            continue
        outputs.append([i,j, last_ele])
        last_ele=val
    outputs.append([i,len(row)-1, last_ele])
print(outputs)

# [[0, 0, 35], [0, 4, 11], [0, 5, 0], [1, 4, 10], 
#  [1, 5, 0], [2, 4, 10], [2, 5, 0], [3, 4, 34], 
#  [3, 5, 0], [4, 4, 34], [4, 5, 0], [5, 1, 0],
#  [5, 4, 12], [5, 5, 0]]

We simply iterate once over the rows and and check if the preceding element is the same as the current. If it is not, then we add the last seen element to our output list along with the row and column index.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM