概述
静态类,可以直接使用
/*
* fengyhack@gmail.com
* copyleft (L) 2013
* fengyhack provide the whole code and
* thanks for CWK&CXC
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
namespace ImageProc
{
public class MyBitmapUtility
{
public enum ColorMode
{
Gray = 1,
Red,
Green,
Blue,
RGB,
ARGB
}
public enum GrayMethod
{
MinValue = 1,
MaxValue,
Average,
Weighted
}
public enum NoiseType
{
Gauss = 1,
PepperSalt
}
public enum WnkType
{
PFFT = 1,
ILPF,
IHP,
BLPF,
BHPF,
ELPF,
EHPF
}
public class Complex
{
private double real;
private double imag;
public double Real
{
get
{
return this.real;
}
set
{
this.real = value;
}
}
public double Imag
{
get
{
return this.imag;
}
set
{
this.imag = value;
}
}
public Complex()
{
this.real = 0.0;
this.imag = 0.0;
}
public Complex(double dReal, double dImag)
{
this.real = dReal;
this.imag = dImag;
}
public Complex(MyBitmapUtility.Complex other)
{
this.real = other.real;
this.imag = other.imag;
}
public static MyBitmapUtility.Complex operator +(MyBitmapUtility.Complex comp1, MyBitmapUtility.Complex comp2)
{
return comp1.Add(comp2);
}
public static MyBitmapUtility.Complex operator -(MyBitmapUtility.Complex comp1, MyBitmapUtility.Complex comp2)
{
return comp1.Subtract(comp2);
}
public static MyBitmapUtility.Complex operator *(MyBitmapUtility.Complex comp, double factor)
{
return comp.Multiply(factor);
}
public static MyBitmapUtility.Complex operator *(double factor, MyBitmapUtility.Complex comp)
{
return comp.Multiply(factor);
}
public static MyBitmapUtility.Complex operator *(MyBitmapUtility.Complex comp1, MyBitmapUtility.Complex comp2)
{
return comp1.Multiply(comp2);
}
public MyBitmapUtility.Complex Add(MyBitmapUtility.Complex comp)
{
double dReal = this.real + comp.real;
double dImag = this.imag + comp.imag;
return new MyBitmapUtility.Complex(dReal, dImag);
}
public MyBitmapUtility.Complex Subtract(MyBitmapUtility.Complex comp)
{
double dReal = this.real - comp.real;
double dImag = this.imag - comp.imag;
return new MyBitmapUtility.Complex(dReal, dImag);
}
public MyBitmapUtility.Complex Multiply(double factor)
{
double dReal = this.real * factor;
double dImag = this.imag * factor;
return new MyBitmapUtility.Complex(dReal, dImag);
}
public MyBitmapUtility.Complex Multiply(MyBitmapUtility.Complex comp)
{
double dReal = this.real * comp.real - this.imag * comp.imag;
double dImag = this.real * comp.imag + this.imag * comp.real;
return new MyBitmapUtility.Complex(dReal, dImag);
}
public double SquareNorm()
{
double num = this.real * this.real;
double num2 = this.imag * this.imag;
return num + num2;
}
public double AbsNorm()
{
double d = this.SquareNorm();
return Math.Sqrt(d);
}
public double Angle()
{
if (this.real == 0.0 && this.imag == 0.0)
{
return 0.0;
}
if (this.real == 0.0)
{
if (this.imag == 0.0)
{
return 0.0;
}
if (this.imag > 0.0)
{
return 1.5707963267948966;
}
return -1.5707963267948966;
}
else
{
if (this.real > 0.0)
{
return Math.Atan2(this.imag, this.real);
}
if (this.imag >= 0.0)
{
return Math.Atan2(this.imag, this.real) + 3.1415926535897931;
}
return Math.Atan2(this.imag, this.real) - 3.1415926535897931;
}
}
public MyBitmapUtility.Complex Conjugate()
{
return new MyBitmapUtility.Complex(this.real, -this.imag);
}
}
public const int pixBytes = 4;
public static byte MaxOfThree(byte a, byte b, byte c)
{
byte b2 = a;
if (b > b2)
{
b2 = b;
}
if (c > b2)
{
b2 = c;
}
return b2;
}
public static byte MinOfThree(byte a, byte b, byte c)
{
byte b2 = a;
if (b < b2)
{
b2 = b;
}
if (c < b2)
{
b2 = c;
}
return b2;
}
public static byte[,] InitByteArray(int width, int height, byte vdef = 0)
{
byte[,] array = new byte[width, height];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
array[j, i] = vdef;
}
}
return array;
}
public static byte[] InitByteSequence(int width, int height, byte vdef = 0)
{
byte[] array = new byte[width * height];
int num = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
array[num++] = vdef;
}
}
return array;
}
public static Bitmap LoadBitmapFromFile(string strFileName)
{
Bitmap bitmap = (Bitmap)Image.FromFile(strFileName, true);
int width = bitmap.Width;
int height = bitmap.Height;
Bitmap bitmap2 = new Bitmap(width, height, PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(bitmap2);
graphics.DrawImage(bitmap, new Rectangle(0, 0, width, height), new Rectangle(0, 0, width, height), GraphicsUnit.Pixel);
graphics.Save();
graphics.Dispose();
bitmap.Dispose();
return bitmap2;
}
public static void SaveBitmapToFile(Bitmap srcBitmap)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Title = "图片另存为...";
saveFileDialog.OverwritePrompt = true;
saveFileDialog.Filter = "BMP文件(*.bmp)|*.bmp|JPG文件(*.jpg)|*.jpg|TIF文件(*.tif)|*.tif|PNG文件(*.png)|*.png";
DialogResult dialogResult = saveFileDialog.ShowDialog();
if (dialogResult == DialogResult.OK)
{
if (saveFileDialog.FileName == "")
{
return;
}
string fileName = saveFileDialog.FileName;
string text = fileName.Remove(0, fileName.Length - 3);
text = text.ToLower();
string a;
if ((a = text) != null)
{
if (a == "bmp")
{
srcBitmap.Save(fileName, ImageFormat.Bmp);
return;
}
if (a == "jpg")
{
srcBitmap.Save(fileName, ImageFormat.Jpeg);
return;
}
if (a == "tif")
{
srcBitmap.Save(fileName, ImageFormat.Tiff);
return;
}
if (!(a == "png"))
{
return;
}
srcBitmap.Save(fileName, ImageFormat.Png);
}
}
}
public static void AutoSaveImage(Bitmap srcBitmap, string strPrefix = "AutoSave")
{
string text = Directory.GetCurrentDirectory();
text += "\Output\";
if (!Directory.Exists(text))
{
Directory.CreateDirectory(text);
}
DateTime dateTime = DateTime.Now.ToLocalTime();
string text2 = text + "[";
text2 = text2 + strPrefix + "]Modified_";
text2 += dateTime.ToLongDateString();
text2 = text2 + dateTime.Hour.ToString() + "时";
text2 = text2 + dateTime.Minute.ToString() + "分";
text2 = text2 + dateTime.Second.ToString() + "秒";
text2 += dateTime.Millisecond.ToString();
text2 += ".bmp";
srcBitmap.Save(text2, ImageFormat.Bmp);
MessageBox.Show(text2 + "n文件已保存.");
}
public unsafe static double[] StatRgbValues(Bitmap srcBitmap, MyBitmapUtility.ColorMode cm = MyBitmapUtility.ColorMode.Gray)
{
double[] array = new double[256];
int width = srcBitmap.Width;
int height = srcBitmap.Height;
BitmapData bitmapData = srcBitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
byte* ptr = (byte*)((void*)bitmapData.Scan0);
int num = bitmapData.Stride - width * 4;
for (int i = 0; i < 256; i++)
{
array[i] = 0.0;
}
int num2;
switch (cm)
{
case MyBitmapUtility.ColorMode.Red:
num2 = 2;
goto IL_90;
case MyBitmapUtility.ColorMode.Green:
num2 = 1;
goto IL_90;
}
num2 = 0;
IL_90:
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
array[(int)ptr[(IntPtr)num2 / 1]] += 1.0;
ptr += (IntPtr)4 / 1;
}
ptr += (IntPtr)num / 1;
}
double num3 = 0.0;
for (int i = 0; i < 256; i++)
{
double num4 = array[i];
if (num4 > num3)
{
num3 = num4;
}
}
for (int i = 0; i < 256; i++)
{
array[i] /= num3;
}
srcBitmap.UnlockBits(bitmapData);
return array;
}
public static Bitmap GetHistgram(Bitmap srcBitmap, MyBitmapUtility.ColorMode cm = MyBitmapUtility.ColorMode.Gray)
{
double[] array = MyBitmapUtility.StatRgbValues(srcBitmap, cm);
int num = 256;
Color color = Color.Gray;
switch (cm)
{
case MyBitmapUtility.ColorMode.Red:
color = Color.Red;
break;
case MyBitmapUtility.ColorMode.Green:
color = Color.Green;
break;
case MyBitmapUtility.ColorMode.Blue:
color = Color.Blue;
break;
}
Pen pen = new Pen(color, 1f);
Bitmap bitmap = new Bitmap(num, num + 20);
Graphics graphics = Graphics.FromImage(bitmap);
for (int i = 0; i < num; i++)
{
double num2 = 1.0 - array[i];
int y = (int)((double)num * num2);
graphics.DrawLine(pen, i + 1, y, i + 1, num);
}
graphics.DrawLine(Pens.Black, new Point(0, num), new Point(num, num));
Font font = new Font("Times", 9f);
graphics.DrawString("0", font, Brushes.Black, new PointF(0f, (float)num));
graphics.DrawString("255", font, Brushes.Black, new PointF((float)(num - 20), (float)num));
graphics.Save();
graphics.Dispose();
return bitmap;
}
public unsafe static Bitmap ConvertBitmapToGray(Bitmap srcBitmap, MyBitmapUtility.GrayMethod mthd = MyBitmapUtility.GrayMethod.Weighted)
{
int width = srcBitmap.Width;
int height = srcBitmap.Height;
BitmapData bitmapData = srcBitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
byte* ptr = (byte*)((void*)bitmapData.Scan0);
int num = bitmapData.Stride - width * 4;
switch (mthd)
{
case MyBitmapUtility.GrayMethod.MinValue:
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
byte b = ptr[(IntPtr)2 / 1];
byte b2 = ptr[(IntPtr)1 / 1];
byte b3 = *ptr;
byte b4 = MyBitmapUtility.MinOfThree(b, b2, b3);
*ptr = (ptr[(IntPtr)1 / 1] = (*(ptr + (IntPtr)2 / 1) = b4));
ptr += (IntPtr)4 / 1;
}
ptr += (IntPtr)num / 1;
}
goto IL_201;
case MyBitmapUtility.GrayMethod.MaxValue:
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
byte b = ptr[(IntPtr)2 / 1];
byte b2 = ptr[(IntPtr)1 / 1];
byte b3 = *ptr;
byte b4 = MyBitmapUtility.MaxOfThree(b, b2, b3);
*ptr = (ptr[(IntPtr)1 / 1] = (*(ptr + (IntPtr)2 / 1) = b4));
ptr += (IntPtr)4 / 1;
}
ptr += (IntPtr)num / 1;
}
goto IL_201;
case MyBitmapUtility.GrayMethod.Average:
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
byte b = ptr[(IntPtr)2 / 1];
byte b2 = ptr[(IntPtr)1 / 1];
byte b3 = *ptr;
byte b4 = (byte)((double)((b + b2 + b3) / 3) + 0.5);
*ptr = (ptr[(IntPtr)1 / 1] = (*(ptr + (IntPtr)2 / 1) = b4));
ptr += (IntPtr)4 / 1;
}
ptr += (IntPtr)num / 1;
}
goto IL_201;
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
byte b = ptr[(IntPtr)2 / 1];
byte b2 = ptr[(IntPtr)1 / 1];
byte b3 = *ptr;
byte b4 = (byte)(38 * b + 75 * b2 + 15 * b3 >> 7);
*ptr = (ptr[(IntPtr)1 / 1] = (*(ptr + (IntPtr)2 / 1) = b4));
ptr += (IntPtr)4 / 1;
}
ptr += (IntPtr)num / 1;
}
IL_201:
srcBitmap.UnlockBits(bitmapData);
return srcBitmap;
}
public unsafe static byte[,] BitmapToByteArray(Bitmap srcBitmap, MyBitmapUtility.ColorMode cm = MyBitmapUtility.ColorMode.Blue)
{
int width = srcBitmap.Width;
int height = srcBitmap.Height;
byte[,] array = MyBitmapUtility.InitByteArray(height, width, 0);
BitmapData bitmapData = srcBitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
byte* ptr = (byte*)((void*)bitmapData.Scan0);
int num = bitmapData.Stride - width * 4;
int num2;
switch (cm)
{
case MyBitmapUtility.ColorMode.Red:
num2 = 2;
goto IL_70;
case MyBitmapUtility.ColorMode.Green:
num2 = 1;
goto IL_70;
}
num2 = 0;
IL_70:
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
array[i, j] = ptr[(IntPtr)num2 / 1];
ptr += (IntPtr)4 / 1;
}
ptr += (IntPtr)num / 1;
}
srcBitmap.UnlockBits(bitmapData);
return array;
}
public unsafe static byte[] BitmapToByteSequence(Bitmap srcBitmap, MyBitmapUtility.ColorMode cm = MyBitmapUtility.ColorMode.Blue)
{
int width = srcBitmap.Width;
int height = srcBitmap.Height;
byte[] array = MyBitmapUtility.InitByteSequence(height, width, 0);
BitmapData bitmapData = srcBitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
byte* ptr = (byte*)((void*)bitmapData.Scan0);
int num = bitmapData.Stride - width * 4;
int num2;
switch (cm)
{
case MyBitmapUtility.ColorMode.Red:
num2 = 2;
goto IL_70;
case MyBitmapUtility.ColorMode.Green:
num2 = 1;
goto IL_70;
}
num2 = 0;
IL_70:
int num3 = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
array[num3++] = ptr[(IntPtr)num2 / 1];
ptr += (IntPtr)4 / 1;
}
ptr += (IntPtr)num / 1;
}
srcBitmap.UnlockBits(bitmapData);
return array;
}
public unsafe static Bitmap ArrayToGrayBitmap(byte[,] srcArray, int width, int height)
{
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
byte* ptr = (byte*)((void*)bitmapData.Scan0);
int num = bitmapData.Stride - width * 4;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
*ptr = (ptr[(IntPtr)1 / 1] = (*(ptr + (IntPtr)2 / 1) = srcArray[i, j]));
ptr[(IntPtr)3 / 1] = 255;
ptr += (IntPtr)4 / 1;
}
ptr += (IntPtr)num / 1;
}
bitmap.UnlockBits(bitmapData);
return bitmap;
}
public unsafe static Bitmap ArrayToColorBitmap(byte[,] redArray, byte[,] greenArray, byte[,] blueArray, int width, int height)
{
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
byte* ptr = (byte*)((void*)bitmapData.Scan0);
int num = bitmapData.Stride - width * 4;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
*ptr = blueArray[i, j];
ptr[(IntPtr)1 / 1] = greenArray[i, j];
ptr[(IntPtr)2 / 1] = redArray[i, j];
ptr[(IntPtr)3 / 1] = 255;
ptr += (IntPtr)4 / 1;
}
ptr += (IntPtr)num / 1;
}
bitmap.UnlockBits(bitmapData);
return bitmap;
}
public unsafe static Bitmap SequenceToGrayBitmap(byte[] srcArray, int width, int height, bool shift = true)
{
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
byte* ptr = (byte*)((void*)bitmapData.Scan0);
int stride = bitmapData.Stride;
int num = bitmapData.Stride - width * 4;
byte* ptr2 = null;
int num2 = 0;
for (int i = 0; i < height; i++)
{
if (i < height / 2)
{
ptr2 = ptr + (IntPtr)(stride * height / 2) / 1;
}
else
{
ptr2 = ptr - (IntPtr)(stride * height / 2) / 1;
}
for (int j = 0; j < width; j++)
{
if (j < width / 2)
{
ptr2 += (IntPtr)(4 * width / 2) / 1;
}
else
{
ptr2 -= (IntPtr)(4 * width / 2) / 1;
}
*ptr2 = (ptr2[(IntPtr)1 / 1] = (*(ptr2 + (IntPtr)2 / 1) = srcArray[num2++]));
ptr2[(IntPtr)3 / 1] = 255;
ptr += (IntPtr)4 / 1;
}
ptr += (IntPtr)num / 1;
}
bitmap.UnlockBits(bitmapData);
return bitmap;
}
public unsafe static Bitmap SequenceToColorBitmap(byte[] redArray, byte[] greenArray, byte[] blueArray, int width, int height)
{
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
byte* ptr = (byte*)((void*)bitmapData.Scan0);
int num = bitmapData.Stride - width * 4;
int num2 = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
*ptr = blueArray[num2];
ptr[(IntPtr)1 / 1] = greenArray[num2];
ptr[(IntPtr)2 / 1] = redArray[num2];
ptr[(IntPtr)3 / 1] = 255;
num2++;
ptr += (IntPtr)4 / 1;
}
ptr += (IntPtr)num / 1;
}
bitmap.UnlockBits(bitmapData);
return bitmap;
}
public static byte[,] BitmapToBinaryArray(Bitmap srcBitmap, byte threshold)
{
int width = srcBitmap.Width;
int height = srcBitmap.Height;
srcBitmap = MyBitmapUtility.ConvertBitmapToGray(srcBitmap, MyBitmapUtility.GrayMethod.Weighted);
byte[,] array = MyBitmapUtility.BitmapToByteArray(srcBitmap, MyBitmapUtility.ColorMode.Blue);
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
byte b = array[j, i];
array[i, j] = ((b > threshold) ? 255 : 0);
}
}
return array;
}
public unsafe static Bitmap BinaryArrayToBitmap(byte[,] srcBinArray, int width, int height, Color ck, Color cw)
{
byte a = ck.A;
byte r = ck.R;
byte g = ck.G;
byte b = ck.B;
byte a2 = cw.A;
byte r2 = cw.R;
byte g2 = cw.G;
byte b2 = cw.B;
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
byte* ptr = (byte*)((void*)bitmapData.Scan0);
int num = bitmapData.Stride - width * 4;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (srcBinArray[i, j] < 128)
{
*ptr = b;
ptr[(IntPtr)1 / 1] = g;
ptr[(IntPtr)2 / 1] = r;
ptr[(IntPtr)3 / 1] = a;
}
else
{
*ptr = b2;
ptr[(IntPtr)1 / 1] = g2;
ptr[(IntPtr)2 / 1] = r2;
ptr[(IntPtr)3 / 1] = a2;
}
ptr += (IntPtr)4 / 1;
}
ptr += (IntPtr)num / 1;
}
bitmap.UnlockBits(bitmapData);
return bitmap;
}
public unsafe static Bitmap FlipColorFast(Bitmap srcBitmap, MyBitmapUtility.ColorMode cm = MyBitmapUtility.ColorMode.RGB)
{
Bitmap bitmap = (Bitmap)srcBitmap.Clone();
int width = bitmap.Width;
int height = bitmap.Height;
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
int num = bitmapData.Stride * height;
byte* ptr = (byte*)((void*)bitmapData.Scan0);
switch (cm)
{
case MyBitmapUtility.ColorMode.Gray:
case MyBitmapUtility.ColorMode.Blue:
for (int i = 0; i < num; i += 4)
{
ptr[(IntPtr)i / 1] = ~(*(ptr + (IntPtr)i / 1));
}
goto IL_F2;
case MyBitmapUtility.ColorMode.Red:
for (int i = 2; i < num; i += 4)
{
ptr[(IntPtr)i / 1] = ~(*(ptr + (IntPtr)i / 1));
}
goto IL_F2;
case MyBitmapUtility.ColorMode.Green:
for (int i = 1; i < num; i += 4)
{
ptr[(IntPtr)i / 1] = ~(*(ptr + (IntPtr)i / 1));
}
goto IL_F2;
}
for (int i = 0; i < num; i++)
{
if (i % 4 != 3)
{
ptr[(IntPtr)i / 1] = ~(*(ptr + (IntPtr)i / 1));
}
}
IL_F2:
bitmap.UnlockBits(bitmapData);
return bitmap;
}
public unsafe static Bitmap EqualizeHist(Bitmap srcBitmap)
{
int width = srcBitmap.Width;
int height = srcBitmap.Height;
int[] array = new int[256];
int[] array2 = new int[256];
int[] array3 = new int[256];
int[] array4 = new int[256];
int[] array5 = new int[256];
int[] array6 = new int[256];
byte[] array7 = new byte[256];
byte[] array8 = new byte[256];
byte[] array9 = new byte[256];
Bitmap bitmap = (Bitmap)srcBitmap.Clone();
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
int num = bitmapData.Stride - width * 4;
byte* ptr = (byte*)((void*)bitmapData.Scan0);
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int num2 = (int)(*ptr);
int num3 = (int)ptr[(IntPtr)1 / 1];
int num4 = (int)ptr[(IntPtr)2 / 1];
array3[num2]++;
array2[num3]++;
array[num4]++;
ptr += (IntPtr)4 / 1;
}
ptr += (IntPtr)num / 1;
}
int num5 = width * height;
double num6 = 255.0 / (double)num5;
array4[0] = array[0];
array5[0] = array2[0];
array6[0] = array3[0];
for (int i = 1; i < 256; i++)
{
array4[i] = array4[i - 1] + array[i];
array5[i] = array5[i - 1] + array2[i];
array6[i] = array6[i - 1] + array3[i];
array7[i] = (byte)((double)array4[i] * num6 + 0.5);
array8[i] = (byte)((double)array5[i] * num6 + 0.5);
array9[i] = (byte)((double)array6[i] * num6 + 0.5);
}
ptr = (byte*)((void*)bitmapData.Scan0);
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int num2 = (int)(*ptr);
int num3 = (int)ptr[(IntPtr)1 / 1];
int num4 = (int)ptr[(IntPtr)2 / 1];
*ptr = array9[num2];
ptr[(IntPtr)1 / 1] = array8[num2];
ptr[(IntPtr)2 / 1] = array7[num2];
ptr += (IntPtr)4 / 1;
}
ptr += (IntPtr)num / 1;
}
bitmap.UnlockBits(bitmapData);
return bitmap;
}
public static Bitmap MirrorHorizenal(Bitmap srcBitmap)
{
Bitmap bitmap = (Bitmap)srcBitmap.Clone();
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);
return bitmap;
}
public static Bitmap MirrorVertical(Bitmap srcBitmap)
{
Bitmap bitmap = (Bitmap)srcBitmap.Clone();
bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
return bitmap;
}
public static Bitmap Thresholding(Bitmap srcBitmap, byte threshold)
{
int width = srcBitmap.Width;
int height = srcBitmap.Height;
byte[,] srcBinArray = MyBitmapUtility.BitmapToBinaryArray(srcBitmap, threshold);
return MyBitmapUtility.BinaryArrayToBitmap(srcBinArray, width, height, Color.White, Color.Black);
}
public static Bitmap ResizeBitmap(Bitmap srcBitmap, int dstWidth, int dstHeight)
{
int width = srcBitmap.Width;
int height = srcBitmap.Height;
Bitmap bitmap = new Bitmap(dstWidth, dstHeight, PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.DrawImage(srcBitmap, new Rectangle(0, 0, dstWidth, dstHeight), new Rectangle(0, 0, width, height), GraphicsUnit.Pixel);
graphics.Save();
graphics.Dispose();
return bitmap;
}
public static Bitmap RotateBitmap(Bitmap srcBitmap, float angle, Color bkColor)
{
int num = srcBitmap.Width + 2;
int num2 = srcBitmap.Height + 2;
Bitmap bitmap = new Bitmap(num, num2, PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(bkColor);
graphics.DrawImageUnscaled(srcBitmap, 1, 1);
graphics.Dispose();
GraphicsPath graphicsPath = new GraphicsPath();
graphicsPath.AddRectangle(new RectangleF(0f, 0f, (float)num, (float)num2));
Matrix matrix = new Matrix();
matrix.Rotate(angle);
RectangleF bounds = graphicsPath.GetBounds(matrix);
int num3 = (int)bounds.X;
int num4 = (int)bounds.Y;
num = (int)bounds.Width;
num2 = (int)bounds.Height;
Bitmap bitmap2 = new Bitmap(num, num2, PixelFormat.Format32bppArgb);
graphics = Graphics.FromImage(bitmap2);
graphics.Clear(bkColor);
graphics.TranslateTransform((float)(-(float)num3), (float)(-(float)num4));
graphics.RotateTransform(angle);
graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
graphics.DrawImageUnscaled(bitmap, 0, 0);
graphics.Dispose();
bitmap.Dispose();
return bitmap2;
}
public static Bitmap CropRegion(Bitmap srcBitmap, Region region)
{
Graphics graphics = Graphics.FromImage(srcBitmap);
RectangleF bounds = region.GetBounds(graphics);
int num = (int)bounds.X;
int num2 = (int)bounds.Y;
int width = (int)bounds.Width;
int height = (int)bounds.Height;
region.Translate(-num, -num2);
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
Graphics graphics2 = Graphics.FromImage(bitmap);
graphics2.SetClip(region, CombineMode.Replace);
graphics2.DrawImage(srcBitmap, new Rectangle(0, 0, width, height), bounds, GraphicsUnit.Pixel);
graphics.Dispose();
graphics2.Dispose();
srcBitmap.Dispose();
return bitmap;
}
public static Bitmap RemoveRegion(Bitmap srcBitmap, Region region)
{
int width = srcBitmap.Width;
int height = srcBitmap.Height;
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Region region2 = new Region(new Rectangle(0, 0, width, height));
region.Complement(region2);
graphics.SetClip(region, CombineMode.Replace);
graphics.DrawImage(srcBitmap, new Rectangle(0, 0, width, height), new Rectangle(0, 0, width, height), GraphicsUnit.Pixel);
srcBitmap.Dispose();
return bitmap;
}
public unsafe static Bitmap NoiseBlend(Bitmap srcBitmap, MyBitmapUtility.NoiseType nt, double[] paramNoise)
{
Bitmap bitmap = (Bitmap)srcBitmap.Clone();
int width = bitmap.Width;
int height = bitmap.Height;
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
Random random = new Random((int)DateTime.Now.Ticks);
Random random2 = new Random(~(int)DateTime.Now.Ticks);
byte* ptr = (byte*)((void*)bitmapData.Scan0);
int num = bitmapData.Stride - width * 4;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
double num4;
switch (nt)
{
case MyBitmapUtility.NoiseType.Gauss:
{
double num2;
do
{
num2 = random.NextDouble();
}
while (num2 <= 4.94065645841247E-324);
double num3 = random2.NextDouble();
num4 = Math.Sqrt(-2.0 * Math.Log(num2)) * Math.Cos(6.2831853071795862 * num3) * paramNoise[1] + paramNoise[0];
break;
}
case MyBitmapUtility.NoiseType.PepperSalt:
{
double num2;
do
{
num2 = random.NextDouble();
}
while (num2 + 4.94065645841247E-324 >= 1.0);
num4 = paramNoise[0] + Math.Sqrt(-1.0 * paramNoise[1] * Math.Log(1.0 - num2));
break;
}
default:
num4 = 0.0;
break;
}
num4 += (double)(*ptr);
byte b;
if (num4 > 255.0)
{
b = 255;
}
else
{
if (num4 < 0.0)
{
b = 0;
}
else
{
b = (byte)(num4 + 0.5);
}
}
*ptr = b;
ptr += (IntPtr)4 / 1;
}
ptr += (IntPtr)num / 1;
}
bitmap.UnlockBits(bitmapData);
return bitmap;
}
public static Bitmap NeighberAverSmooth(Bitmap srcBitmap)
{
int width = srcBitmap.Width;
int height = srcBitmap.Height;
byte[,] redArray = MyBitmapUtility.BitmapToByteArray(srcBitmap, MyBitmapUtility.ColorMode.Red);
byte[,] greenArray = MyBitmapUtility.BitmapToByteArray(srcBitmap, MyBitmapUtility.ColorMode.Green);
byte[,] blueArray = MyBitmapUtility.BitmapToByteArray(srcBitmap, MyBitmapUtility.ColorMode.Blue);
MyBitmapUtility.NeighberAverSmooth(ref redArray, width, height);
MyBitmapUtility.NeighberAverSmooth(ref greenArray, width, height);
MyBitmapUtility.NeighberAverSmooth(ref blueArray, width, height);
return MyBitmapUtility.ArrayToColorBitmap(redArray, greenArray, blueArray, width, height);
}
public static Bitmap NeighberMedSmooth(Bitmap srcBitmap)
{
int width = srcBitmap.Width;
int height = srcBitmap.Height;
byte[,] redArray = MyBitmapUtility.BitmapToByteArray(srcBitmap, MyBitmapUtility.ColorMode.Red);
byte[,] greenArray = MyBitmapUtility.BitmapToByteArray(srcBitmap, MyBitmapUtility.ColorMode.Green);
byte[,] blueArray = MyBitmapUtility.BitmapToByteArray(srcBitmap, MyBitmapUtility.ColorMode.Blue);
MyBitmapUtility.NeighberMedSmooth(ref redArray, width, height);
MyBitmapUtility.NeighberMedSmooth(ref greenArray, width, height);
MyBitmapUtility.NeighberMedSmooth(ref blueArray, width, height);
return MyBitmapUtility.ArrayToColorBitmap(redArray, greenArray, blueArray, width, height);
}
public static Bitmap NeighberSharpen(Bitmap srcBitmap)
{
int width = srcBitmap.Width;
int height = srcBitmap.Height;
byte[,] redArray = MyBitmapUtility.BitmapToByteArray(srcBitmap, MyBitmapUtility.ColorMode.Red);
byte[,] greenArray = MyBitmapUtility.BitmapToByteArray(srcBitmap, MyBitmapUtility.ColorMode.Green);
byte[,] blueArray = MyBitmapUtility.BitmapToByteArray(srcBitmap, MyBitmapUtility.ColorMode.Blue);
MyBitmapUtility.NeighberSharpen(ref redArray, width, height);
MyBitmapUtility.NeighberSharpen(ref greenArray, width, height);
MyBitmapUtility.NeighberSharpen(ref blueArray, width, height);
return MyBitmapUtility.ArrayToColorBitmap(redArray, greenArray, blueArray, width, height);
}
public static Bitmap SurfaceConcave(Bitmap srcBitmap)
{
int width = srcBitmap.Width;
int height = srcBitmap.Height;
byte[,] redArray = MyBitmapUtility.BitmapToByteArray(srcBitmap, MyBitmapUtility.ColorMode.Red);
byte[,] greenArray = MyBitmapUtility.BitmapToByteArray(srcBitmap, MyBitmapUtility.ColorMode.Green);
byte[,] blueArray = MyBitmapUtility.BitmapToByteArray(srcBitmap, MyBitmapUtility.ColorMode.Blue);
MyBitmapUtility.SurfaceConcave(ref redArray, width, height);
MyBitmapUtility.SurfaceConcave(ref greenArray, width, height);
MyBitmapUtility.SurfaceConcave(ref blueArray, width, height);
return MyBitmapUtility.ArrayToColorBitmap(redArray, greenArray, blueArray, width, height);
}
public static Bitmap LaplacianSharpen(Bitmap srcBitmap)
{
int width = srcBitmap.Width;
int height = srcBitmap.Height;
byte[,] redArray = MyBitmapUtility.BitmapToByteArray(srcBitmap, MyBitmapUtility.ColorMode.Red);
byte[,] greenArray = MyBitmapUtility.BitmapToByteArray(srcBitmap, MyBitmapUtility.ColorMode.Green);
byte[,] blueArray = MyBitmapUtility.BitmapToByteArray(srcBitmap, MyBitmapUtility.ColorMode.Blue);
MyBitmapUtility.LaplacianSharpen(ref redArray, width, height);
MyBitmapUtility.LaplacianSharpen(ref greenArray, width, height);
MyBitmapUtility.LaplacianSharpen(ref blueArray, width, height);
return MyBitmapUtility.ArrayToColorBitmap(redArray, greenArray, blueArray, width, height);
}
public static void NeighberAverSmooth(ref byte[,] imgData, int width, int height)
{
for (int i = 1; i < height - 1; i++)
{
for (int j = 1; j < width - 1; j++)
{
double num = 0.0;
for (int k = -1; k <= 1; k++)
{
for (int l = -1; l <= 1; l++)
{
num += (double)imgData[i + k, j + l];
}
}
num /= 9.0;
if (num < 0.0)
{
imgData[i, j] = 0;
}
else
{
if (num > 255.0)
{
imgData[i, j] = 255;
}
else
{
imgData[i, j] = (byte)(num + 0.5);
}
}
}
}
}
public static void NeighberMedSmooth(ref byte[,] imgData, int width, int height)
{
byte[] array = new byte[9];
for (int i = 1; i < height - 1; i++)
{
for (int j = 1; j < width - 1; j++)
{
int num = 0;
for (int k = -1; k <= 1; k++)
{
for (int l = -1; l <= 1; l++)
{
array[num++] = imgData[i + k, j + l];
}
}
imgData[j, i] = MyBitmapUtility.GetMedian(array, 9);
}
}
}
public static byte GetMedian(byte[] values, int count)
{
int num = count / 2;
byte b = values[0];
for (int i = 0; i < count; i++)
{
b = values[i];
int num2 = 0;
for (int j = 0; j < count; j++)
{
if (j != i)
{
if (b < values[j])
{
num2++;
}
if (num2 > num)
{
break;
}
}
}
if (num2 == num)
{
break;
}
}
return b;
}
public static void NeighberSharpen(ref byte[,] imgData, int width, int height)
{
for (int i = 1; i < height - 1; i++)
{
for (int j = 1; j < width - 1; j++)
{
double num = (double)imgData[i, j];
double num2 = (double)imgData[i - 1, j - 1];
double num3 = num + Math.Abs(num - num2) / 4.0;
if (num3 < 0.0)
{
imgData[i, j] = 0;
}
else
{
if (num3 > 255.0)
{
imgData[i, j] = 255;
}
else
{
imgData[i, j] = (byte)(num3 + 0.5);
}
}
}
}
}
public static void SurfaceConcave(ref byte[,] imgData, int width, int height)
{
for (int i = 1; i < height - 1; i++)
{
for (int j = 1; j < width - 1; j++)
{
int num = (int)imgData[i, j];
int num2 = (int)imgData[i + 1, j + 1];
int num3 = num2 - num + 128;
if (num3 < 0)
{
imgData[i, j] = 0;
}
else
{
if (num3 > 255)
{
imgData[i, j] = 255;
}
else
{
imgData[i, j] = (byte)num3;
}
}
}
}
}
public static void LaplacianSharpen(ref byte[,] imgData, int width, int height)
{
int[] array = new int[9];
for (int i = 0; i < 9; i++)
{
array[i] = -1;
}
array[4] = 9;
for (int i = 1; i < height - 1; i++)
{
for (int j = 1; j < width - 1; j++)
{
double num = 0.0;
int num2 = 0;
for (int k = -1; k <= 1; k++)
{
for (int l = -1; l <= 1; l++)
{
num += (double)((int)imgData[i + k, j + l] * array[num2++]);
}
}
if (num < 0.0)
{
imgData[i, j] = 0;
}
else
{
if (num > 255.0)
{
imgData[i, j] = 255;
}
else
{
imgData[i, j] = (byte)(num + 0.5);
}
}
}
}
}
public static uint invBit(uint src, uint n)
{
uint num = (src >> 1 & 1431655765u) | (src & 1431655765u) << 1;
num = ((num >> 2 & 858993459u) | (num & 858993459u) << 2);
num = ((num >> 4 & 252645135u) | (num & 252645135u) << 4);
num = ((num >> 8 & 16711935u) | (num & 16711935u) << 8);
num = (num >> 16 | num << 16);
return num >> (int)(32u - n);
}
public static uint pow2(uint mi)
{
switch (mi)
{
case 0u:
return 1u;
case 1u:
return 2u;
case 2u:
return 4u;
case 3u:
return 8u;
case 4u:
return 16u;
case 5u:
return 32u;
case 6u:
return 64u;
case 7u:
return 128u;
case 8u:
return 256u;
case 9u:
return 512u;
case 10u:
return 1024u;
case 11u:
return 2048u;
case 12u:
return 4096u;
default:
return 1u;
}
}
public static void SINandCOS(ref double[] fftCOS, ref double[] fftSIN, uint baseN)
{
int num = 0;
while ((long)num < (long)((ulong)(baseN / 2u)))
{
fftCOS[num] = Math.Cos(6.2831853071795862 * (double)num / baseN);
fftSIN[num] = Math.Sin(6.2831853071795862 * (double)num / baseN);
num++;
}
}
public static void fft(ref MyBitmapUtility.Complex[] freqData, uint N, uint sumL, double[] fftCOS, double[] fftSIN, uint baseN, bool inv = false)
{
MyBitmapUtility.Complex[] array = new MyBitmapUtility.Complex[N];
for (uint num = 0u; num < N; num += 1u)
{
uint num2 = MyBitmapUtility.invBit(num, sumL);
array[(int)((UIntPtr)num)] = freqData[(int)((UIntPtr)num2)];
}
for (uint num3 = 1u; num3 <= sumL; num3 += 1u)
{
uint num4 = MyBitmapUtility.pow2(num3);
uint num5 = MyBitmapUtility.pow2(num3 - 1u);
uint num6 = num5;
uint num7 = N >> (int)num3;
uint num8 = num4;
uint num9 = num4 - 1u;
int num10 = inv ? -1 : 1;
for (uint num11 = 0u; num11 < num5; num11 += 1u)
{
MyBitmapUtility.Complex comp = new MyBitmapUtility.Complex(fftCOS[(int)((UIntPtr)(baseN / num4 * num11))], (double)num10 * fftSIN[(int)((UIntPtr)(baseN / num4 * num11))]);
uint num12 = num9 - (num5 - num11 - 1u);
for (uint num = 0u; num < num7; num += 1u)
{
array[(int)((UIntPtr)num12)] = array[(int)((UIntPtr)num12)] * comp;
uint num13 = num12 - num6;
MyBitmapUtility.Complex comp2 = array[(int)((UIntPtr)num13)];
array[(int)((UIntPtr)num13)] = comp2 + array[(int)((UIntPtr)num12)];
array[(int)((UIntPtr)num12)] = comp2 - array[(int)((UIntPtr)num12)];
num12 += num8;
}
}
}
freqData = (MyBitmapUtility.Complex[])array.Clone();
}
public static void FreqFilterILHP(ref MyBitmapUtility.Complex[,] freqData, int fdWidth, int fdHeight, int imgWidth, int imgHeight, double fStop, bool bLowPass)
{
double num = fStop / (double)imgHeight * (double)fdHeight;
double num2 = num / (double)fdHeight * (double)fdWidth / (double)imgWidth * (double)fdWidth;
for (int i = 0; i < fdHeight; i++)
{
for (int j = 0; j < fdWidth; j++)
{
int num3;
if (bLowPass)
{
num3 = (((double)((i - fdHeight / 2) * (i - fdHeight / 2)) / (num * num) + (double)((j - fdWidth / 2) * (j - fdWidth / 2)) / (num2 * num2) > 1.0) ? 0 : 1);
}
else
{
num3 = (((double)((i - fdHeight / 2) * (i - fdHeight / 2)) / (num * num) + (double)((j - fdWidth / 2) * (j - fdWidth / 2)) / (num2 * num2) > 1.0) ? 1 : 0);
}
freqData[i, j] = (double)num3 * freqData[i, j];
}
}
}
public static void FreqFilterBLHP(ref MyBitmapUtility.Complex[,] freqData, int fdWidth, int fdHeight, double fStop, int fn, bool bLowPass)
{
if (fStop <= 0.0)
{
fStop = 1.0;
}
double num = (double)(fdWidth / 2);
double num2 = (double)(fdHeight / 2);
for (int i = 0; i < fdHeight; i++)
{
for (int j = 0; j < fdWidth; j++)
{
double num3 = Math.Sqrt(Math.Pow((double)i - num2, 2.0) + Math.Pow((double)j - num, 2.0));
double factor;
if (bLowPass)
{
factor = 1.0 / (1.0 + Math.Pow(num3 / fStop, (double)(2 * fn)));
}
else
{
factor = 1.0 / (1.0 + Math.Pow(fStop / num3, (double)(2 * fn)));
}
freqData[i, j] = factor * freqData[i, j];
}
}
}
public static void FreqFilterGLHP(ref MyBitmapUtility.Complex[,] freqData, int fdWidth, int fdHeight, double fStop, bool bLowPass)
{
if (fStop <= 0.0)
{
fStop = 1.0;
}
double num = (double)(fdWidth / 2);
double num2 = (double)(fdHeight / 2);
for (int i = 0; i < fdHeight; i++)
{
for (int j = 0; j < fdWidth; j++)
{
double x = Math.Sqrt(Math.Pow((double)i - num2, 2.0) + Math.Pow((double)j - num, 2.0));
double factor;
if (bLowPass)
{
factor = Math.Exp(-Math.Pow(x, 2.0) / (2.0 * Math.Pow(fStop, 2.0)));
}
else
{
factor = 1.0 - Math.Exp(-Math.Pow(x, 2.0) / (2.0 * Math.Pow(fStop, 2.0)));
}
freqData[i, j] = factor * freqData[i, j];
}
}
}
public static void FreqFilterELHP(ref MyBitmapUtility.Complex[,] freqData, int fdWidth, int fdHeight, double fStop, int fn, bool bLowPass)
{
if (fStop <= 0.0)
{
fStop = 1.0;
}
double num = (double)(fdWidth / 2);
double num2 = (double)(fdHeight / 2);
for (int i = 0; i < fdHeight; i++)
{
for (int j = 0; j < fdWidth; j++)
{
double num3 = Math.Sqrt(Math.Pow((double)i - num2, 2.0) + Math.Pow((double)j - num, 2.0));
double factor;
if (bLowPass)
{
factor = Math.Exp(-Math.Pow(num3 / fStop, (double)fn));
}
else
{
factor = Math.Exp(-Math.Pow(fStop / num3, (double)fn));
}
freqData[i, j] = factor * freqData[i, j];
}
}
}
public static MyBitmapUtility.Complex[,] ImageFFT(byte[,] imgData, int imgWidth, int imgHeight)
{
uint num = 0u;
uint num2 = 1u;
while ((ulong)num2 < (ulong)((long)imgHeight))
{
num += 1u;
num2 += num2;
}
uint num3 = 0u;
uint num4 = 1u;
while ((ulong)num4 < (ulong)((long)imgWidth))
{
num3 += 1u;
num4 += num4;
}
MyBitmapUtility.Complex[,] array = new MyBitmapUtility.Complex[(int)((UIntPtr)num2), (int)((UIntPtr)num4)];
for (int i = 0; i < imgHeight; i++)
{
for (int j = 0; j < imgWidth; j++)
{
array[i, j] = new MyBitmapUtility.Complex((double)((((i + j) % 2 != 0) ? -1 : 1) * (int)imgData[i, j]), 0.0);
}
}
uint num5 = (num2 > num4) ? num2 : num4;
double[] fftCOS = new double[num5 / 2u];
double[] fftSIN = new double[num5 / 2u];
MyBitmapUtility.SINandCOS(ref fftCOS, ref fftSIN, num5);
MyBitmapUtility.Complex[] array2 = new MyBitmapUtility.Complex[num2];
int num6 = 0;
while ((long)num6 < (long)((ulong)num2))
{
array2[num6] = new MyBitmapUtility.Complex(0.0, 0.0);
num6++;
}
int num7 = 0;
while ((long)num7 < (long)((ulong)num4))
{
int num8 = 0;
while ((long)num8 < (long)((ulong)num2))
{
array2[num8] = array[num8, num7];
num8++;
}
MyBitmapUtility.fft(ref array2, num2, num, fftCOS, fftSIN, num5, false);
int num9 = 0;
while ((long)num9 < (long)((ulong)num2))
{
array[num9, num7] = array2[num9];
num9++;
}
num7++;
}
int num10 = 0;
while ((long)num10 < (long)((ulong)num2))
{
MyBitmapUtility.Complex[] array3 = new MyBitmapUtility.Complex[num4];
int num11 = 0;
while ((long)num11 < (long)((ulong)num4))
{
array3[num11] = array[num10, num11];
num11++;
}
MyBitmapUtility.fft(ref array3, num4, num3, fftCOS, fftSIN, num5, false);
for (uint num12 = 0u; num12 < num4; num12 += 1u)
{
checked(array[(int)((IntPtr)unchecked((long)num10)), (int)((IntPtr)unchecked((ulong)num12))]) = array3[(int)((UIntPtr)num12)];
}
num10++;
}
return array;
}
public static void IFFT(ref MyBitmapUtility.Complex[,] ImageRI, int imgWidth, int imgHeight)
{
uint num = 0u;
uint num2 = 1u;
while ((ulong)num2 < (ulong)((long)imgHeight))
{
num += 1u;
num2 += num2;
}
uint num3 = 0u;
uint num4 = 1u;
while ((ulong)num4 < (ulong)((long)imgWidth))
{
num3 += 1u;
num4 += num4;
}
uint num5 = (num2 > num4) ? num2 : num4;
double[] fftCOS = new double[num5 / 2u];
double[] fftSIN = new double[num5 / 2u];
MyBitmapUtility.SINandCOS(ref fftCOS, ref fftSIN, num5);
MyBitmapUtility.Complex[] array = new MyBitmapUtility.Complex[num2];
int num6 = 0;
while ((long)num6 < (long)((ulong)num2))
{
array[num6] = new MyBitmapUtility.Complex(0.0, 0.0);
num6++;
}
int num7 = 0;
while ((long)num7 < (long)((ulong)num4))
{
int num8 = 0;
while ((long)num8 < (long)((ulong)num2))
{
array[num8] = ImageRI[num8, num7];
num8++;
}
MyBitmapUtility.fft(ref array, num2, num, fftCOS, fftSIN, num5, true);
int num9 = 0;
while ((long)num9 < (long)((ulong)num2))
{
ImageRI[num9, num7] = array[num9];
num9++;
}
num7++;
}
int num10 = 0;
while ((long)num10 < (long)((ulong)num2))
{
MyBitmapUtility.Complex[] array2 = new MyBitmapUtility.Complex[num4];
int num11 = 0;
while ((long)num11 < (long)((ulong)num4))
{
array2[num11] = ImageRI[num10, num11];
num11++;
}
MyBitmapUtility.fft(ref array2, num4, num3, fftCOS, fftSIN, num5, true);
for (uint num12 = 0u; num12 < num4; num12 += 1u)
{
checked(ImageRI[(int)((IntPtr)unchecked((long)num10)), (int)((IntPtr)unchecked((ulong)num12))]) = array2[(int)((UIntPtr)num12)];
}
num10++;
}
}
public static byte[,] ComplexToByteArray(MyBitmapUtility.Complex[,] freqData, int width, int height, double sup = 1.7976931348623157E+308)
{
byte[,] array = new byte[height, width];
double num = -1.7976931348623157E+308;
double num2 = 1.7976931348623157E+308;
double num3;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
num3 = freqData[i, j].Real;
if (num3 < 0.0)
{
num3 = -num3;
}
if (num3 > sup)
{
num3 = sup;
}
if (num3 > num)
{
num = num3;
}
if (num3 < num2)
{
num2 = num3;
}
freqData[i, j].Real = num3;
}
}
num3 = 255.0 / (num - num2);
for (int k = 0; k < height; k++)
{
for (int l = 0; l < width; l++)
{
array[k, l] = (byte)(num3 * freqData[k, l].Real + 0.5);
}
}
return array;
}
public static Bitmap FastFourierTransform(Bitmap srcBitmap, double sup)
{
int width = srcBitmap.Width;
int height = srcBitmap.Height;
Bitmap srcBitmap2 = MyBitmapUtility.ConvertBitmapToGray(srcBitmap, MyBitmapUtility.GrayMethod.Weighted);
byte[,] imgData = MyBitmapUtility.BitmapToByteArray(srcBitmap2, MyBitmapUtility.ColorMode.Blue);
MyBitmapUtility.Complex[,] freqData = MyBitmapUtility.ImageFFT(imgData, width, height);
byte[,] srcArray = MyBitmapUtility.ComplexToByteArray(freqData, width, height, sup);
return MyBitmapUtility.ArrayToGrayBitmap(srcArray, width, height);
}
public static Bitmap FreqFilterIdeal(Bitmap srcBitmap, double stopFreq, bool bLowpass = true)
{
int width = srcBitmap.Width;
int height = srcBitmap.Height;
Bitmap srcBitmap2 = MyBitmapUtility.ConvertBitmapToGray(srcBitmap, MyBitmapUtility.GrayMethod.Weighted);
byte[,] imgData = MyBitmapUtility.BitmapToByteArray(srcBitmap2, MyBitmapUtility.ColorMode.Blue);
MyBitmapUtility.Complex[,] freqData = MyBitmapUtility.ImageFFT(imgData, width, height);
MyBitmapUtility.FreqFilterILHP(ref freqData, width, height, width, height, stopFreq, bLowpass);
MyBitmapUtility.IFFT(ref freqData, width, height);
byte[,] srcArray = MyBitmapUtility.ComplexToByteArray(freqData, width, height, 1.7976931348623157E+308);
return MyBitmapUtility.ArrayToGrayBitmap(srcArray, width, height);
}
public static Bitmap FreqFilterGauss(Bitmap srcBitmap, double stopFreq, bool bLowPass = true)
{
int width = srcBitmap.Width;
int height = srcBitmap.Height;
Bitmap srcBitmap2 = MyBitmapUtility.ConvertBitmapToGray(srcBitmap, MyBitmapUtility.GrayMethod.Weighted);
byte[,] imgData = MyBitmapUtility.BitmapToByteArray(srcBitmap2, MyBitmapUtility.ColorMode.Blue);
MyBitmapUtility.Complex[,] freqData = MyBitmapUtility.ImageFFT(imgData, width, height);
MyBitmapUtility.FreqFilterGLHP(ref freqData, width, height, stopFreq, bLowPass);
MyBitmapUtility.IFFT(ref freqData, width, height);
byte[,] srcArray = MyBitmapUtility.ComplexToByteArray(freqData, width, height, 1.7976931348623157E+308);
return MyBitmapUtility.ArrayToGrayBitmap(srcArray, width, height);
}
public static Bitmap FreqFilterButterworth(Bitmap srcBitmap, double stopFreq, int order = 1, bool bLowPass = true)
{
int width = srcBitmap.Width;
int height = srcBitmap.Height;
Bitmap srcBitmap2 = MyBitmapUtility.ConvertBitmapToGray(srcBitmap, MyBitmapUtility.GrayMethod.Weighted);
byte[,] imgData = MyBitmapUtility.BitmapToByteArray(srcBitmap2, MyBitmapUtility.ColorMode.Blue);
MyBitmapUtility.Complex[,] freqData = MyBitmapUtility.ImageFFT(imgData, width, height);
MyBitmapUtility.FreqFilterBLHP(ref freqData, width, height, stopFreq, order, bLowPass);
MyBitmapUtility.IFFT(ref freqData, width, height);
byte[,] srcArray = MyBitmapUtility.ComplexToByteArray(freqData, width, height, 1.7976931348623157E+308);
return MyBitmapUtility.ArrayToGrayBitmap(srcArray, width, height);
}
public static Bitmap FreqFilterExpoly(Bitmap srcBitmap, double stopFreq, int order = 1, bool bLowPass = true)
{
int width = srcBitmap.Width;
int height = srcBitmap.Height;
Bitmap srcBitmap2 = MyBitmapUtility.ConvertBitmapToGray(srcBitmap, MyBitmapUtility.GrayMethod.Weighted);
byte[,] imgData = MyBitmapUtility.BitmapToByteArray(srcBitmap2, MyBitmapUtility.ColorMode.Blue);
MyBitmapUtility.Complex[,] freqData = MyBitmapUtility.ImageFFT(imgData, width, height);
MyBitmapUtility.FreqFilterELHP(ref freqData, width, height, stopFreq, order, bLowPass);
MyBitmapUtility.IFFT(ref freqData, width, height);
byte[,] srcArray = MyBitmapUtility.ComplexToByteArray(freqData, width, height, 1.7976931348623157E+308);
return MyBitmapUtility.ArrayToGrayBitmap(srcArray, width, height);
}
public static Bitmap FillAsSquare(Bitmap srcBitmap, int wh)
{
Bitmap bitmap = new Bitmap(wh, wh, PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.FillRectangle(Brushes.Black, new Rectangle(0, 0, wh, wh));
graphics.DrawImage(srcBitmap, new Point(0, 0));
graphics.Save();
graphics.Dispose();
srcBitmap.Dispose();
return bitmap;
}
}
}
转载于:https://www.cnblogs.com/fengyhack/p/10603805.html
最后
以上就是爱笑大叔为你收集整理的图像处理类(C#版本,新手,写的比较菜,仅供参考)的全部内容,希望文章能够帮你解决图像处理类(C#版本,新手,写的比较菜,仅供参考)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复