find_segment_index()

home > kero > Documentation

kero.ImageProcessing.photoBox.py

def find_segment_index(mat, start_entry):
  return row_index, column_index_list, out_mat, liber_mat
mat 2D matrix. List of list or numpy array.
start_entry [Integer, Integer]. This is the matrix index (zero-based) that is to point to a non-zero zero element. This function will search for a segment in the matrix that is non-zero in all entries.
return row_index Integer. The index of the row where the non-zero segment is located.
return column_index_list List of integer. The list of indices at row_index-th row that makes the non-zero segment.
return out_mat 2D matrix. This is the input mat, but with the segment removed.
return liber_mat 2D matrix. This is the matrix containing only the non-zero segment.

Example usage 1:

import kero.ImageProcessing.photoBox as kip
import numpy as np


gs_matrix = np.random.randint(2, size=(5, 10))
print(gs_matrix)

this_i, this_j = kip.find_first_non_zero_index(gs_matrix)
print("first non zero index: ", this_i, this_j)

start_entry = [this_i, this_j]
[row_index, column_index_list, out_mat, liber_mat] = kip.find_segment_index(gs_matrix, start_entry)
print("first segment index: ", row_index, " : ", column_index_list)

print("third output:\n", out_mat)
print("last output:")
for x in liber_mat:
	print(x)

As shown below, the first non-zero index and the first continuously non-zero segment are highlighted in blue. The red highlight shows the same matrix with this segment removed. The next blue highlight shows the extracted segment.

[[1 1 1 0 1 1 1 0 0 1]
 [1 1 0 0 1 0 0 1 0 1]
 [0 1 0 0 0 0 1 0 1 1]
 [1 0 1 1 1 1 0 0 0 0]
 [1 1 0 1 1 0 0 1 0 0]]
first non zero index:  0 0
first segment index (first and second output):  0  :  [0, 1, 2]
third output:
 [[0 0 0 0 1 1 1 0 0 1]
 [1 1 0 0 1 0 0 1 0 1]
 [0 1 0 0 0 0 1 0 1 1]
 [1 0 1 1 1 1 0 0 0 0]
 [1 1 0 1 1 0 0 1 0 0]]
last output:
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

kero version: 0.4 and above