Add additional processing.

This commit is contained in:
2024-04-24 20:26:33 -04:00
parent 56a231ed47
commit cb49db49ca

View File

@@ -4,7 +4,6 @@ using System.IO;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;
using System.Security.Permissions;
namespace MarketData.CNNProcessing
{
@@ -15,9 +14,18 @@ namespace MarketData.CNNProcessing
private PointMapping pointMapping;
private int width;
private int height;
public ImageHelper()
{
}
public ImageHelper(ImageHelper imageHelper)
{
this.bitmap=Copy(imageHelper.bitmap);
width=bitmap.Width;
height=bitmap.Height;
}
public void Dispose()
{
if(null!=bitmap)
@@ -32,6 +40,21 @@ namespace MarketData.CNNProcessing
}
}
private Bitmap Copy(Bitmap bitmap)
{
Bitmap copy = new Bitmap(bitmap.Width, bitmap.Height);
for(int rowIndex=0;rowIndex<bitmap.Height;rowIndex++)
{
for(int colIndex=0;colIndex<bitmap.Width;colIndex++)
{
Color color=bitmap.GetPixel(rowIndex,colIndex);
copy.SetPixel(rowIndex,colIndex,color);
}
}
return copy;
}
public bool LoadImage(string pathFileName)
{
Stream bitmapStream = null;
@@ -157,10 +180,12 @@ namespace MarketData.CNNProcessing
Save(pathFileName,bwBitmap);
bwBitmap.Dispose();
}
public void Save(String pathFileName)
{
Save(pathFileName,null);
}
private void Save(String pathFileName,Bitmap altBitmap=null)
{
Validate();
@@ -171,7 +196,7 @@ namespace MarketData.CNNProcessing
// Convert to 8 bits per pixel black and white
private Bitmap ToBlackAndWhite(Bitmap bmp)
{
var result = new Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
Bitmap result = new Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
BitmapData data = result.LockBits(new Rectangle(0, 0, result.Width, result.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
byte[] bytes = new byte[data.Height * data.Stride];
Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
@@ -190,27 +215,52 @@ namespace MarketData.CNNProcessing
return result;
}
// Convert to 8 bits per pixel gray scale
// private Bitmap ToGrayScale(Bitmap bmp)
//{
// var result = new Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
// BitmapData data = result.LockBits(new Rectangle(0, 0, result.Width, result.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
// byte[] bytes = new byte[data.Height * data.Stride];
// Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
// for (int y = 0; y < bmp.Height; y++)
// {
// for (int x = 0; x < bmp.Width; x++)
// {
// Color color = bmp.GetPixel(x, y);
// byte rgb = (byte)((color.R + color.G + color.B) / 3);
// bytes[y * data.Stride + x] = rgb;
// }
// }
// Marshal.Copy(bytes, 0, data.Scan0, bytes.Length);
// result.UnlockBits(data);
// return result;
// }
public void RotateRight()
{
Bitmap result = new Bitmap(bitmap.Height, bitmap.Width);
for(int rowIndex=0;rowIndex<bitmap.Height;rowIndex++)
{
for(int colIndex=0;colIndex<bitmap.Width;colIndex++)
{
Color color=GetPixel(rowIndex,colIndex);
result.SetPixel((bitmap.Width-1)-colIndex,rowIndex,color);
}
}
bitmap.Dispose();
bitmap=result;
width=bitmap.Width;
height=bitmap.Height;
}
public void RotateLeft()
{
Bitmap result = new Bitmap(bitmap.Height, bitmap.Width);
for(int rowIndex=0;rowIndex<bitmap.Height;rowIndex++)
{
for(int colIndex=0;colIndex<bitmap.Width;colIndex++)
{
Color color=GetPixel(rowIndex,colIndex);
result.SetPixel(colIndex, (bitmap.Width-1)-rowIndex,color);
}
}
bitmap.Dispose();
bitmap=result;
width=bitmap.Width;
height=bitmap.Height;
}
public void ToGrayScale()
{
Validate();
Bitmap grayScaleBitmap = ToGrayScale(bitmap);
bitmap.Dispose();
bitmap=grayScaleBitmap;
width=bitmap.Width;
height=bitmap.Height;
}
private Bitmap ToGrayScale(Bitmap bmp)
{
@@ -229,6 +279,64 @@ namespace MarketData.CNNProcessing
return bitmap;
}
public void Blur(Int32 blurSize)
{
Bitmap blurBmp=Blur(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height), blurSize);
bitmap.Dispose();
bitmap=blurBmp;
height=bitmap.Height;
width=bitmap.Width;
}
private static Bitmap Blur(Bitmap image, Rectangle rectangle, Int32 blurSize)
{
Bitmap blurred = new Bitmap(image.Width, image.Height);
// make an exact copy of the bitmap provided
using (Graphics graphics = Graphics.FromImage(blurred))
graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
// look at every pixel in the blur rectangle
for (int xx = rectangle.X; xx < rectangle.X + rectangle.Width; xx++)
{
for (int yy = rectangle.Y; yy < rectangle.Y + rectangle.Height; yy++)
{
int avgR = 0, avgG = 0, avgB = 0;
int blurPixelCount = 0;
// average the color of the red, green and blue for each pixel in the
// blur size while making sure you don't go outside the image bounds
for (int x = xx; (x < xx + blurSize && x < image.Width); x++)
{
for (int y = yy; (y < yy + blurSize && y < image.Height); y++)
{
Color pixel = blurred.GetPixel(x, y);
avgR += pixel.R;
avgG += pixel.G;
avgB += pixel.B;
blurPixelCount++;
}
}
avgR = avgR / blurPixelCount;
avgG = avgG / blurPixelCount;
avgB = avgB / blurPixelCount;
// now that we know the average for the blur size, set each pixel to that color
for (int x = xx; x < xx + blurSize && x < image.Width && x < rectangle.Width; x++)
{
for (int y = yy; y < yy + blurSize && y < image.Height && y < rectangle.Height; y++)
{
blurred.SetPixel(x, y, Color.FromArgb(avgR, avgG, avgB));
}
}
}
}
return blurred;
}
public Color GetPixel(int x,int y)
{
return bitmap.GetPixel(x,y);