2using System.Collections.Generic;
6using System.Drawing.Drawing2D;
7using System.Drawing.Imaging;
25 BRIGHTNESS_CONTRAST_GAMMA,
29 BRIGHTNESS_GAMMA_CONTRAST
39 public static Bitmap
ResizeImage(Image image,
int width,
int height)
41 if (image.Width == width && image.Height == height)
42 return new Bitmap(image);
44 var destRect =
new Rectangle(0, 0, width, height);
45 var destImage =
new Bitmap(width, height);
47 destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
49 using (var graphics = Graphics.FromImage(destImage))
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;
57 using (var wrapMode =
new ImageAttributes())
59 wrapMode.SetWrapMode(WrapMode.TileFlipXY);
60 graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
75 public static Bitmap
AdjustContrast(Image bmp,
float fBrightness = 0.0f,
float fContrast = 1.0f,
float fGamma = 1.0f)
77 float fAdjBrightNess = fBrightness - 1.0f;
80 new float[] { fContrast, 0, 0, 0, 0 },
81 new float[] { 0, fContrast, 0, 0, 0 },
82 new float[] { 0, 0, fContrast, 0, 0 },
83 new float[] { 0, 0, 0, 1.0f, 0 },
84 new float[] { fAdjBrightNess, fAdjBrightNess, fAdjBrightNess, 0, 1 }
87 ImageAttributes imageAttributes =
new ImageAttributes();
88 imageAttributes.ClearColorMatrix();
89 imageAttributes.SetColorMatrix(
new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
90 imageAttributes.SetGamma(fGamma, ColorAdjustType.Bitmap);
92 Bitmap bmpNew =
new Bitmap(bmp.Width, bmp.Height);
94 using (Graphics g = Graphics.FromImage(bmpNew))
96 g.DrawImage(bmp,
new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, imageAttributes);
102 private static int truncate(
int n)
122 private static void applyBrightness(ref
int nR, ref
int nG, ref
int nB,
float fBrightness)
124 nR = truncate(nR + (
int)fBrightness);
125 nG = truncate(nG + (
int)fBrightness);
126 nB = truncate(nB + (
int)fBrightness);
139 private static void applyContrast(ref
int nR, ref
int nG, ref
int nB,
float fContrast)
141 double dfC = (fContrast - 1.0f) * 255.0;
142 double dfFactor = (259.0f * (dfC + 255.0)) / (255.0 * (259 - dfC));
144 nR = truncate((
int)(dfFactor * (nR - 128) + 128));
145 nG = truncate((
int)(dfFactor * (nG - 128) + 128));
146 nB = truncate((
int)(dfFactor * (nB - 128) + 128));
159 private static void applyGamma(ref
int nR, ref
int nG, ref
int nB,
float fGamma)
161 double dfG = 1.0 / fGamma;
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));
179 if (fBrightness == 0 && fContrast == 1 && fGamma == 1)
183 throw new Exception(
"AdjustContrast only valid on ByteData!");
190 throw new Exception(
"AdjustContrast requires 3 channels!");
192 int nSpatialDim = nH * nW;
193 for (
int h = 0; h < nH; h++)
195 for (
int w = 0; w < nW; w++)
197 int nIdxR = (nSpatialDim * 0) + ((h * nW) + w);
198 int nIdxG = (nSpatialDim * 1) + ((h * nW) + w);
199 int nIdxB = (nSpatialDim * 2) + ((h * nW) + w);
204 if (fBrightness != 0)
205 applyBrightness(ref nR, ref nG, ref nB, fBrightness);
209 if (fContrast != 1.0f)
210 applyContrast(ref nR, ref nG, ref nB, fContrast);
213 applyGamma(ref nR, ref nG, ref nB, fGamma);
218 applyGamma(ref nR, ref nG, ref nB, fGamma);
220 if (fContrast != 1.0f)
221 applyContrast(ref nR, ref nG, ref nB, fContrast);
238 MemoryStream ms =
new MemoryStream();
239 imageIn.Save(ms,
System.Drawing.Imaging.ImageFormat.Png);
251 MemoryStream ms =
new MemoryStream(byteArrayIn);
252 Image returnImage = Image.FromStream(ms);
263 public static Image
Center(Bitmap bmp, Color clrBackground)
265 int nTopYColorRow = getFirstColorRow(bmp, clrBackground);
266 int nBottomYColorRow = getLastColorRow(bmp, clrBackground);
268 if (nTopYColorRow <= 0 && nBottomYColorRow >= bmp.Height - 1)
271 int nVerticalShift = (((bmp.Height - 1) - nBottomYColorRow) - nTopYColorRow) / 2;
273 if (nVerticalShift == 0)
276 Bitmap bmpNew =
new Bitmap(bmp.Width, bmp.Height);
277 Brush br =
new SolidBrush(clrBackground);
279 using (Graphics g = Graphics.FromImage(bmpNew))
281 g.FillRectangle(br, 0, 0, bmp.Width, bmp.Height);
282 g.DrawImage(bmp, 0, nVerticalShift);
290 private static int getFirstColorRow(Bitmap bmp, Color clrTarget)
292 for (
int y = 0; y < bmp.Height; y++)
294 for (
int x = 0; x < bmp.Width; x++)
296 Color clr = bmp.GetPixel(x, y);
298 if (clr.R != clrTarget.R || clr.G != clrTarget.G || clr.B != clrTarget.B || clr.A != clrTarget.A)
306 private static int getLastColorRow(Bitmap bmp, Color clrTarget)
308 for (
int y = bmp.Height - 1; y >= 0; y-- )
310 for (
int x = 0; x < bmp.Width; x++)
312 Color clr = bmp.GetPixel(x, y);
314 if (clr.R != clrTarget.R || clr.G != clrTarget.G || clr.B != clrTarget.B || clr.A != clrTarget.A)
The SimpleDatum class holds a data input within host memory.
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.