▁the ▁label ▁of ▁the ▁North ▁pixel ▁to ▁the ▁current ▁pixel ▁No ▁– ▁Check ▁next ▁condition ▁Do ▁the ▁pixel ' s ▁North ▁and ▁West ▁neighb ors ▁have ▁different ▁pixel ▁values ▁than ▁current ▁pixel ? ▁Yes ▁– ▁Create ▁a ▁new ▁label ▁id ▁and ▁assign ▁it ▁to ▁the ▁current ▁pixel ▁ ▁The ▁algorithm ▁continues ▁this ▁way , ▁and ▁creates ▁new ▁region ▁labels ▁whenever ▁necessary . ▁The ▁key ▁to ▁a ▁fast ▁algorithm , ▁however , ▁is ▁how ▁this ▁mer ging ▁is ▁done . ▁This ▁algorithm ▁uses ▁the ▁union - find ▁data ▁structure ▁which ▁provides ▁excellent ▁performance ▁for ▁keeping ▁track ▁of ▁equivalence ▁relationships . ▁Union - find ▁essentially ▁stores ▁labels ▁which ▁correspond ▁to ▁the ▁same ▁blob ▁in ▁a ▁dis joint - set ▁data ▁structure , ▁making ▁it ▁easy ▁to ▁remember ▁the ▁equivalence ▁of ▁two ▁labels ▁by ▁the ▁use ▁of ▁an ▁interface ▁method ▁E . g .: ▁find Set ( l ). ▁find Set ( l ) ▁returns ▁the ▁minimum ▁label ▁value ▁that ▁is ▁equivalent ▁to ▁the ▁function ▁argument ▁' l '. ▁ ▁Once ▁the ▁initial ▁label ing ▁and ▁equivalence ▁recording ▁is ▁completed , ▁the ▁second ▁pass ▁merely ▁rep laces ▁each ▁pixel ▁label ▁with ▁its ▁equivalent ▁dis joint - set ▁representative ▁element . ▁ ▁A ▁faster - sc anning ▁algorithm ▁for ▁connected - region ▁extra ction ▁is ▁presented ▁below . ▁ ▁On ▁the ▁first ▁pass : ▁Iter ate ▁through ▁each ▁element ▁of ▁the ▁data ▁by ▁column , ▁then ▁by ▁row ▁( R aster ▁Sc anning ) ▁If ▁the ▁element ▁is ▁not ▁the ▁background ▁ ▁Get ▁the ▁neighbor ing ▁elements