text stringlengths 0 828 |
|---|
for n, factors in usable_factors.iteritems() |
for factor in factors |
if (factor <= max_vert_cells and |
n // factor <= max_hor_cells)] |
if not overlap_options: |
# We can't stack the image |
return convolution(bin_template, bin_image, tollerance=tollerance) |
best_overlap = min(overlap_options, |
key=lambda x: ((ih // x[0] + th) * (iw // x[1] + tw))) |
return overlapped_convolution(bin_template, bin_image, |
tollerance=tollerance, splits=best_overlap)" |
4257,"def overlapped_convolution(bin_template, bin_image, |
tollerance=0.5, splits=(4, 2)): |
"""""" |
As each of these images are hold only binary values, and RFFT2 works on |
float64 greyscale values, we can make the convolution more efficient by |
breaking the image up into :splits: sectons. Each one of these sections |
then has its greyscale value adjusted and then stacked. |
We then apply the convolution to this 'stack' of images, and adjust the |
resultant position matches. |
"""""" |
th, tw = bin_template.shape |
ih, iw = bin_image.shape |
hs, ws = splits |
h = ih // hs |
w = iw // ws |
count = numpy.count_nonzero(bin_template) |
assert count > 0 |
assert h >= th |
assert w >= tw |
yoffset = [(i * h, ((i + 1) * h) + (th - 1)) for i in range(hs)] |
xoffset = [(i * w, ((i + 1) * w) + (tw - 1)) for i in range(ws)] |
# image_stacks is Origin (x,y), array, z (height in stack) |
image_stacks = [((x1, y1), bin_image[y1:y2, x1:x2], float((count + 1) ** (num))) |
for num, (x1, x2, y1, y2) in |
enumerate((x1, x2, y1, y2) for (x1, x2) |
in xoffset for (y1, y2) in yoffset)] |
pad_h = max(i.shape[0] for _, i, _ in image_stacks) |
pad_w = max(i.shape[1] for _, i, _ in image_stacks) |
# rfft metrics must be an even size - why ... maths? |
pad_w += pad_w % 2 |
pad_h += pad_h % 2 |
overlapped_image = sum_2d_images(pad_bin_image_to_shape(i, (pad_h, pad_w)) |
* num for _, i, num in image_stacks) |
#print ""Overlap splits %r, Image Size (%d,%d), |
#Overlapped Size (%d,%d)"" % (splits,iw,ih,pad_w,pad_h) |
# Calculate the convolution of the FFT's of the overlapped image & template |
convolution_freqs = (rfft2(overlapped_image) * |
rfft2(bin_template[::-1, ::-1], |
overlapped_image.shape)) |
# Reverse the FFT to find the result overlapped image |
convolution_image = irfft2(convolution_freqs) |
# At this point, the maximum point in convolution_image should be the |
# bottom right (why?) of the area of greatest match |
results = set() |
for (x, y), _, num in image_stacks[::-1]: |
test = convolution_image / num |
filtered = ((test >= (count - tollerance)) & |
(test <= (count + tollerance))) |
match_points = numpy.transpose(numpy.nonzero(filtered)) # bottom right |
for (fy, fx) in match_points: |
if fx < (tw - 1) or fy < (th - 1): |
continue |
results.add((x + fx - (tw - 1), y + fy - (th - 1))) |
convolution_image %= num |
return list(results)" |
4258,"def get_partition_scores(image, min_w=1, min_h=1): |
""""""Return list of best to worst binary splits along the x and y axis. |
"""""" |
h, w = image.shape[:2] |
if w == 0 or h == 0: |
return [] |
area = h * w |
cnz = numpy.count_nonzero |
total = cnz(image) |
if total == 0 or area == total: |
return [] |
if h < min_h * 2: |
y_c = [] |
else: |
y_c = [(-abs((count / ((h - y) * w)) - ((total - count) / (y * w))), |
y, 0) |
for count, y in ((cnz(image[y:]), y) |
for y in range(min_h, image.shape[0] - min_h))] |
if w < min_w * 2: |
x_c = [] |
else: |
x_c = [(-abs((count / (h * (w - x))) - ((total - count) / (h * x))), |
x, 1) |
for count, x in ((cnz(image[:, x:]), x) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.