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:
which generated image:
in case, script didn't work perfectly, worked pretty well.
however, another image of same maze produced result:
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
Post a Comment