<jupyter_start><jupyter_text>Test three image alpha blend<jupyter_code>import math
import numpy as np

# Import DiskFind utilities for PredatorEye.
import sys
sys.path.append('/content/drive/My Drive/PredatorEye/shared_code/')
import DiskFind as df

df.set_global_random_seed(20220113)
# See SO question "Numpy - multiple 3d array with a 2d array"
#     Based on this SO answer: https://stackoverflow.com/a/46930388/1991373
#     See also about np.tensordot: https://stackoverflow.com/a/46930585/1991373

print('df.relative_disk_radius() =', df.relative_disk_radius())

image_depth = 3
image_size = 128
disk_radius = image_size * df.relative_disk_radius()
disk_radius_int = round(disk_radius)
disk_diameter_int = disk_radius_int * 2
image_shape = (image_size, image_size, image_depth)
disk_shape = (disk_diameter_int, disk_diameter_int, image_depth)
disk_matte_shape = (disk_diameter_int, disk_diameter_int, 1)

print('image_size =', image_size)
print('disk_radius =', disk_radius)
print('disk_radius_int =', disk_radius_int)

# matte = np.ones(disk_matte_shape, dtype=np.float32)
matte = np.zeros(disk_matte_shape, dtype=np.float32)
print('matte.shape =', matte.shape)


# Draw soft-eged disk with given centerpoint and color.
# (Copied from Find_3_disks)
def draw_disk(center, color, image):
    cx = int(center[0])
    cy = int(center[1])
    dr = int(disk_radius)

    # TODO temp for debugging
    color = np.array(color)
    bg_color = np.zeros(color.shape)
    image[:,:] = 0.81

    for x in range(cx - dr, cx + dr + 1):
        for y in range(cy - dr, cy + dr + 1):
            d = math.sqrt(math.pow(x - cx, 2) + math.pow(y - cy, 2))
            if (d <= dr):
                blend = df.spot_utility((x, y), center, dr * 0.85, dr)
                image[x, y, :] = df.interpolate(blend, bg_color, color) 
    print_matte(image)
    return image

def print_matte(image):
    for y in range(disk_diameter_int):
        line = ''
        for x in range(disk_diameter_int):
            s = '   ' +str(int(image[x, y][0] * 99))
            line += s[-2:] + ' '
        print(line)

# matte = draw_disk((disk_radius_int, disk_radius_int), [1.0], matte)
# df.draw_image(np.concatenate((matte, matte, matte), axis=2))

# Moved to DiskFind.py

# # Generate a soft disk matte, an array with shape (diameter, diameter, 1).
# # Uses the TexSyn Spot parameters: inner_radius and outer_radius.
# # TODO can outer_radius always be (diameter / 2.0)?
# def make_disk_matte(diameter, inner_radius, outer_radius):
#     # Coerce diameter to be int, to be used as an index for pixels.
#     diameter = round(diameter)
#     # Make the array to return.
#     image = np.zeros((diameter, diameter, 1), dtype=np.float32)
#     # Centerpoint of disk (as float, independent of pixel boundaries).
#     cp = (diameter / 2.0, diameter / 2.0)
#     # Loop over all pixel coordiantes (xi, yi) with (xf, yf) as center of pixel.
#     for xi in range(diameter):
#         xf = xi + 0.5
#         for yi in range(diameter):
#             yf = yi + 0.5
#             image[xi, yi, :] = df.spot_utility((xf, yf), cp,
#                                                 inner_radius, outer_radius)
#             # # TODO temp for debugging
#             # d = math.sqrt(math.pow(xf - cp[0], 2) + math.pow(yf - cp[1], 2))
#             # if (d > outer_radius):
#             #     image[xi, yi, :] = 0.81
#     return image

matte = df.make_disk_matte(disk_diameter_int,
                           disk_radius * 0.85,
                           disk_radius)
df.draw_image(np.concatenate((matte, matte, matte), axis=2))

matte = df.make_disk_matte(25, 0, 12.5)
df.draw_image(np.concatenate((matte, matte, matte), axis=2))

matte = df.make_disk_matte(24, 0, 12)
df.draw_image(np.concatenate((matte, matte, matte), axis=2))
matte = np.zeros((10, 10, 1), dtype=np.float32)
print('matte.shape =', matte.shape)
print('matte[0,0,:] =', matte[0,0,:])
df.draw_image(np.concatenate((matte, matte, matte), axis=2))

matte[:,:] = 1
print('matte.shape =', matte.shape)
print('matte[0,0,:] =', matte[0,0,:])
df.draw_image(np.concatenate((matte, matte, matte), axis=2))

matte[:,:] = 0.5
print('matte.shape =', matte.shape)
print('matte[0,0,:] =', matte[0,0,:])
df.draw_image(np.concatenate((matte, matte, matte), axis=2))

matte = np.random.rand(10, 10, 1).astype(np.float32)
print('matte.shape =', matte.shape)
print('matte[0,0,:] =', matte[0,0,:])
df.draw_image(np.concatenate((matte, matte, matte), axis=2))
rgb = np.zeros((10, 10, 3), dtype=np.float32)
print('rgb.shape =', rgb.shape)
print('rgb[0,0,:] =', rgb[0,0,:])
df.draw_image(rgb)

rgb[:,:,:] = [1.0, 0.5, 0.0]
print('rgb.shape =', rgb.shape)
print('rgb[0,0,:] =', rgb[0,0,:])
df.draw_image(rgb)

rgb = np.random.rand(10, 10, 3).astype(np.float32)
print('rgb.shape =', rgb.shape)
print('rgb[0,0,:] =', rgb[0,0,:])
df.draw_image(rgb)<jupyter_output>rgb.shape = (10, 10, 3)
rgb[0,0,:] = [0. 0. 0.]<jupyter_text>Checkerboard test pattern<jupyter_code># Moved to DiskFind.py

# # Generate image (rgb pixel tensor) with a black and white checkerboard pattern.
# def make_checkerboard(size, half_wavelength, c0=[0,0,0], c1=[1,1,1]):
#     # image = np.zeros((size, size, 3), dtype=np.float32)
#     image = np.empty((size, size, 3), dtype=np.float32)
#     image[:,:,:] = c0
#     for x in range(size):
#         for y in range(size):
#             if 0 == ((int(x / half_wavelength) + int(y / half_wavelength)) % 2):
#                 # image[x, y, :] = [1, 1, 1]
#                 image[x, y, :] = c1
#     return image

df.draw_image(df.make_checkerboard(100, 5))
df.draw_image(df.make_checkerboard(100, 5, [1, 1, 0], [0, 0, 1]))<jupyter_output><empty_output><jupyter_text>Low pass filter<jupyter_code># Moved to DiskFind.py

# def lpf_3x3_box(image):
#     s = image.shape
#     filtered = np.empty(s, dtype=np.float32)
#     for x in range(s[0]):
#         for y in range(s[1]):
#             sum = [0,0,0]
#             count = 0
#             for i in range(-1, 2):
#                 for j in range(-1, 2):
#                     p = x + i
#                     q = y + j
#                     if (p >= 0) and (q >= 0) and (p < s[0]) and (q < s[1]):
#                         count += 1
#                         sum += image[p, q, :]
#             filtered[x, y, :] = sum / count
#     return filtered

# def lpf_3x3(image):
#     s = image.shape
#     filtered = np.empty(s, dtype=np.float32)
#     kernel = np.array([[1, 2, 1],
#                        [2, 4, 2],
#                        [1, 2, 1]])
#     for x in range(s[0]):
#         for y in range(s[1]):
#             sum = [0,0,0]
#             count = 0
#             for i in range(-1, 2):
#                 for j in range(-1, 2):
#                     p = x + i
#                     q = y + j
#                     if (p >= 0) and (q >= 0) and (p < s[0]) and (q < s[1]):
#                         # count += 1
#                         # sum += image[p, q, :]
#                         w = kernel[i - 1, j - 1]
#                         count += w
#                         sum += image[p, q, :] * w
#             filtered[x, y, :] = sum / count
#     return filtered


bg = np.random.rand(disk_diameter_int, disk_diameter_int, 3).astype(np.float32)
print('bg.shape =', bg.shape)
print('type(bg) =', type(bg))
df.draw_image(bg)

# fbg = np.random.rand(disk_diameter_int, disk_diameter_int, 3).astype(np.float32)

fbg = df.lpf_3x3_box(bg)
print('fbg.shape =', fbg.shape)
df.draw_image(fbg)

fbg = df.lpf_3x3(bg)
print('fbg.shape =', fbg.shape)
df.draw_image(fbg)<jupyter_output>bg.shape = (26, 26, 3)
type(bg) = <class 'numpy.ndarray'><jupyter_text>Soft matte<jupyter_code># Moved to DiskFind.py

# # Matte (composite) one "foreground" RGB image over another "background" image,
# # according to per-pixel scalar alpha weights in "matte".
# def soft_matte(foreground, background, matte):
#     # Multiply given (RGB) image by weights in given (single channel) matte.
#     def soft_matte_wighted_element(image, matte):
#         w = image.shape[0]
#         h = image.shape[1]
#         reshape_image = image.reshape(w * h, 3).T
#         reshape_matte = matte.reshape(w * h, 1).T
#         alpha_reshape = reshape_matte * reshape_image
#         alpha = alpha_reshape.T.reshape([w, h, 3])
#         return alpha

#     # Intended as more specific error messages for mismatch of args.
#     def soft_matte_check_args(foreground, background, matte):
#         assert foreground.shape[2] == 3, "'foreground' should be 3 floats deep"
#         assert background.shape[2] == 3, "'background' should be 3 floats deep"
#         assert matte.shape[2] == 1, "'matte' should be 1 float deep"
#         a = foreground.shape[0:1] == matte.shape[0:1]
#         assert a, "width or height of 'foreground' and 'matte' do not match"
#         a = background.shape[0:1] == matte.shape[0:1]
#         assert a, "width or height of 'background' and 'matte' do not match"

#     # Check args and sum the two image arguments, weighted by matte and inverse.
#     soft_matte_check_args(foreground, background, matte)
#     return (soft_matte_wighted_element(foreground, matte) +
#             soft_matte_wighted_element(background, 1 - matte))



bg = np.random.rand(disk_diameter_int, disk_diameter_int, 3).astype(np.float32)
# bg = lpf_3x3_box(bg)
bg = df.lpf_3x3(bg)
print('bg.shape =', bg.shape)
df.draw_image(bg)

fg = df.make_checkerboard(disk_diameter_int, 2, [1, 1, 0], [0, 0, 1])
print('fg.shape =', fg.shape)
df.draw_image(fg)

# matte = make_disk_matte(disk_diameter_int, disk_diameter_int / 4, disk_diameter_int / 2)
matte = df.make_disk_matte(disk_diameter_int,
                           disk_diameter_int * 0.5 * 0.8,
                           disk_diameter_int * 0.5)
print('matte.shape =', matte.shape)
df.draw_image(np.concatenate((matte, matte, matte), axis=2))

# df.draw_image(soft_matte(fg, bg, matte))
df.draw_image(df.soft_matte(bg, fg, matte))<jupyter_output>bg.shape = (26, 26, 3)
{'fraction_non_alphanumeric': 0.15748821346173136, 'fraction_numerical': 0.03380479486407864, 'mean_word_length': 2.763684409211023, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '601227', 'license': 'MIT', 'n_tokens_mistral': 4009, 'n_tokens_neox': 3739, 'n_words': 862, 'path': 'graveyard/ThreeImageSoftMatte.ipynb', 'repo_name': 'cwreynolds/PredatorEye', 'size': '9969'}