00001
00002
00003
00004
00005
00006
00007
00008
00009 from __future__ import division
00010
00011 from random import random
00012 from random import seed
00013
00014 from math import sqrt
00015 from math import sqrt
00016 from math import ceil
00017
00018 from random_distributions import make_distribution_curve
00019 from fast_grid import Grid2D
00020 from gradient import mix_color
00021
00022 from image import greyscale_grid_to_rgb_grid
00023 from image import rgb_image_to_image_grid_channels
00024 from image import channels_to_rgb_grid
00025 from image import grid_to_rgb_image
00026 from image import normalize_grey
00027 from image import mix_grey
00028 from image import tile
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 def random_grid(distribution, dims, period=1):
00041 grid = Grid2D(dims)
00042
00043 width, height = dims
00044
00045 for i in range(0, width, period):
00046 for j in range(0, height, period):
00047 r = random()
00048 value = distribution(r)
00049
00050 for index in grid.window_index_iter((i, j), (i + period, j+period)):
00051 grid[index] = value
00052
00053 return grid
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 def corr(grid, acc_probs, t=.9):
00075 width, height = grid.dims
00076 n = int(sqrt(len(acc_probs)))
00077 m = (n - 1) // 2
00078 new_grid = Grid2D(grid.dims, (0, 0, 0, 1))
00079
00080 for index in grid.index_iter():
00081 r = random()
00082 k = 0
00083
00084 while r > acc_probs[k]:
00085 k += 1
00086
00087 i, j = index
00088 x = i + k % n - m
00089 y = j + k // n - m
00090
00091 if x < 0:
00092 x += width
00093 elif x >= width:
00094 x -= width
00095
00096 if y < 0:
00097 y += height
00098 elif y >= height:
00099 y -= height
00100
00101 new_grid[index] = mix_grey(grid[index], grid[int(x), int(y)], t)
00102
00103 return new_grid
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 def corr2(grid, props, t = .9):
00119 width, height = grid.dims
00120 n = props.dims[0]
00121 m = (n - 1) // 2
00122 new_grid = Grid2D(grid.dims, (0, 0, 0, 1))
00123
00124 for index in grid.index_iter():
00125 new_grey = 0
00126 for window_index, props_index_iter in zip(grid.wrapped_square_index_iter(index, m), props.index_iter()):
00127 new_grey += grid[window_index] * props[props_index_iter]
00128 new_grid[index] = mix_grey(grid[index], new_grey, t)
00129
00130 return new_grid
00131
00132
00133
00134
00135 def corr_window(grid, n):
00136 m = 2*n + 1
00137 energy_grid = Grid2D((m, m), 0)
00138 average_coef = 1 / (grid.width * grid.height)
00139
00140 for index in grid.index_iter():
00141 for window_cell, energy_index in zip(grid.wrapped_square_iter(index, n), energy_grid.index_iter()):
00142 energy_grid[energy_index] += abs(window_cell - grid[index]) * average_coef
00143
00144 return energy_grid
00145
00146
00147
00148
00149 def add_circle(grid, center, radius, amplitude):
00150 x, y = center
00151 width, height = grid.dims
00152 radius_sqr = radius * radius
00153 for yi in xrange(2 * radius + 1):
00154 yc = radius - yi
00155
00156 yy = int(y + yc)
00157
00158 if yy < 0 or yy >= height:
00159 continue
00160
00161 xc = int(ceil(sqrt(radius_sqr - yc*yc)))
00162
00163 for xx in xrange(max(0, x - xc), min(x + xc + 1, width - 1)):
00164 grid[xx, yy] += amplitude
00165
00166
00167
00168
00169
00170 def add_cone(grid, center, radius, amplitude):
00171 x, y = center
00172 radius_sqr = radius * radius
00173 for yi in xrange(2 * radius + 1):
00174 yc = radius - yi
00175 xc = int(ceil(sqrt(radius_sqr - yc*yc)))
00176 yy = int(y + yc)
00177 for xx in xrange(x - xc, x + xc + 1):
00178 grid[xx, yy] += amplitude * (1 + radius - sqrt(yc*yc + (xx-x)*(xx-x)))/(1 + radius)
00179
00180
00181
00182
00183 def add_square(grid, center, radius, amplitude):
00184 x, y = center
00185 for yy in xrange(y - radius, y + radius + 1):
00186 for xx in xrange(x - radius, x + radius + 1):
00187 grid[xx, yy] += amplitude
00188
00189
00190
00191
00192 def scale_lists_to_int(samples, scale):
00193 new_samples = []
00194 x_scale, y_scale = scale
00195
00196 for sample in samples:
00197 x, y = sample
00198 new_samples.append((int(x * x_scale), int(y * y_scale)))
00199
00200 return new_samples
00201
00202
00203 def samples_to_estimation_grid(samples, image_dims, shape_function=add_circle):
00204 grid = Grid2D(image_dims, 0)
00205 samples = scale_lists_to_int(samples, image_dims)
00206 k = 0
00207 maxp = -1
00208
00209 for point in samples:
00210 k += 1
00211 add_circle(grid, point, 5, 0.5)
00212
00213 grid = normalize_grey(grid)
00214
00215 return grid
00216
00217
00218
00219
00220 def samples_to_image(samples, image_dims, fname, shape_function=add_circle):
00221 grid = samples_to_estimation_grid(samples, image_dims, shape_function)
00222 grid = greyscale_grid_to_rgb_grid(grid, (1, 1, 1, 1))
00223 grid_to_rgb_image(grid, fname)