|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Drawing; |
| 7 | +using System.Drawing.Imaging; |
| 8 | +using System.Text; |
| 9 | +using Microsoft.ML.Runtime; |
| 10 | +using Microsoft.ML.Runtime.CommandLine; |
| 11 | +using Microsoft.ML.Runtime.Data; |
| 12 | +using Microsoft.ML.Runtime.EntryPoints; |
| 13 | +using Microsoft.ML.Runtime.Internal.Utilities; |
| 14 | +using Microsoft.ML.Runtime.Model; |
| 15 | +using Microsoft.ML.Runtime.ImageAnalytics; |
| 16 | + |
| 17 | +[assembly: LoadableClass(ImageGrayscaleTransform.Summary, typeof(ImageGrayscaleTransform), typeof(ImageGrayscaleTransform.Arguments), typeof(SignatureDataTransform), |
| 18 | + ImageGrayscaleTransform.UserName, "ImageGrayscaleTransform", "ImageGrayscale")] |
| 19 | + |
| 20 | +[assembly: LoadableClass(ImageGrayscaleTransform.Summary, typeof(ImageGrayscaleTransform), null, typeof(SignatureLoadDataTransform), |
| 21 | + ImageGrayscaleTransform.UserName, ImageGrayscaleTransform.LoaderSignature)] |
| 22 | + |
| 23 | +namespace Microsoft.ML.Runtime.ImageAnalytics |
| 24 | +{ |
| 25 | + // REVIEW: Rewrite as LambdaTransform to simplify. |
| 26 | + // REVIEW: Should it be separate transform or part of ImageResizerTransform? |
| 27 | + /// <summary> |
| 28 | + /// Transform which takes one or many columns of <see cref="ImageType"/> type in IDataView and |
| 29 | + /// convert them to greyscale representation of the same image. |
| 30 | + /// </summary> |
| 31 | + public sealed class ImageGrayscaleTransform : OneToOneTransformBase |
| 32 | + { |
| 33 | + public sealed class Column : OneToOneColumn |
| 34 | + { |
| 35 | + public static Column Parse(string str) |
| 36 | + { |
| 37 | + var res = new Column(); |
| 38 | + if (res.TryParse(str)) |
| 39 | + return res; |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + public bool TryUnparse(StringBuilder sb) |
| 44 | + { |
| 45 | + Contracts.AssertValue(sb); |
| 46 | + return TryUnparseCore(sb); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public class Arguments : TransformInputBase |
| 51 | + { |
| 52 | + [Argument(ArgumentType.Multiple | ArgumentType.Required, HelpText = "New column definition(s) (optional form: name:src)", ShortName = "col", SortOrder = 1)] |
| 53 | + public Column[] Column; |
| 54 | + } |
| 55 | + |
| 56 | + internal const string Summary = "Convert image into grayscale."; |
| 57 | + |
| 58 | + internal const string UserName = "Image Greyscale Transform"; |
| 59 | + public const string LoaderSignature = "ImageGrayscaleTransform"; |
| 60 | + private static VersionInfo GetVersionInfo() |
| 61 | + { |
| 62 | + return new VersionInfo( |
| 63 | + modelSignature: "IMGGRAYT", |
| 64 | + verWrittenCur: 0x00010001, // Initial |
| 65 | + verReadableCur: 0x00010001, |
| 66 | + verWeCanReadBack: 0x00010001, |
| 67 | + loaderSignature: LoaderSignature); |
| 68 | + } |
| 69 | + |
| 70 | + private const string RegistrationName = "ImageGrayscale"; |
| 71 | + |
| 72 | + // Public constructor corresponding to SignatureDataTransform. |
| 73 | + public ImageGrayscaleTransform(IHostEnvironment env, Arguments args, IDataView input) |
| 74 | + : base(env, RegistrationName, env.CheckRef(args, nameof(args)).Column, input, t => t is ImageType ? null : "Expected Image type") |
| 75 | + { |
| 76 | + Host.AssertNonEmpty(Infos); |
| 77 | + Host.Assert(Infos.Length == Utils.Size(args.Column)); |
| 78 | + Metadata.Seal(); |
| 79 | + } |
| 80 | + |
| 81 | + private ImageGrayscaleTransform(IHost host, ModelLoadContext ctx, IDataView input) |
| 82 | + : base(host, ctx, input, t => t is ImageType ? null : "Expected Image type") |
| 83 | + { |
| 84 | + Host.AssertValue(ctx); |
| 85 | + // *** Binary format *** |
| 86 | + // <base> |
| 87 | + Host.AssertNonEmpty(Infos); |
| 88 | + Metadata.Seal(); |
| 89 | + } |
| 90 | + |
| 91 | + public static ImageGrayscaleTransform Create(IHostEnvironment env, ModelLoadContext ctx, IDataView input) |
| 92 | + { |
| 93 | + Contracts.CheckValue(env, nameof(env)); |
| 94 | + var h = env.Register(RegistrationName); |
| 95 | + h.CheckValue(ctx, nameof(ctx)); |
| 96 | + h.CheckValue(input, nameof(input)); |
| 97 | + ctx.CheckAtModel(GetVersionInfo()); |
| 98 | + return h.Apply("Loading Model", ch => new ImageGrayscaleTransform(h, ctx, input)); |
| 99 | + } |
| 100 | + |
| 101 | + public override void Save(ModelSaveContext ctx) |
| 102 | + { |
| 103 | + Host.CheckValue(ctx, nameof(ctx)); |
| 104 | + ctx.CheckAtModel(); |
| 105 | + ctx.SetVersionInfo(GetVersionInfo()); |
| 106 | + |
| 107 | + // *** Binary format *** |
| 108 | + // <base> |
| 109 | + SaveBase(ctx); |
| 110 | + } |
| 111 | + |
| 112 | + protected override ColumnType GetColumnTypeCore(int iinfo) |
| 113 | + { |
| 114 | + Host.Assert(0 <= iinfo & iinfo < Infos.Length); |
| 115 | + return Infos[iinfo].TypeSrc; |
| 116 | + } |
| 117 | + |
| 118 | + private static readonly ColorMatrix _grayscaleColorMatrix = new ColorMatrix( |
| 119 | + new float[][] |
| 120 | + { |
| 121 | + new float[] {.3f, .3f, .3f, 0, 0}, |
| 122 | + new float[] {.59f, .59f, .59f, 0, 0}, |
| 123 | + new float[] {.11f, .11f, .11f, 0, 0}, |
| 124 | + new float[] {0, 0, 0, 1, 0}, |
| 125 | + new float[] {0, 0, 0, 0, 1} |
| 126 | + }); |
| 127 | + |
| 128 | + protected override Delegate GetGetterCore(IChannel ch, IRow input, int iinfo, out Action disposer) |
| 129 | + { |
| 130 | + Host.AssertValueOrNull(ch); |
| 131 | + Host.AssertValue(input); |
| 132 | + Host.Assert(0 <= iinfo && iinfo < Infos.Length); |
| 133 | + |
| 134 | + var src = default(Bitmap); |
| 135 | + var getSrc = GetSrcGetter<Bitmap>(input, iinfo); |
| 136 | + |
| 137 | + disposer = |
| 138 | + () => |
| 139 | + { |
| 140 | + if (src != null) |
| 141 | + { |
| 142 | + src.Dispose(); |
| 143 | + src = null; |
| 144 | + } |
| 145 | + }; |
| 146 | + |
| 147 | + ValueGetter<Bitmap> del = |
| 148 | + (ref Bitmap dst) => |
| 149 | + { |
| 150 | + if (dst != null) |
| 151 | + dst.Dispose(); |
| 152 | + |
| 153 | + getSrc(ref src); |
| 154 | + if (src == null || src.Height <= 0 || src.Width <= 0) |
| 155 | + return; |
| 156 | + |
| 157 | + dst = new Bitmap(src.Width, src.Height); |
| 158 | + ImageAttributes attributes = new ImageAttributes(); |
| 159 | + attributes.SetColorMatrix(_grayscaleColorMatrix); |
| 160 | + var srcRectangle = new Rectangle(0, 0, src.Width, src.Height); |
| 161 | + using (var g = Graphics.FromImage(dst)) |
| 162 | + { |
| 163 | + g.DrawImage(src, srcRectangle, 0, 0, src.Width, src.Height, GraphicsUnit.Pixel, attributes); |
| 164 | + } |
| 165 | + Host.Assert(dst.Width == src.Width && dst.Height == src.Height); |
| 166 | + }; |
| 167 | + |
| 168 | + return del; |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments