Collage template automatic generation

There is a collage that combines multiple images to create a single image. Most collage services do this by embedding images in collage templates. This time, I will try to generate this collage template stochastically.

This can be achieved by dividing one rectangle into $ n $ rectangles. This procedure is expressed probabilistically so that templates can be generated infinitely by changing the seed of random numbers. Only $ n $ is given.

Suppose that one area is divided into one in one step. The selected region follows the category distribution. A uniform distribution is acceptable, but the multinomial distribution is used assuming the case where you want to preferentially divide a large area (the same as the uniform distribution in this code). This is chosen because there is only one area at first. There are two next, so choose one and divide it. There are three next, so ... and so on.

Now, after deciding the area to be divided, decide how to divide it. Dividing corresponds to defining one straight line (hyperplane if it is 4 or more dimensions). In other words, $ w $ and $ b $ of the straight line $ w ^ T x --b $ should be defined.

This time, we will not divide it diagonally. Then, there are only two ways to divide it, the vertical direction and the horizontal direction. That is, only $ w = [1 \ 0] ^ T $ or $ w = [0 \ 1] ^ T $. This time, the method of division follows the Bernoulli distribution.

This way, all that remains is to decide b. This follows a uniform distribution. Let $ b = [b_1 \ b_2] $ and then $ b \ sim U (0, w) $ and $ b \ sim U (0, h) $. However, $ w $ is the width of the divided area and $ h $ is the vertical width of the divided area. Here, it may be possible to use a normal distribution so that the middle part is preferentially divided.

This is the end of the procedure. I experimented. The code is attached at the end. End with an example of $ n = 7 $.

collage.png

import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

def generate_template(n, width, height, random_state=1, max_random_state=10000, offset=0):
    L = [np.array([offset, offset, width-offset, height-offset])]
    random_state_lists = stats.randint.rvs(0, max_random_state, size=(n-1, 4), random_state=random_state)

    for random_state_list in random_state_lists:
        n_areas = len(L)
        if n_areas == 1:
            i = 0
        else:
            p = np.repeat(1 / (n_areas + i), n_areas)
            x = stats.multinomial.rvs(1, p, size=1, random_state=random_state_list[0])[0]
            i = x.argmax()

        y = stats.bernoulli.rvs(0.5, size=1, random_state=random_state_list[1])[0]
        if y == 0:
            b = stats.uniform.rvs(L[i][0], L[i][2] - L[i][0], size=1, random_state=random_state_list[2])[0]
            #b = stats.uniform.rvs(L[i][0], L[i][2] - L[i][0], size=1, random_state=random_state_list[2])[0]
        else:
            b = stats.uniform.rvs(L[i][1], L[i][3] - L[i][1], size=1, random_state=random_state_list[3])[0]
            #b = stats.uniform.rvs(L[i][1], L[i][3] - L[i][1], size=1, random_state=random_state_list[3])[0]
        if y == 0:
            area1 = np.array([L[i][0], L[i][1], b-offset/2, L[i][3]])
            area2 = np.array([b+offset/2, L[i][1], L[i][2], L[i][3]])
        else:
            area1 = np.array([L[i][0], L[i][1], L[i][2], b-offset/2])
            area2 = np.array([L[i][0], b+offset/2, L[i][2], L[i][3]])
        L.pop(i)
        L.append(area1)
        L.append(area2)
    return L

L = generate_template(7, 1, 1, random_state=3, offset=0)
colors=["b", "g", "r", "c", "m", "y", "k"]
fig = plt.figure()
ax = fig.add_subplot(111)
for l_i, c_i in zip(L, colors):
    rect = plt.Rectangle(xy=[l_i[0], l_i[1]], width=l_i[2] - l_i[0], height=l_i[3] - l_i[1], facecolor=c_i, fill=True)
    ax.add_patch(rect)
plt.tight_layout()
plt.show()

Recommended Posts

Collage template automatic generation
Automatic mosaic generation
07. Sentence generation by template
Automatic quiz generation with COTOHA
PyCharm test code automatic generation
Masashi Sada Automatic generation of senryu
template
Automatic document generation from docstring with sphinx
Automatic PowerPoint generation with python-pptx (personal memo)