MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
ImageTools.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Drawing;
6using System.Drawing.Drawing2D;
7using System.Drawing.Imaging;
8using System.IO;
9
10namespace MyCaffe.basecode
11{
15 public class ImageTools
16 {
21 {
25 BRIGHTNESS_CONTRAST_GAMMA,
29 BRIGHTNESS_GAMMA_CONTRAST
30 }
31
39 public static Bitmap ResizeImage(Image image, int width, int height)
40 {
41 if (image.Width == width && image.Height == height)
42 return new Bitmap(image);
43
44 var destRect = new Rectangle(0, 0, width, height);
45 var destImage = new Bitmap(width, height);
46
47 destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
48
49 using (var graphics = Graphics.FromImage(destImage))
50 {
51 graphics.CompositingMode = CompositingMode.SourceCopy;
52 graphics.CompositingQuality = CompositingQuality.HighQuality;
53 graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
54 graphics.SmoothingMode = SmoothingMode.HighQuality;
55 graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
56
57 using (var wrapMode = new ImageAttributes())
58 {
59 wrapMode.SetWrapMode(WrapMode.TileFlipXY);
60 graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
61 }
62 }
63
64 return destImage;
65 }
66
75 public static Bitmap AdjustContrast(Image bmp, float fBrightness = 0.0f, float fContrast = 1.0f, float fGamma = 1.0f)
76 {
77 float fAdjBrightNess = fBrightness - 1.0f;
78 float[][] ptsArray =
79 {
80 new float[] { fContrast, 0, 0, 0, 0 }, // scale red.
81 new float[] { 0, fContrast, 0, 0, 0 }, // scale green.
82 new float[] { 0, 0, fContrast, 0, 0 }, // scale blue.
83 new float[] { 0, 0, 0, 1.0f, 0 }, // don't scale alpha.
84 new float[] { fAdjBrightNess, fAdjBrightNess, fAdjBrightNess, 0, 1 }
85 };
86
87 ImageAttributes imageAttributes = new ImageAttributes();
88 imageAttributes.ClearColorMatrix();
89 imageAttributes.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
90 imageAttributes.SetGamma(fGamma, ColorAdjustType.Bitmap);
91
92 Bitmap bmpNew = new Bitmap(bmp.Width, bmp.Height);
93
94 using (Graphics g = Graphics.FromImage(bmpNew))
95 {
96 g.DrawImage(bmp, new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, imageAttributes);
97 }
98
99 return bmpNew;
100 }
101
102 private static int truncate(int n)
103 {
104 if (n < 0)
105 return 0;
106 else if (n > 255)
107 return 255;
108 else
109 return n;
110 }
111
122 private static void applyBrightness(ref int nR, ref int nG, ref int nB, float fBrightness)
123 {
124 nR = truncate(nR + (int)fBrightness);
125 nG = truncate(nG + (int)fBrightness);
126 nB = truncate(nB + (int)fBrightness);
127 }
128
139 private static void applyContrast(ref int nR, ref int nG, ref int nB, float fContrast)
140 {
141 double dfC = (fContrast - 1.0f) * 255.0;
142 double dfFactor = (259.0f * (dfC + 255.0)) / (255.0 * (259 - dfC));
143
144 nR = truncate((int)(dfFactor * (nR - 128) + 128));
145 nG = truncate((int)(dfFactor * (nG - 128) + 128));
146 nB = truncate((int)(dfFactor * (nB - 128) + 128));
147 }
148
159 private static void applyGamma(ref int nR, ref int nG, ref int nB, float fGamma)
160 {
161 double dfG = 1.0 / fGamma;
162
163 nR = (int)(255.0 * Math.Pow((nR / 255.0), dfG));
164 nG = (int)(255.0 * Math.Pow((nG / 255.0), dfG));
165 nB = (int)(255.0 * Math.Pow((nB / 255.0), dfG));
166 }
167
177 public static void AdjustContrast(SimpleDatum sd, float fBrightness = 0.0f, float fContrast = 1.0f, float fGamma = 1.0f, ADJUSTCONTRAST_ORDERING ordering = ADJUSTCONTRAST_ORDERING.BRIGHTNESS_CONTRAST_GAMMA)
178 {
179 if (fBrightness == 0 && fContrast == 1 && fGamma == 1)
180 return;
181
182 if (sd.IsRealData)
183 throw new Exception("AdjustContrast only valid on ByteData!");
184
185 int nC = sd.Channels;
186 int nH = sd.Height;
187 int nW = sd.Width;
188
189 if (nC != 3)
190 throw new Exception("AdjustContrast requires 3 channels!");
191
192 int nSpatialDim = nH * nW;
193 for (int h = 0; h < nH; h++)
194 {
195 for (int w = 0; w < nW; w++)
196 {
197 int nIdxR = (nSpatialDim * 0) + ((h * nW) + w);
198 int nIdxG = (nSpatialDim * 1) + ((h * nW) + w);
199 int nIdxB = (nSpatialDim * 2) + ((h * nW) + w);
200 int nR = sd.ByteData[nIdxR];
201 int nG = sd.ByteData[nIdxG];
202 int nB = sd.ByteData[nIdxB];
203
204 if (fBrightness != 0)
205 applyBrightness(ref nR, ref nG, ref nB, fBrightness);
206
207 if (ordering == ADJUSTCONTRAST_ORDERING.BRIGHTNESS_CONTRAST_GAMMA)
208 {
209 if (fContrast != 1.0f)
210 applyContrast(ref nR, ref nG, ref nB, fContrast);
211
212 if (fGamma != 1.0f)
213 applyGamma(ref nR, ref nG, ref nB, fGamma);
214 }
215 else
216 {
217 if (fGamma != 1.0f)
218 applyGamma(ref nR, ref nG, ref nB, fGamma);
219
220 if (fContrast != 1.0f)
221 applyContrast(ref nR, ref nG, ref nB, fContrast);
222 }
223
224 sd.ByteData[nIdxR] = (byte)nR;
225 sd.ByteData[nIdxG] = (byte)nG;
226 sd.ByteData[nIdxB] = (byte)nB;
227 }
228 }
229 }
230
236 public static byte[] ImageToByteArray(Image imageIn)
237 {
238 MemoryStream ms = new MemoryStream();
239 imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
240
241 return ms.ToArray();
242 }
243
249 public static Image ByteArrayToImage(byte[] byteArrayIn)
250 {
251 MemoryStream ms = new MemoryStream(byteArrayIn);
252 Image returnImage = Image.FromStream(ms);
253
254 return returnImage;
255 }
256
263 public static Image Center(Bitmap bmp, Color clrBackground)
264 {
265 int nTopYColorRow = getFirstColorRow(bmp, clrBackground);
266 int nBottomYColorRow = getLastColorRow(bmp, clrBackground);
267
268 if (nTopYColorRow <= 0 && nBottomYColorRow >= bmp.Height - 1)
269 return bmp;
270
271 int nVerticalShift = (((bmp.Height - 1) - nBottomYColorRow) - nTopYColorRow) / 2;
272
273 if (nVerticalShift == 0)
274 return bmp;
275
276 Bitmap bmpNew = new Bitmap(bmp.Width, bmp.Height);
277 Brush br = new SolidBrush(clrBackground);
278
279 using (Graphics g = Graphics.FromImage(bmpNew))
280 {
281 g.FillRectangle(br, 0, 0, bmp.Width, bmp.Height);
282 g.DrawImage(bmp, 0, nVerticalShift);
283 }
284
285 br.Dispose();
286
287 return bmpNew;
288 }
289
290 private static int getFirstColorRow(Bitmap bmp, Color clrTarget)
291 {
292 for (int y = 0; y < bmp.Height; y++)
293 {
294 for (int x = 0; x < bmp.Width; x++)
295 {
296 Color clr = bmp.GetPixel(x, y);
297
298 if (clr.R != clrTarget.R || clr.G != clrTarget.G || clr.B != clrTarget.B || clr.A != clrTarget.A)
299 return y;
300 }
301 }
302
303 return bmp.Height;
304 }
305
306 private static int getLastColorRow(Bitmap bmp, Color clrTarget)
307 {
308 for (int y = bmp.Height - 1; y >= 0; y-- )
309 {
310 for (int x = 0; x < bmp.Width; x++)
311 {
312 Color clr = bmp.GetPixel(x, y);
313
314 if (clr.R != clrTarget.R || clr.G != clrTarget.G || clr.B != clrTarget.B || clr.A != clrTarget.A)
315 return y;
316 }
317 }
318
319 return -1;
320 }
321 }
322}
The ImageTools class is a helper class used to manipulate image data.
Definition: ImageTools.cs:16
static void AdjustContrast(SimpleDatum sd, float fBrightness=0.0f, float fContrast=1.0f, float fGamma=1.0f, ADJUSTCONTRAST_ORDERING ordering=ADJUSTCONTRAST_ORDERING.BRIGHTNESS_CONTRAST_GAMMA)
The AdjustContrast function adjusts the brightness, contrast and gamma of the image and returns the n...
Definition: ImageTools.cs:177
static Image Center(Bitmap bmp, Color clrBackground)
Find the first and last colored rows of an image and centers the colored portion of the image vertica...
Definition: ImageTools.cs:263
static byte[] ImageToByteArray(Image imageIn)
Converts an Image into an array of byte.
Definition: ImageTools.cs:236
ADJUSTCONTRAST_ORDERING
Defines the odering for which the AdjustContrast applies brightness, contrast and gamma adjustments.
Definition: ImageTools.cs:21
static Image ByteArrayToImage(byte[] byteArrayIn)
Converts an array of byte into an Image.
Definition: ImageTools.cs:249
static Bitmap ResizeImage(Image image, int width, int height)
Resize the image to the specified width and height.
Definition: ImageTools.cs:39
static Bitmap AdjustContrast(Image bmp, float fBrightness=0.0f, float fContrast=1.0f, float fGamma=1.0f)
The AdjustContrast function adjusts the brightness, contrast and gamma of the image and returns the n...
Definition: ImageTools.cs:75
The SimpleDatum class holds a data input within host memory.
Definition: SimpleDatum.cs:161
int Channels
Return the number of channels of the data.
bool IsRealData
Returns whether or not the data contains real numbers or byte data.
int Width
Return the width of the data.
byte[] ByteData
Return the byte data. This field is valid when IsRealData = false.
int Height
Return the height of the data.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12