forked from TensorStack-AI/TensorStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
203 lines (181 loc) · 7.18 KB
/
Extensions.cs
File metadata and controls
203 lines (181 loc) · 7.18 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
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using TensorStack.Common.Tensor;
namespace TensorStack.Image
{
public static class Extensions
{
/// <summary>
/// Converts ImageTensor to BitmapSource.
/// </summary>
/// <param name="imageTensor">The image tensor.</param>
/// <returns>BitmapSource.</returns>
public static BitmapSource ToImage(this ImageTensor imageTensor)
{
return imageTensor.ToBitmapImage();
}
/// <summary>
/// Converts ImageTensorBase to ImageTensor.
/// </summary>
/// <param name="imageTensor">The image tensor.</param>
/// <returns>ImageTensor.</returns>
public static ImageInput ToImageInput(this ImageTensor imageTensor)
{
return new ImageInput(imageTensor);
}
/// <summary>
/// Saves the ImageTensor.
/// </summary>
/// <param name="imageTensor">The image tensor.</param>
/// <param name="filename">The filename.</param>
public static Task SaveAsync(this ImageTensor imageTensor, string filename)
{
return Task.Run(() =>
{
using (var image = imageTensor.ToImageInput())
{
image.Save(filename);
}
});
}
/// <summary>
/// Saves the ImageTensor.
/// </summary>
/// <param name="imageTensor">The image tensor.</param>
/// <param name="filename">The filename.</param>
public static Task SaveAsync(this ImageInput imageTensor, string filename)
{
return Task.Run(() => imageTensor.Save(filename));
}
/// <summary>
/// Saves the specified image to file.
/// </summary>
/// <param name="bitmap">The bitmap.</param>
/// <param name="filePath">The file path.</param>
internal static void Save(this WriteableBitmap bitmap, string filePath)
{
ImageService.DefaultDispatcher.Invoke(() =>
{
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
using (FileStream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
encoder.Save(stream);
}
});
}
/// <summary>
/// Converts ImageTensor to WriteableBitmap.
/// </summary>
/// <param name="imageTensor">The image tensor.</param>
/// <returns>WriteableBitmap.</returns>
internal static WriteableBitmap ToBitmapImage(this ImageTensor imageTensor)
{
var channels = imageTensor.Dimensions[1];
var height = imageTensor.Dimensions[2];
var width = imageTensor.Dimensions[3];
return ImageService.DefaultDispatcher.Invoke(() =>
{
if (channels == 1)
return imageTensor.ToSingleChannelImage();
var stride = width * 4;
var pixelBuffer = new byte[height * stride];
var writeableBitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Pbgra32, null);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int pixelIndex = (y * width + x) * 4;
pixelBuffer[pixelIndex + 0] = GetByteValue(imageTensor[0, 2, y, x]); // B
pixelBuffer[pixelIndex + 1] = GetByteValue(imageTensor[0, 1, y, x]); // G
pixelBuffer[pixelIndex + 2] = GetByteValue(imageTensor[0, 0, y, x]); // R
pixelBuffer[pixelIndex + 3] = channels == 4 ? GetByteValue(imageTensor[0, 3, y, x]) : byte.MaxValue; // A
}
}
writeableBitmap.WritePixels(new Int32Rect(0, 0, width, height), pixelBuffer, stride, 0);
return writeableBitmap;
});
}
/// <summary>
/// Converts WriteableBitmap to ImageTensor.
/// </summary>
/// <param name="writeableBitmap">The writeable bitmap.</param>
/// <returns>ImageTensor.</returns>
internal static ImageTensor ToTensor(this WriteableBitmap writeableBitmap)
{
var width = writeableBitmap.PixelWidth;
var height = writeableBitmap.PixelHeight;
var tensor = new ImageTensor(new[] { 1, 4, height, width });
unsafe
{
byte* buffer = (byte*)writeableBitmap.BackBuffer.ToPointer();
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int pixelIndex = (y * width + x) * 4; // BGRA
tensor[0, 0, y, x] = GetFloatValue(buffer[pixelIndex + 2]); // R
tensor[0, 1, y, x] = GetFloatValue(buffer[pixelIndex + 1]); // G
tensor[0, 2, y, x] = GetFloatValue(buffer[pixelIndex + 0]); // B
tensor[0, 3, y, x] = GetFloatValue(buffer[pixelIndex + 3]); // A
}
}
}
return tensor;
}
/// <summary>
/// Converts to single channel Image.
/// </summary>
/// <param name="imageTensor">The image tensor.</param>
/// <returns>WriteableBitmap.</returns>
private static WriteableBitmap ToSingleChannelImage(this ImageTensor imageTensor)
{
var width = imageTensor.Dimensions[3];
var height = imageTensor.Dimensions[2];
byte[] pixels = new byte[width * height];
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
pixels[y * width + x] = GetByteValue(imageTensor[0, 0, y, x]);
}
}
var writeableBitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Gray8, null);
writeableBitmap.Lock();
try
{
IntPtr buffer = writeableBitmap.BackBuffer;
Marshal.Copy(pixels, 0, buffer, pixels.Length);
writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
}
finally
{
writeableBitmap.Unlock();
}
return writeableBitmap;
}
/// <summary>
/// Gets the normalized byte value.
/// </summary>
/// <param name="value">The value.</param>
private static byte GetByteValue(this float value)
{
return (byte)Math.Round(Math.Clamp(value / 2 + 0.5, 0, 1) * 255);
}
/// <summary>
/// Gets the normalized float value.
/// </summary>
/// <param name="value">The value.</param>
private static float GetFloatValue(this byte value)
{
return (value / 255.0f) * 2.0f - 1.0f;
}
}
}