def _ncut_relabel(rag, thresh, num_cuts):
 (d, w) = _ncut.DW_matrices(rag)
 m = w.shape[0]
 if (m > 2):
  d2 = d.copy()
  d2.data = np.reciprocal(np.sqrt(d2.data, out=d2.data), out=d2.data)
  (vals, vectors) = linalg.eigsh(((d2 * (d - w)) * d2), which='SM', k=min(100, (m - 2)))
  (vals, vectors) = (np.real(vals), np.real(vectors))
  index2 = _ncut_cy.argmin2(vals)
  ev = vectors[:, index2]
  (cut_mask, mcut) = get_min_ncut(ev, d, w, num_cuts)
  if (mcut < thresh):
   (sub1, sub2) = partition_by_cut(cut_mask, rag)
   _ncut_relabel(sub1, thresh, num_cuts)
   _ncut_relabel(sub2, thresh, num_cuts)
   return
 _label_all(rag, 'ncut label')

def _ncut_relabel ( rag , thresh , num_cuts ) : ( d , w ) = _ncut . DW_matrices ( rag ) m = w . shape [ 0 ] if ( m > 2 ) : d2 = d . copy ( ) d2 . data = np . reciprocal ( np . sqrt ( d2 . data , out = d2 . data ) , out = d2 . data ) ( vals , vectors ) = linalg . eigsh ( ( ( d2 * ( d - w ) ) * d2 ) , which = 'SM' , k = min ( 100 , ( m - 2 ) ) ) ( vals , vectors ) = ( np . real ( vals ) , np . real ( vectors ) ) index2 = _ncut_cy . argmin2 ( vals ) ev = vectors [ : , index2 ] ( cut_mask , mcut ) = get_min_ncut ( ev , d , w , num_cuts ) if ( mcut < thresh ) : ( sub1 , sub2 ) = partition_by_cut ( cut_mask , rag ) _ncut_relabel ( sub1 , thresh , num_cuts ) _ncut_relabel ( sub2 , thresh , num_cuts ) return _label_all ( rag , 'ncut label' )
perform normalized graph cut on the region adjacency graph .