简体   繁体   English

如何检测矩阵中的框

[英]how to detect boxes in a matrix

I am having many different 6By6 matrices. 我有许多不同的6By6矩阵。 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. 每个数组应引用第ith个索引,第j个索引以及该矩形的值。

For example, I have as input this matrix [[35. 例如,我将此矩阵[[35。 11. 11. 11. 11. 0.],[10. 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.]] 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]] 所以我想作为输出[[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. 如果不是,则将最后看到的元素以及行和列索引添加到输出列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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