Add image processing capabilities for jpg

This commit is contained in:
2024-04-15 10:31:44 -04:00
parent c0c1d37bf0
commit a9fee9ca7b

View File

@@ -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();
}
}
}
/// <summary>
/// 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.
/// </summary>
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);
}
/// <summary>
/// Resize image to given width and height. The documenation on CNN's indicates that resizing will work better than maintaining apsect
/// ration with padding
/// </summary>
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;
}
}
}
/// <summary>
/// Resize the image to the specified width and height.
/// </summary>
/// <param name="image">The image to resize.</param>
/// <param name="width">The width to resize to.</param>
/// <param name="height">The height to resize to.</param>
/// <returns>The resized image.</returns>
//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)
{