如何检测矩阵中的框
how to detect boxes in a matrix
我有很多不同的 6By6 矩阵。每个矩阵包含不同的值。这些值表示布局将如何划分。
每个矩阵应该有如下一致的矩形(应该有连续的矩形,颜色代表单独的一致矩形):
所以我的问题是如何成功检测到那些框(矩形)。
我想要一个数组列表作为输出。每个数组应引用第 i 个索引、第 j 个索引和该矩形的值。
例如,我将这个矩阵 [[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.]]
所以我想要输出 [[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]]
我们只是在行上迭代一次,然后检查前面的元素是否与当前元素相同。如果不是,那么我们将最后看到的元素连同行和列索引一起添加到我们的输出列表中。
我有很多不同的 6By6 矩阵。每个矩阵包含不同的值。这些值表示布局将如何划分。
每个矩阵应该有如下一致的矩形(应该有连续的矩形,颜色代表单独的一致矩形):
所以我的问题是如何成功检测到那些框(矩形)。 我想要一个数组列表作为输出。每个数组应引用第 i 个索引、第 j 个索引和该矩形的值。
例如,我将这个矩阵 [[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.]]
所以我想要输出 [[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]]
我们只是在行上迭代一次,然后检查前面的元素是否与当前元素相同。如果不是,那么我们将最后看到的元素连同行和列索引一起添加到我们的输出列表中。