簡體   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