Convert a black or white only rgb array into a 1 or 0 array in Python -


i relatively new python , working image containing 'hits' particle detector totally black , white.

in order count number of hits (and later separate hits different particles) need group adjacent white pixels.

my code ensures image black or white , tries use scipy label function group white pixels (this should work groups none 0 values , made black pixels have 0's , white pixels hold 1. returns 0 labels , unsure why. think may fact not 1's , 0's still tuples of lists working with.

is there way create array of 1's or 0's based on whether pixel black or white?

def analyseimage(self, impath):       img = image.open(impath)     grey = img.convert('l')     bw = np.asarray(grey).copy()      #switches black , white label effective     bw[bw < 128] = 255    # white     bw[bw >= 128] = 0 # black      lbl, nlbls = label(bw)     labels = range(1, nlbls + 1)     coords = [np.column_stack(np.where(lbl == k)) k in labels]      imfile = image.fromarray(bw) 

after line bw[bw < 128] = 255 # white, elements < 128 have been set 255, means every element >= 128 (since 255 > 128). following line replaces each element 0, since again elements >= 128.

try instead:

def analyseimage(self, impath):       img = image.open(impath)     grey = img.convert('l')      #switches black (0) , white (255) label effective     bw = np.asarray([255 if x < 128 else 0 x in grey])      lbl, nlbls = label(bw)     labels = range(1, nlbls + 1)     coords = [np.column_stack(np.where(lbl == k)) k in labels]      imfile = image.fromarray(bw) 

alternatively, create copy of bw , use copy in comparison 128:

bw2 = bw.copy() bw[bw2 < 128] = 255 bw[bw2 >= 128] = 0 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -