Recursively change black to white in image with numpy -


what's best way remove large shadowed regions greyscaled images. i'm struggling write method takes 2d numpy array , entry (x,y) in a, , "crawls" through array changing (x',y') entry "connected" (x,y) 0 255. mean connected there's path of 0 valued entries (x,y) (x',y'). here's picture of mean.

enter image description here

the black region @ bottom should set grayscale 255. i'm positive algorithm should recursive, there fast way in numpy, or using pil?

edit:

ok advice, here's i've been able come with;

def creep(data, x, y):     data[x, y]=255      (i,j) in [(1,0),(-1,0),(0,1),(0,-1)]:         x, y = x + i, y + j          try:             if data[x, y]==0:                 return creep(data, x, y)         except:             pass      return data   def crop_big_region(data):     """ looks black regions in image , makes them white """     n, m = data.shape     r = int(0.012*min(n,m))      num_samples = int(0.0001*n*m)     _ in xrange(0,num_samples):         x, y = numpy.random.randint(r,n - r), numpy.random.randint(r,m -r)         if numpy.all(data[x-r:x+r, y-r:y+r] == 0):             data[x,y] = 255             data = creep(data, x, y)     return data  

it seems sort of work, except returns lines, instead of filling out entire region.

enter image description here

think i'm tired figure out recursive step here properly.

as @boaz pointed out more image processing question python question. can achieve desired result using so-called adaptive thresholding. scikits-image has nice implementation available, complete tutorial here:

http://scikit-image.org/docs/dev/auto_examples/plot_threshold_adaptive.html

you need tune bit, should work.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -