python - Improve accuracy of scaling down image -


i'm using python 2.7 , pil (pillow)

i have script takes rough images of maze , makes cleaner, smaller image. sample input , output:

sample

which generated image:

this one

in case, script didn't work perfectly, worked pretty well.

however, another image of same maze produced result:

less good

that's less good.

i'm generating images displayed side-by-side looking @ average values each square on 16x16 grid, deciding if square represents black or white pixels. however, since perspective transformation isn't perfect, squares aren't lined up.

are there algorithms accuracy? way @ squares of grid aren't square chunks?

a piece of code:

#this image transformed , thresholded, first half of side-by-side images thresh = image.open('thresholded_image.jpg') pixsize = thresh.size[0]/16 segments = [] in range(16):     j in range(16):         box = (j*pixsize,i*pixsize,(j+1)*pixsize,(i+1)*pixsize)         segments.append(thresh.crop(box)) def blackwhite(image):     '''return `true` if image white, else `false`'''     l=image.convert('l').load()     w,h=image.size     lums=sum([[l[x,y] x in range(w)] y in range(h)],[])     return sum(lums)/float(len(lums))>127 whites = [] y in range(16):     x in range(16):         seg = segments[16*y+x]         if blackwhite(seg):             whites.append((x,y))  maze = image.new('l',(16,16)) l=maze.load() w in whites:     x,y=w     l[x,y] = 255 

(as requested, reposting comment answer.)

consider weighting pixels near center of square you're evaluating more heavily, , towards edges less - combat small misalignment. try locate corners , adjust image corners form perfect square combat skew.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -