-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
ConvertToGrayScaleInMemory.cs
90 lines (78 loc) · 4.16 KB
/
ConvertToGrayScaleInMemory.cs
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
using System;
using System.Drawing;
using Microsoft.ML;
using Microsoft.ML.Transforms.Image;
namespace Samples.Dynamic
{
class ConvertToGrayScaleInMemory
{
public static void Example()
{
var mlContext = new MLContext();
// Create an image list.
var images = new[] { new ImageDataPoint(2, 3, Color.Blue), new
ImageDataPoint(2, 3, Color.Red) };
// Convert the list of data points to an IDataView object, which is
// consumable by ML.NET API.
var data = mlContext.Data.LoadFromEnumerable(images);
// Convert image to gray scale.
var pipeline = mlContext.Transforms.ConvertToGrayscale("GrayImage",
"Image");
// Fit the model.
var model = pipeline.Fit(data);
// Test path: image files -> IDataView -> Enumerable of Bitmaps.
var transformedData = model.Transform(data);
// Load images in DataView back to Enumerable.
var transformedDataPoints = mlContext.Data.CreateEnumerable<
ImageDataPoint>(transformedData, false);
// Print out input and output pixels.
foreach (var dataPoint in transformedDataPoints)
{
var image = dataPoint.Image;
var grayImage = dataPoint.GrayImage;
for (int x = 0; x < grayImage.Width; ++x)
{
for (int y = 0; y < grayImage.Height; ++y)
{
var pixel = image.GetPixel(x, y);
var grayPixel = grayImage.GetPixel(x, y);
Console.WriteLine($"The original pixel is {pixel} and its" +
$"pixel in gray is {grayPixel}");
}
}
}
// Expected output:
// The original pixel is Color[A = 255, R = 0, G = 0, B = 255] and its pixel in gray is Color[A = 255, R = 28, G = 28, B = 28]
// The original pixel is Color[A = 255, R = 0, G = 0, B = 255] and its pixel in gray is Color[A = 255, R = 28, G = 28, B = 28]
// The original pixel is Color[A = 255, R = 0, G = 0, B = 255] and its pixel in gray is Color[A = 255, R = 28, G = 28, B = 28]
// The original pixel is Color[A = 255, R = 0, G = 0, B = 255] and its pixel in gray is Color[A = 255, R = 28, G = 28, B = 28]
// The original pixel is Color[A = 255, R = 0, G = 0, B = 255] and its pixel in gray is Color[A = 255, R = 28, G = 28, B = 28]
// The original pixel is Color[A = 255, R = 0, G = 0, B = 255] and its pixel in gray is Color[A = 255, R = 28, G = 28, B = 28]
// The original pixel is Color[A = 255, R = 255, G = 0, B = 0] and its pixel in gray is Color[A = 255, R = 77, G = 77, B = 77]
// The original pixel is Color[A = 255, R = 255, G = 0, B = 0] and its pixel in gray is Color[A = 255, R = 77, G = 77, B = 77]
// The original pixel is Color[A = 255, R = 255, G = 0, B = 0] and its pixel in gray is Color[A = 255, R = 77, G = 77, B = 77]
// The original pixel is Color[A = 255, R = 255, G = 0, B = 0] and its pixel in gray is Color[A = 255, R = 77, G = 77, B = 77]
// The original pixel is Color[A = 255, R = 255, G = 0, B = 0] and its pixel in gray is Color[A = 255, R = 77, G = 77, B = 77]
// The original pixel is Color[A = 255, R = 255, G = 0, B = 0] and its pixel in gray is Color[A = 255, R = 77, G = 77, B = 77]
}
private class ImageDataPoint
{
[ImageType(3, 4)]
public Bitmap Image { get; set; }
[ImageType(3, 4)]
public Bitmap GrayImage { get; set; }
public ImageDataPoint()
{
Image = null;
GrayImage = null;
}
public ImageDataPoint(int width, int height, Color color)
{
Image = new Bitmap(width, height);
for (int i = 0; i < width; ++i)
for (int j = 0; j < height; ++j)
Image.SetPixel(i, j, color);
}
}
}
}