# How can I implement Guided Random algorithm walker matrices?

def make_graph_edges(image):
if(len(image.shape)==2):
n_x, n_y = image.shape
vertices = np.arange(n_x * n_y ).reshape((n_x, n_y))
edges_horizontal = np.vstack(( vertices[:, :-1].ravel(), vertices[:, 1:].ravel()))   # X *(Y-1)
edges_vertical   = np.vstack(( vertices[   :-1].ravel(), vertices[1:   ].ravel()))   #(X-1)* Y
edges = np.hstack((edges_horizontal, edges_vertical))
return edges

• weights Function:
def compute_weights(image,mask,alpha, beta, eps=1.e-6):
# Weight calculation is main difference in multispectral version
for ax in [1, 0] ], axis=0) ** 2            # gradient ^2
# 5-Connected
for ax in [1, 0] ], axis=0)**2
#----------------------------------------
# 1-Connected
#----------------------------------------
#------------------------------------------------------
intra_scale_factor  = -beta  / (10 * image.std())
intra_weights += eps
#------------------------------------------------------
inter_scale_factor  = -alpha / (10 * image.std())
inter_weights += eps
#------------------------------------------------------
return -intra_weights, inter_weights

• Building Matrices:
def build_matrices(image, mask, alpha=90, beta=130):
edges_2D = make_graph_edges(image)

# vox = np.concatenate((image[...,np.newaxis], mask[...,np.newaxis]), axis=2)
# edges_3D = make_graph_edges(vox)
#================
# Matrix Laplace
#================
# Build the sparse linear system
pixel_nb  = edges_2D.shape  # N = n_x * (n_y - 1) * +  (n_x - 1) * n_y
print('Edges Shape: ',edges_2D.shape,'intra-Weights shape: ',intra_weights.shape)
i_indices = edges_2D.ravel()   # Src - Dest
print('i',i_indices.shape)
j_indices = edges_2D[::-1].ravel() # Same list in reverse order ( Dest - Src)
print('j',j_indices.shape)
stacked_intra = np.hstack((intra_weights, intra_weights)) # weights (S-->D, D-->S) are same because graph is undirected
lap = sparse.coo_matrix((2*stacked_intra, (i_indices, j_indices)), shape=(pixel_nb, pixel_nb))
lap.setdiag(-2*np.ravel(lap.sum(axis=0)))
print('Lap',lap.shape)
Laplace = lap.tocsr()
#================
# Matrix Omega
#================
# Build the sparse linear system
stacked_inter = np.hstack((inter_weights, inter_weights)) # weights (S-->D, D-->S) are same because graph is undirected
Omeg = sparse.coo_matrix((2*stacked_inter, (i_indices, j_indices)), shape=(pixel_nb, pixel_nb))
print('Omeg',Omeg.shape)
Omega = Omeg.tocsr()
#================
# Matrix A
#================
# Build the sparse linear system
Mat_A = 0
return Laplace, Omega, Mat_A


    #================