diff --git a/MarketDataLib/CNNProcessing/ImageHelper.cs b/MarketDataLib/CNNProcessing/ImageHelper.cs index 569722f..2d0b93f 100644 --- a/MarketDataLib/CNNProcessing/ImageHelper.cs +++ b/MarketDataLib/CNNProcessing/ImageHelper.cs @@ -1,16 +1,10 @@ using System; -using System.Collections.Generic; -using System.Collections; -using System.Text; using System.Drawing; -using System.Diagnostics; -using System.Globalization; using System.IO; -using System.Linq; -using MarketData.Utils; using System.Drawing.Imaging; using System.Runtime.InteropServices; using System.Drawing.Drawing2D; +using System.Security.Permissions; namespace MarketData.CNNProcessing { @@ -37,6 +31,128 @@ namespace MarketData.CNNProcessing graphics=null; } } + + public bool LoadImage(string pathFileName) + { + Stream bitmapStream = null; + try + { + bitmapStream = File.Open(pathFileName,FileMode.Open); + Image image = Image.FromStream(bitmapStream); + bitmap=new Bitmap(image); + width=bitmap.Width; + height=bitmap.Height; + return true; + } + catch(Exception) + { + return false; + } + finally + { + if(null!=bitmapStream) + { + bitmapStream.Close(); + bitmapStream.Dispose(); + bitmapStream.Dispose(); + } + } + } + + /// + /// Resize image to given width maintaining aspect ration for height. + /// If aspect ration is used for CNN then you will need to introduce padding. For instance 640,640 image and then paste the aspect + /// mainained image onto that. The CNN will then have to learn that black means nothing. Resizing is the recommended way to go. + /// + public bool Resize(int newWidth) + { + double aspectRatio=(double)bitmap.Width/(double)bitmap.Height; + int newHeight=(int)(((double)newWidth)/aspectRatio); + if(0!=newHeight%2)newHeight++; + return Resize(newWidth,newHeight); + } + + /// + /// Resize image to given width and height. The documenation on CNN's indicates that resizing will work better than maintaining apsect + /// ration with padding + /// + public bool Resize(int newWidth, int newHeight) + { + Graphics graphics = null; + ImageAttributes imageAttributes = null; + try + { + Validate(); + Rectangle destRect=new Rectangle(0,0,newWidth,newHeight); + Bitmap destBitmap = new Bitmap(newWidth,newHeight); + + destBitmap.SetResolution(bitmap.HorizontalResolution,bitmap.VerticalResolution); + graphics = Graphics.FromImage(destBitmap); + graphics.CompositingMode = CompositingMode.SourceCopy; + graphics.CompositingQuality = CompositingQuality.HighQuality; + graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; + graphics.SmoothingMode = SmoothingMode.HighQuality; + graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; + imageAttributes = new ImageAttributes(); + imageAttributes.SetWrapMode(WrapMode.TileFlipXY); + graphics.DrawImage(bitmap, destRect, 0,0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, imageAttributes); + bitmap.Dispose(); + bitmap=destBitmap; + imageAttributes.Dispose(); + graphics.Dispose(); + return true; + } + catch(Exception) + { + return false; + } + finally + { + if(null!=graphics) + { + graphics.Dispose(); + graphics=null; + } + if(null!=imageAttributes) + { + imageAttributes.Dispose(); + imageAttributes=null; + } + } + } + + /// + /// Resize the image to the specified width and height. + /// + /// The image to resize. + /// The width to resize to. + /// The height to resize to. + /// The resized image. + //public static Bitmap ResizeImage(Image image, int width, int height) + //{ + // var destRect = new Rectangle(0, 0, width, height); + // var destImage = new Bitmap(width, height); + + // destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); + + // using (var graphics = Graphics.FromImage(destImage)) + // { + // graphics.CompositingMode = CompositingMode.SourceCopy; + // graphics.CompositingQuality = CompositingQuality.HighQuality; + // graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; + // graphics.SmoothingMode = SmoothingMode.HighQuality; + // graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; + + // using (var wrapMode = new ImageAttributes()) + // { + // wrapMode.SetWrapMode(WrapMode.TileFlipXY); + // graphics.DrawImage(image, destRect, 0, 0, image.Width,image.Height, GraphicsUnit.Pixel, wrapMode); + // } + // } + + // return destImage; + //} + public void CreateImage(int width, int height,PointMapping pointMapping) { this.width=width; @@ -69,6 +185,10 @@ namespace MarketData.CNNProcessing Bitmap bwBitmap=ToBlackAndWhite(bitmap); Save(pathFileName,bwBitmap); bwBitmap.Dispose(); + } + public void Save(String pathFileName) + { + Save(pathFileName,null); } private void Save(String pathFileName,Bitmap altBitmap=null) {