繁体   English   中英

如何检测矩阵中的框

[英]how to detect boxes in a matrix

我有许多不同的6By6矩阵。 每个矩阵包含不同的值。 这些值表示布局将如何划分。 每个矩阵应具有如下所示的一致矩形(应该有连续矩形,颜色代表单独的一致矩形): 由plt.imshow显示的矩阵

所以我的问题是如何成功检测到那些盒子(矩形)。 我想要输出数组列表。 每个数组应引用第ith个索引,第j个索引以及该矩形的值。

例如,我将此矩阵[[35。 11. 11. 11. 11. 0。],[10。 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.]]

所以我想作为输出[[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]

我检测矩形的尝试在以下代码中:

#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

因此,我编写的代码正在检测矩形,但以更独立的方式。 我总是有一个输出数组,它引用一个只有一行和一列作为索引的值。

我认为这应该工作:

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]]

我们只需对行进行一次迭代,然后检查前面的元素是否与当前元素相同。 如果不是,则将最后看到的元素以及行和列索引添加到输出列表中。

暂无
暂无

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

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