-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathImageTiles.cs
More file actions
233 lines (199 loc) · 7.9 KB
/
ImageTiles.cs
File metadata and controls
233 lines (199 loc) · 7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright (c) TensorStack. All rights reserved.
// Licensed under the Apache 2.0 License.
using System;
using System.Collections.Generic;
using TensorStack.Common.Tensor;
namespace TensorStack.Common.Image
{
public static class ImageTiles
{
/// <summary>
/// Computes the tiles.
/// </summary>
/// <param name="inputImage">The input image.</param>
/// <param name="tileSize">Size of the tile.</param>
/// <param name="maxTileSize">Maximum size of the tile.</param>
/// <returns>List<TileJob>.</returns>
public static List<TileJob> ComputeTiles(ImageTensor inputImage, int tileSize, int maxTileSize)
{
var ys = ComputeOffsets(inputImage.Height, tileSize);
var xs = ComputeOffsets(inputImage.Width, tileSize);
var imageTiles = new List<TileJob>();
foreach (int y in ys)
foreach (int x in xs)
imageTiles.Add(new TileJob(x, y, maxTileSize));
return imageTiles;
}
/// <summary>
/// Compute tile offsets (step = tile - overlap). Last tile clamped backward.
/// </summary>
/// <param name="full">The full.</param>
/// <param name="step">The step.</param>
private static int[] ComputeOffsets(int full, int step)
{
if (full <= step)
return [0];
var list = new List<int>();
for (int pos = 0; pos + step < full; pos += step)
list.Add(pos);
int last = full - step;
if (list[^1] != last)
list.Add(last);
return list.ToArray();
}
/// <summary>
/// Creates the weight sum.
/// </summary>
/// <param name="outputImage">The output image.</param>
public static Tensor<float> CreateWeightSum(ImageTensor outputImage)
{
return new Tensor<float>([1, 1, outputImage.Height, outputImage.Width]);
}
/// <summary>
/// Weighted feathering map for tile blending
/// </summary>
/// <param name="tileSize">Size of the tile.</param>
/// <param name="overlap">The overlap.</param>
public static float[,] CreateWeightMap(int tileSize, int overlap)
{
var w = new float[tileSize, tileSize];
for (int y = 0; y < tileSize; y++)
{
float wy = 1f;
if (y < overlap)
wy = (float)y / overlap;
else if (y >= tileSize - overlap)
wy = (float)(tileSize - y - 1) / overlap;
for (int x = 0; x < tileSize; x++)
{
float wx = 1f;
if (x < overlap)
wx = (float)x / overlap;
else if (x >= tileSize - overlap)
wx = (float)(tileSize - x - 1) / overlap;
w[y, x] = MathF.Min(wx, wy);
}
}
return w;
}
/// <summary>
/// Extracts the tile span.
/// </summary>
/// <param name="imageTensor">The image tensor.</param>
/// <param name="posX">The position x.</param>
/// <param name="posY">The position y.</param>
/// <param name="tileSize">Size of the tile.</param>
/// <param name="channels">The channels.</param>
/// <returns>TensorSpan<System.Single>.</returns>
public static TensorSpan<float> ExtractTileSpan(Tensor<float> imageTensor, int posX, int posY, int tileSize, int channels)
{
int height = imageTensor.Dimensions[2];
int width = imageTensor.Dimensions[3];
var tileShape = new[] { 1, channels, tileSize, tileSize };
var tileSpan = new TensorSpan<float>(tileShape);
for (int c = 0; c < channels; c++)
{
for (int y = 0; y < tileSize; y++)
{
int srcY = Math.Min(posY + y, height - 1);
for (int x = 0; x < tileSize; x++)
{
int srcX = Math.Min(posX + x, width - 1);
tileSpan[0, c, y, x] = imageTensor[0, c, srcY, srcX];
}
}
}
return tileSpan;
}
/// <summary>
/// Blends the tile into the outputImage.
/// </summary>
/// <param name="outputImage">The output image.</param>
/// <param name="weightSum">The weight sum.</param>
/// <param name="tile">The tile.</param>
/// <param name="weight">The weight.</param>
/// <param name="posX">The position x.</param>
/// <param name="posY">The position y.</param>
public static void BlendTile(ImageTensor output, Tensor<float> weightSum, Tensor<float> tile, float[,] weight, int posX, int posY, int scaleFactor)
{
var channels = tile.Dimensions[1];
var tileSize = tile.Dimensions[2];
var outH = output.Height;
var outW = output.Width;
var outHW = outH * outW;
var tileHW = tileSize * tileSize;
var outSpan = output.Memory.Span;
var tileSpan = tile.Memory.Span;
var weightSumSpan = weightSum.Memory.Span;
var outX0 = posX * scaleFactor;
var outY0 = posY * scaleFactor;
var weightH = weight.GetLength(0);
var weightW = weight.GetLength(1);
Span<int> wxLut = stackalloc int[tileSize];
Span<int> wyLut = stackalloc int[tileSize];
for (int i = 0; i < tileSize; i++)
{
wxLut[i] = Math.Min(i / scaleFactor, weightW - 1);
wyLut[i] = Math.Min(i / scaleFactor, weightH - 1);
}
for (int y = 0; y < tileSize; y++)
{
int oy = outY0 + y;
if ((uint)oy >= (uint)outH)
continue;
int wy = wyLut[y];
int outRow = oy * outW;
for (int x = 0; x < tileSize; x++)
{
int ox = outX0 + x;
if ((uint)ox >= (uint)outW)
continue;
float w = weight[wy, wxLut[x]];
if (w == 0f)
continue;
int outIdx = outRow + ox;
int tileIdx = y * tileSize + x;
int o = outIdx;
int t = tileIdx;
for (int c = 0; c < channels; c++)
{
outSpan[o] += tileSpan[t] * w;
o += outHW;
t += tileHW;
}
weightSumSpan[outIdx] += w;
}
}
}
/// <summary>
/// Normalizes the specified output.
/// </summary>
/// <param name="output">The output.</param>
/// <param name="weightSum">The weight sum.</param>
public static ImageTensor Normalize(ImageTensor output, Tensor<float> weightSum)
{
var height = output.Height;
var width = output.Width;
var channels = output.Dimensions[1];
var outSpan = output.Memory.Span;
var weightSpan = weightSum.Memory.Span;
var hw = height * width;
var channelStride = hw;
for (int i = 0; i < hw; i++)
{
float w = weightSpan[i];
if (w <= 0f)
continue;
float inv = 1f / w;
int baseIdx = i;
for (int c = 0; c < channels; c++)
{
outSpan[baseIdx] *= inv;
baseIdx += channelStride;
}
}
return output;
}
}
public record TileJob(int X, int Y, int TileSize);
}