From 42dc1d36235292786322d28340a81c6cb3fd46c0 Mon Sep 17 00:00:00 2001 From: hurter Date: Wed, 31 Aug 2011 16:25:05 +0000 Subject: --- ImageProcessing/ColorTools.cs | 217 ++++++++ ImageProcessing/Filters.cs | 604 +++++++++++++++++++++ ImageProcessing/FormImageProcessing.Designer.cs | 235 ++++++++ ImageProcessing/FormImageProcessing.cs | 441 +++++++++++++++ ImageProcessing/FormImageProcessing.resx | 129 +++++ ImageProcessing/ImageProcessing.csproj | 90 +++ ImageProcessing/Program.cs | 21 + ImageProcessing/Properties/AssemblyInfo.cs | 36 ++ ImageProcessing/Properties/Resources.Designer.cs | 71 +++ ImageProcessing/Properties/Resources.resx | 117 ++++ ImageProcessing/Properties/Settings.Designer.cs | 30 + ImageProcessing/Properties/Settings.settings | 7 + ImageProcessing/bin/Debug/1.png | Bin 0 -> 377494 bytes ImageProcessing/bin/Debug/2.png | Bin 0 -> 413209 bytes ImageProcessing/bin/Debug/3.png | Bin 0 -> 296197 bytes ImageProcessing/bin/Debug/4.png | Bin 0 -> 122646 bytes ImageProcessing/bin/Debug/ImageProcessing.exe | Bin 0 -> 25088 bytes ImageProcessing/bin/Debug/ImageProcessing.pdb | Bin 0 -> 65024 bytes .../bin/Debug/ImageProcessing.vshost.exe | Bin 0 -> 11600 bytes .../bin/Debug/ImageProcessing.vshost.exe.manifest | 11 + ImageProcessing/bin/Debug/grid color.jpg | Bin 0 -> 193452 bytes ImageProcessing/bin/Debug/screenshoot1.Jpg | Bin 0 -> 29667 bytes ImageProcessing/bin/Debug/screenshoot2.Jpg | Bin 0 -> 2611 bytes ImageProcessing/bin/Debug/screenshoot3.Jpg | Bin 0 -> 43860 bytes ImageProcessing/bin/Debug/strip.png | Bin 0 -> 16807 bytes .../DesignTimeResolveAssemblyReferences.cache | Bin 0 -> 4501 bytes .../DesignTimeResolveAssemblyReferencesInput.cache | Bin 0 -> 6305 bytes .../obj/x86/Debug/GenerateResource.read.1.tlog | Bin 0 -> 518 bytes .../obj/x86/Debug/GenerateResource.write.1.tlog | Bin 0 -> 1298 bytes .../ImageProcessing.FormImageProcessing.resources | Bin 0 -> 180 bytes .../ImageProcessing.Properties.Resources.resources | Bin 0 -> 180 bytes .../ImageProcessing.csproj.FileListAbsolute.txt | 18 + ImageProcessing/obj/x86/Debug/ImageProcessing.exe | Bin 0 -> 25088 bytes ImageProcessing/obj/x86/Debug/ImageProcessing.pdb | Bin 0 -> 65024 bytes .../obj/x86/Debug/ResolveAssemblyReference.cache | Bin 0 -> 8031 bytes .../DesignTimeResolveAssemblyReferencesInput.cache | Bin 0 -> 6194 bytes .../obj/x86/Release/GenerateResource.read.1.tlog | Bin 0 -> 236 bytes .../obj/x86/Release/GenerateResource.write.1.tlog | Bin 0 -> 610 bytes .../ImageProcessing.FormImageProcessing.resources | Bin 0 -> 180 bytes .../ImageProcessing.Properties.Resources.resources | Bin 0 -> 180 bytes .../ImageProcessing.csproj.FileListAbsolute.txt | 5 + 41 files changed, 2032 insertions(+) create mode 100644 ImageProcessing/ColorTools.cs create mode 100644 ImageProcessing/Filters.cs create mode 100644 ImageProcessing/FormImageProcessing.Designer.cs create mode 100644 ImageProcessing/FormImageProcessing.cs create mode 100644 ImageProcessing/FormImageProcessing.resx create mode 100644 ImageProcessing/ImageProcessing.csproj create mode 100644 ImageProcessing/Program.cs create mode 100644 ImageProcessing/Properties/AssemblyInfo.cs create mode 100644 ImageProcessing/Properties/Resources.Designer.cs create mode 100644 ImageProcessing/Properties/Resources.resx create mode 100644 ImageProcessing/Properties/Settings.Designer.cs create mode 100644 ImageProcessing/Properties/Settings.settings create mode 100644 ImageProcessing/bin/Debug/1.png create mode 100644 ImageProcessing/bin/Debug/2.png create mode 100644 ImageProcessing/bin/Debug/3.png create mode 100644 ImageProcessing/bin/Debug/4.png create mode 100644 ImageProcessing/bin/Debug/ImageProcessing.exe create mode 100644 ImageProcessing/bin/Debug/ImageProcessing.pdb create mode 100644 ImageProcessing/bin/Debug/ImageProcessing.vshost.exe create mode 100644 ImageProcessing/bin/Debug/ImageProcessing.vshost.exe.manifest create mode 100644 ImageProcessing/bin/Debug/grid color.jpg create mode 100644 ImageProcessing/bin/Debug/screenshoot1.Jpg create mode 100644 ImageProcessing/bin/Debug/screenshoot2.Jpg create mode 100644 ImageProcessing/bin/Debug/screenshoot3.Jpg create mode 100644 ImageProcessing/bin/Debug/strip.png create mode 100644 ImageProcessing/obj/x86/Debug/DesignTimeResolveAssemblyReferences.cache create mode 100644 ImageProcessing/obj/x86/Debug/DesignTimeResolveAssemblyReferencesInput.cache create mode 100644 ImageProcessing/obj/x86/Debug/GenerateResource.read.1.tlog create mode 100644 ImageProcessing/obj/x86/Debug/GenerateResource.write.1.tlog create mode 100644 ImageProcessing/obj/x86/Debug/ImageProcessing.FormImageProcessing.resources create mode 100644 ImageProcessing/obj/x86/Debug/ImageProcessing.Properties.Resources.resources create mode 100644 ImageProcessing/obj/x86/Debug/ImageProcessing.csproj.FileListAbsolute.txt create mode 100644 ImageProcessing/obj/x86/Debug/ImageProcessing.exe create mode 100644 ImageProcessing/obj/x86/Debug/ImageProcessing.pdb create mode 100644 ImageProcessing/obj/x86/Debug/ResolveAssemblyReference.cache create mode 100644 ImageProcessing/obj/x86/Release/DesignTimeResolveAssemblyReferencesInput.cache create mode 100644 ImageProcessing/obj/x86/Release/GenerateResource.read.1.tlog create mode 100644 ImageProcessing/obj/x86/Release/GenerateResource.write.1.tlog create mode 100644 ImageProcessing/obj/x86/Release/ImageProcessing.FormImageProcessing.resources create mode 100644 ImageProcessing/obj/x86/Release/ImageProcessing.Properties.Resources.resources create mode 100644 ImageProcessing/obj/x86/Release/ImageProcessing.csproj.FileListAbsolute.txt (limited to 'ImageProcessing') diff --git a/ImageProcessing/ColorTools.cs b/ImageProcessing/ColorTools.cs new file mode 100644 index 0000000..5c625fd --- /dev/null +++ b/ImageProcessing/ColorTools.cs @@ -0,0 +1,217 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Drawing; + +namespace ImageProcessing +{ + public struct ColorRGB + { + public byte R; + public byte G; + public byte B; + + public ColorRGB(Color value) + { + this.R = value.R; + this.G = value.G; + this.B = value.B; + } + + public static implicit operator Color(ColorRGB rgb) + { + Color c = Color.FromArgb(rgb.R, rgb.G, rgb.B); + return c; + } + + public static explicit operator ColorRGB(Color c) + { + return new ColorRGB(c); + } + + } + + public struct ColorHSL + { + public double H; + public double S; + public double L; + + public ColorHSL(double h, double s, double l) + { + this.L = l; + this.S = s; + this.H = h; + } + } + + + public class ColorTools + { + + /// + /// Given H,S,L, alph in range of 0-1 + /// + /// + /// + /// + /// + /// Returns a Color (RGB struct) in range of 0-255 + public static Color HSL2RGB(double h, double s, double l, double alpha) + { + double v; + double r, g, b; + + r = l; // default to gray + g = l; + b = l; + + v = (l <= 0.5) ? (l * (1.0 + s)) : (l + s - l * s); + + if (v > 0) + { + double m; + double sv; + int sextant; + double fract, vsf, mid1, mid2; + + m = l + l - v; + sv = (v - m) / v; + h *= 6.0; + + sextant = (int)h; + fract = h - sextant; + vsf = v * sv * fract; + + mid1 = m + vsf; + mid2 = v - vsf; + switch (sextant) + { + + case 0: + r = v; + g = mid1; + b = m; + break; + + case 1: + r = mid2; + g = v; + b = m; + break; + + case 2: + r = m; + g = v; + b = mid1; + break; + + case 3: + r = m; + g = mid2; + b = v; + break; + + case 4: + r = mid1; + g = m; + b = v; + break; + + case 5: + r = v; + g = m; + b = mid2; + break; + case 6: + r = v; + g = mid1; + b = m; + break; + + } + + } + + + Byte R = Convert.ToByte(r * 255.0f); + Byte G = Convert.ToByte(g * 255.0f); + Byte B = Convert.ToByte(b * 255.0f); + Byte A = Convert.ToByte(alpha * 255.0f); + + return Color.FromArgb(A, R, G, B); + + } + + // Given a Color (RGB Struct) in range of 0-255 + + // Return H,S,L in range of 0-1 + public static ColorHSL RGB2HSL(byte r, byte g, byte b) + { + ColorRGB rgb = new ColorRGB(); + rgb.R = r; + rgb.G = g; + rgb.B = b; + return ColorTools.RGB2HSL(rgb); + } + public static ColorHSL RGB2HSL(ColorRGB rgb) + { + + double r = rgb.R / 255.0; + double g = rgb.G / 255.0; + double b = rgb.B / 255.0; + double v; + double m; + double vm; + double r2, g2, b2; + + double h = 0; // default to black + double s = 0; + double l = 0; + + v = Math.Max(r, g); + v = Math.Max(v, b); + m = Math.Min(r, g); + m = Math.Min(m, b); + l = (m + v) / 2.0; + if (l <= 0.0) + { + return new ColorHSL(h, s, l); + } + + vm = v - m; + s = vm; + if (s > 0.0) + { + s /= (l <= 0.5) ? (v + m) : (2.0 - v - m); + } + else + { + return new ColorHSL(h, s, l); + } + + r2 = (v - r) / vm; + g2 = (v - g) / vm; + b2 = (v - b) / vm; + if (r == v) + { + h = (g == m ? 5.0 + b2 : 1.0 - g2); + } + + else if (g == v) + { + h = (b == m ? 1.0 + r2 : 3.0 - b2); + } + else + { + h = (r == m ? 3.0 + g2 : 5.0 - r2); + } + h /= 6.0; + + return new ColorHSL(h, s, l); + + } + + } +} diff --git a/ImageProcessing/Filters.cs b/ImageProcessing/Filters.cs new file mode 100644 index 0000000..59c6cbd --- /dev/null +++ b/ImageProcessing/Filters.cs @@ -0,0 +1,604 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Drawing; +using System.Drawing.Imaging; + +namespace ImageProcessing +{ + public class BitmapFilter + { + static public float GetMedian( float[] array) + { + float[] tempArray = array; + int count = tempArray.Length; + + Array.Sort(tempArray); + + float medianValue = 0; + + if (count % 2 == 0) + { + // count is even, need to get the middle two elements, add them together, then divide by 2 + float middleElement1 = tempArray[(count / 2) - 1]; + float middleElement2 = tempArray[(count / 2)]; + medianValue = (middleElement1 + middleElement2) / 2; + } + else + { + // count is odd, simply get the middle element. + medianValue = tempArray[(count / 2)]; + } + + return medianValue; + } + static public float[] ComputeLocalMean(float[] gray, int width, int height) + { + int kernelWidth = 5; + + float[] tab = new float[width * height]; + for (int y = 0; y < height; y++) + { + for (int x = 0; x < width; x++) + { + tab[x + y * width] = ComputeMean(x, y, gray, width, height, kernelWidth); + } + } + return tab; + } + + private static float ComputeMean(int x, int y, float[] gray, int width, int height, int kernelWidth) + { + float mean = 0; + float count = 0; + for (int yy = Math.Max(0, y - kernelWidth); yy < Math.Min(y + kernelWidth, height); yy++) + { + for (int xx = Math.Max(0, x - kernelWidth); xx < Math.Min(x + kernelWidth, width); xx++) + { + mean += gray[xx + yy * width]; + count++; + } + } + mean /= count; + return mean; + } + + + static public float[] HorizontalThreshold(float[] gray, int width, int height) + { + float[] tab = new float[width * height]; + float[] line = new float[width]; + + for (int y = 0; y < height; y++) + { + for (int x = 0; x < width; x++) + { + line[x] = gray[x + y * width]; + } + float med = GetMedian(line); + for (int x = 0; x < width; x++) + { + float diff = gray[x + y * width] - med; + if (diff < 0) diff = 0; + else + diff = 1; + tab[x + y * width] = diff; + } + } + + return tab; + } + + static public bool Invert(Bitmap b) + { + // GDI+ still lies to us - the return format is BGR, NOT RGB. + BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); + + int stride = bmData.Stride; + System.IntPtr Scan0 = bmData.Scan0; + + unsafe + { + byte* p = (byte*)(void*)Scan0; + + int nOffset = stride - b.Width * 3; + int nWidth = b.Width * 3; + + for (int y = 0; y < b.Height; ++y) + { + for (int x = 0; x < nWidth; ++x) + { + p[0] = (byte)(255 - p[0]); + ++p; + } + p += nOffset; + } + } + + b.UnlockBits(bmData); + + return true; + } + + static public Bitmap GenerateBitmap(float[] gray, int width, int height) + { + Bitmap b = new Bitmap(width, height, PixelFormat.Format24bppRgb); + // GDI+ still lies to us - the return format is BGR, NOT RGB. + BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); + + int stride = bmData.Stride; + System.IntPtr Scan0 = bmData.Scan0; + + unsafe + { + byte* p = (byte*)(void*)Scan0; + + int nOffset = stride - b.Width * 3; + + for (int y = 0; y < b.Height; ++y) + { + for (int x = 0; x < b.Width; ++x) + { + p[0] = p[1] = p[2] = (byte)(gray[y * b.Width + x]*255); + + p += 3; + + } + p += nOffset; + + } + } + + b.UnlockBits(bmData); + + return b; + } + + static public float GenericScaleF(float input, float i1, float o1, float i2, float o2) + { + if (i2 == i1) return ((o2 + o1) / 2.0f); + float a = (o2 - o1) / (i2 - i1); + float b = o1 - a * i1; + return (a * input + b); + } + + static public ColorRGB[] ColorRGBBitmap(Bitmap b) + { + ColorRGB[] tab = new ColorRGB[b.Width * b.Height]; + + Bitmap des = new Bitmap(b); + // GDI+ still lies to us - the return format is BGR, NOT RGB. + BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); + + int stride = bmData.Stride; + System.IntPtr Scan0 = bmData.Scan0; + + unsafe + { + + byte* p = (byte*)(void*)Scan0; + + int nOffset = stride - b.Width * 3; + + for (int y = 0; y < b.Height; ++y) + { + for (int x = 0; x < b.Width; ++x) + { + tab[y * b.Width + x].R = p[2]; + tab[y * b.Width + x].G = p[1]; + tab[y * b.Width + x].B = p[0]; + + p += 3; + + } + p += nOffset; + + } + } + + b.UnlockBits(bmData); + + return tab; + } + + static public ColorHSL[] ColorLSHBitmap(Bitmap b) + { + ColorHSL[] tab = new ColorHSL[b.Width * b.Height]; + + Bitmap des = new Bitmap(b); + // GDI+ still lies to us - the return format is BGR, NOT RGB. + BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); + + int stride = bmData.Stride; + System.IntPtr Scan0 = bmData.Scan0; + + unsafe + { + + byte* p = (byte*)(void*)Scan0; + + int nOffset = stride - b.Width * 3; + + byte red, green, blue; + + for (int y = 0; y < b.Height; ++y) + { + for (int x = 0; x < b.Width; ++x) + { + blue = p[0]; + green = p[1]; + red = p[2]; + + + tab[y * b.Width + x] = ColorTools.RGB2HSL( red, green, blue); + + p += 3; + + } + p += nOffset; + + } + } + + b.UnlockBits(bmData); + + return tab; + } + + static public Bitmap GenerateBitmapFromHSL(ColorHSL[] col, int width, int height) + { + Bitmap b = new Bitmap(width, height, PixelFormat.Format24bppRgb); + // GDI+ still lies to us - the return format is BGR, NOT RGB. + BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); + + int stride = bmData.Stride; + System.IntPtr Scan0 = bmData.Scan0; + + unsafe + { + byte* p = (byte*)(void*)Scan0; + + int nOffset = stride - b.Width * 3; + + for (int y = 0; y < b.Height; ++y) + { + for (int x = 0; x < b.Width; ++x) + { + ColorHSL hsl = col[y * b.Width + x]; + Color c = ColorTools.HSL2RGB(hsl.H,hsl.S,hsl.L,1); + p[0] = c.B; + p[1] = c.G; + p[2] = c.R; + + p += 3; + + } + p += nOffset; + + } + } + + b.UnlockBits(bmData); + + return b; + } + + static public float[] GrayScaleFloat(Bitmap b) + { + float[] tab = new float[b.Width * b.Height]; + + Bitmap des = new Bitmap(b); + // GDI+ still lies to us - the return format is BGR, NOT RGB. + BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); + + int stride = bmData.Stride; + System.IntPtr Scan0 = bmData.Scan0; + + unsafe + { + + byte* p = (byte*)(void*)Scan0; + + int nOffset = stride - b.Width * 3; + + byte red, green, blue; + + for (int y = 0; y < b.Height; ++y) + { + for (int x = 0; x < b.Width; ++x) + { + blue = p[0]; + green = p[1]; + red = p[2]; + + tab[y *b.Width + x] = (float)(.299 * red + .587 * green + .114 * blue)/255.0f; + + p += 3; + + } + p += nOffset; + + } + } + + b.UnlockBits(bmData); + + return tab; + } + + static public Bitmap GrayScaleBitmap(Bitmap b) + { + Bitmap des = new Bitmap(b); + // GDI+ still lies to us - the return format is BGR, NOT RGB. + BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); + BitmapData bmDataDest = des.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); + + int stride = bmData.Stride; + System.IntPtr Scan0 = bmData.Scan0; + System.IntPtr Scan0Dest = bmDataDest.Scan0; + + unsafe + { + byte* p = (byte*)(void*)Scan0; + byte* pDest = (byte*)(void*)Scan0Dest; + + int nOffset = stride - b.Width * 3; + + byte red, green, blue; + + for (int y = 0; y < b.Height; ++y) + { + for (int x = 0; x < b.Width; ++x) + { + blue = p[0]; + green = p[1]; + red = p[2]; + + pDest[0] = pDest[1] = pDest[2] = (byte)(.299 * red + .587 * green + .114 * blue); + + p += 3; + pDest += 3; + } + p += nOffset; + pDest += nOffset; + } + } + + b.UnlockBits(bmData); + des.UnlockBits(bmDataDest); + + return des; + } + + + static public float[] Normalize(float[] grayf, out float min, out float max, out float mean) + { + mean = 0; + min = float.MaxValue; + max = 0; + float[] res = new float[grayf.Length]; + + for (int i = 0; i < grayf.Length; i++) + { + if (grayf[i] > max) max = grayf[i]; + if (grayf[i] < min) min = grayf[i]; + mean += grayf[i]; + } + mean /= grayf.Length; + + for (int i = 0; i < grayf.Length; i++) + { + res[i] = GenericScaleF( grayf[i], min,0,max,1); + } + + return res; + } + + static public bool Brightness(Bitmap b, int nBrightness) + { + if (nBrightness < -255 || nBrightness > 255) + return false; + + // GDI+ still lies to us - the return format is BGR, NOT RGB. + BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); + + int stride = bmData.Stride; + System.IntPtr Scan0 = bmData.Scan0; + + int nVal = 0; + + unsafe + { + byte* p = (byte*)(void*)Scan0; + + int nOffset = stride - b.Width * 3; + int nWidth = b.Width * 3; + + for (int y = 0; y < b.Height; ++y) + { + for (int x = 0; x < nWidth; ++x) + { + nVal = (int)(p[0] + nBrightness); + + if (nVal < 0) nVal = 0; + if (nVal > 255) nVal = 255; + + p[0] = (byte)nVal; + + ++p; + } + p += nOffset; + } + } + + b.UnlockBits(bmData); + + return true; + } + + static public bool Contrast(Bitmap b, sbyte nContrast) + { + if (nContrast < -100) return false; + if (nContrast > 100) return false; + + double pixel = 0, contrast = (100.0 + nContrast) / 100.0; + + contrast *= contrast; + + int red, green, blue; + + // GDI+ still lies to us - the return format is BGR, NOT RGB. + BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); + + int stride = bmData.Stride; + System.IntPtr Scan0 = bmData.Scan0; + + unsafe + { + byte* p = (byte*)(void*)Scan0; + + int nOffset = stride - b.Width * 3; + + for (int y = 0; y < b.Height; ++y) + { + for (int x = 0; x < b.Width; ++x) + { + blue = p[0]; + green = p[1]; + red = p[2]; + + pixel = red / 255.0; + pixel -= 0.5; + pixel *= contrast; + pixel += 0.5; + pixel *= 255; + if (pixel < 0) pixel = 0; + if (pixel > 255) pixel = 255; + p[2] = (byte)pixel; + + pixel = green / 255.0; + pixel -= 0.5; + pixel *= contrast; + pixel += 0.5; + pixel *= 255; + if (pixel < 0) pixel = 0; + if (pixel > 255) pixel = 255; + p[1] = (byte)pixel; + + pixel = blue / 255.0; + pixel -= 0.5; + pixel *= contrast; + pixel += 0.5; + pixel *= 255; + if (pixel < 0) pixel = 0; + if (pixel > 255) pixel = 255; + p[0] = (byte)pixel; + + p += 3; + } + p += nOffset; + } + } + + b.UnlockBits(bmData); + + return true; + } + + static public bool Gamma(Bitmap b, double red, double green, double blue) + { + if (red < .2 || red > 5) return false; + if (green < .2 || green > 5) return false; + if (blue < .2 || blue > 5) return false; + + byte[] redGamma = new byte[256]; + byte[] greenGamma = new byte[256]; + byte[] blueGamma = new byte[256]; + + for (int i = 0; i < 256; ++i) + { + redGamma[i] = (byte)Math.Min(255, (int)((255.0 * Math.Pow(i / 255.0, 1.0 / red)) + 0.5)); + greenGamma[i] = (byte)Math.Min(255, (int)((255.0 * Math.Pow(i / 255.0, 1.0 / green)) + 0.5)); + blueGamma[i] = (byte)Math.Min(255, (int)((255.0 * Math.Pow(i / 255.0, 1.0 / blue)) + 0.5)); + } + + // GDI+ still lies to us - the return format is BGR, NOT RGB. + BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); + + int stride = bmData.Stride; + System.IntPtr Scan0 = bmData.Scan0; + + unsafe + { + byte* p = (byte*)(void*)Scan0; + + int nOffset = stride - b.Width * 3; + + for (int y = 0; y < b.Height; ++y) + { + for (int x = 0; x < b.Width; ++x) + { + p[2] = redGamma[p[2]]; + p[1] = greenGamma[p[1]]; + p[0] = blueGamma[p[0]]; + + p += 3; + } + p += nOffset; + } + } + + b.UnlockBits(bmData); + + return true; + } + + static public bool Color(Bitmap b, int red, int green, int blue) + { + if (red < -255 || red > 255) return false; + if (green < -255 || green > 255) return false; + if (blue < -255 || blue > 255) return false; + + // GDI+ still lies to us - the return format is BGR, NOT RGB. + BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); + + int stride = bmData.Stride; + System.IntPtr Scan0 = bmData.Scan0; + + unsafe + { + byte* p = (byte*)(void*)Scan0; + + int nOffset = stride - b.Width * 3; + int nPixel; + + for (int y = 0; y < b.Height; ++y) + { + for (int x = 0; x < b.Width; ++x) + { + nPixel = p[2] + red; + nPixel = Math.Max(nPixel, 0); + p[2] = (byte)Math.Min(255, nPixel); + + nPixel = p[1] + green; + nPixel = Math.Max(nPixel, 0); + p[1] = (byte)Math.Min(255, nPixel); + + nPixel = p[0] + blue; + nPixel = Math.Max(nPixel, 0); + p[0] = (byte)Math.Min(255, nPixel); + + p += 3; + } + p += nOffset; + } + } + + b.UnlockBits(bmData); + + return true; + } + } + +} diff --git a/ImageProcessing/FormImageProcessing.Designer.cs b/ImageProcessing/FormImageProcessing.Designer.cs new file mode 100644 index 0000000..4d42a26 --- /dev/null +++ b/ImageProcessing/FormImageProcessing.Designer.cs @@ -0,0 +1,235 @@ +namespace ImageProcessing +{ + partial class FormImageProcessing + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.pictureBox1 = new System.Windows.Forms.PictureBox(); + this.menuStrip1 = new System.Windows.Forms.MenuStrip(); + this.showToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.orinigalToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.normalizedToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.horizontalThresholdToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.localMeanToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.diffToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.medianOnMaskToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.maskedInfoWithMedianToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.openFileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.loadGrayScaleToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.lodColorToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); + this.statusStrip1 = new System.Windows.Forms.StatusStrip(); + this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel(); + this.trackBar1 = new System.Windows.Forms.TrackBar(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); + this.menuStrip1.SuspendLayout(); + this.statusStrip1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit(); + this.SuspendLayout(); + // + // pictureBox1 + // + this.pictureBox1.BackColor = System.Drawing.SystemColors.ActiveCaption; + this.pictureBox1.Location = new System.Drawing.Point(0, 53); + this.pictureBox1.Name = "pictureBox1"; + this.pictureBox1.Size = new System.Drawing.Size(691, 474); + this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; + this.pictureBox1.TabIndex = 0; + this.pictureBox1.TabStop = false; + // + // menuStrip1 + // + this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.showToolStripMenuItem, + this.openFileToolStripMenuItem}); + this.menuStrip1.Location = new System.Drawing.Point(0, 0); + this.menuStrip1.Name = "menuStrip1"; + this.menuStrip1.Size = new System.Drawing.Size(691, 24); + this.menuStrip1.TabIndex = 1; + this.menuStrip1.Text = "menuStrip1"; + // + // showToolStripMenuItem + // + this.showToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.orinigalToolStripMenuItem, + this.normalizedToolStripMenuItem, + this.horizontalThresholdToolStripMenuItem, + this.localMeanToolStripMenuItem, + this.diffToolStripMenuItem, + this.medianOnMaskToolStripMenuItem, + this.maskedInfoWithMedianToolStripMenuItem}); + this.showToolStripMenuItem.Name = "showToolStripMenuItem"; + this.showToolStripMenuItem.Size = new System.Drawing.Size(48, 20); + this.showToolStripMenuItem.Text = "Show"; + // + // orinigalToolStripMenuItem + // + this.orinigalToolStripMenuItem.Name = "orinigalToolStripMenuItem"; + this.orinigalToolStripMenuItem.Size = new System.Drawing.Size(201, 22); + this.orinigalToolStripMenuItem.Text = "Orinigal"; + this.orinigalToolStripMenuItem.Click += new System.EventHandler(this.orinigalToolStripMenuItem_Click); + // + // normalizedToolStripMenuItem + // + this.normalizedToolStripMenuItem.Name = "normalizedToolStripMenuItem"; + this.normalizedToolStripMenuItem.Size = new System.Drawing.Size(201, 22); + this.normalizedToolStripMenuItem.Text = "Normalized"; + this.normalizedToolStripMenuItem.Click += new System.EventHandler(this.normalizedToolStripMenuItem_Click); + // + // horizontalThresholdToolStripMenuItem + // + this.horizontalThresholdToolStripMenuItem.Name = "horizontalThresholdToolStripMenuItem"; + this.horizontalThresholdToolStripMenuItem.Size = new System.Drawing.Size(201, 22); + this.horizontalThresholdToolStripMenuItem.Text = "HorizontalThreshold"; + this.horizontalThresholdToolStripMenuItem.Click += new System.EventHandler(this.horizontalThresholdToolStripMenuItem_Click); + // + // localMeanToolStripMenuItem + // + this.localMeanToolStripMenuItem.Name = "localMeanToolStripMenuItem"; + this.localMeanToolStripMenuItem.Size = new System.Drawing.Size(201, 22); + this.localMeanToolStripMenuItem.Text = "LocalMean"; + this.localMeanToolStripMenuItem.Click += new System.EventHandler(this.localMeanToolStripMenuItem_Click); + // + // diffToolStripMenuItem + // + this.diffToolStripMenuItem.Name = "diffToolStripMenuItem"; + this.diffToolStripMenuItem.Size = new System.Drawing.Size(201, 22); + this.diffToolStripMenuItem.Text = "Diff"; + this.diffToolStripMenuItem.Click += new System.EventHandler(this.diffToolStripMenuItem_Click); + // + // medianOnMaskToolStripMenuItem + // + this.medianOnMaskToolStripMenuItem.Name = "medianOnMaskToolStripMenuItem"; + this.medianOnMaskToolStripMenuItem.Size = new System.Drawing.Size(201, 22); + this.medianOnMaskToolStripMenuItem.Text = "MAskedInfo"; + this.medianOnMaskToolStripMenuItem.Click += new System.EventHandler(this.medianOnMaskToolStripMenuItem_Click); + // + // maskedInfoWithMedianToolStripMenuItem + // + this.maskedInfoWithMedianToolStripMenuItem.Name = "maskedInfoWithMedianToolStripMenuItem"; + this.maskedInfoWithMedianToolStripMenuItem.Size = new System.Drawing.Size(201, 22); + this.maskedInfoWithMedianToolStripMenuItem.Text = "MaskedInfoWithMedian"; + this.maskedInfoWithMedianToolStripMenuItem.Click += new System.EventHandler(this.maskedInfoWithMedianToolStripMenuItem_Click); + // + // openFileToolStripMenuItem + // + this.openFileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.loadGrayScaleToolStripMenuItem, + this.lodColorToolStripMenuItem}); + this.openFileToolStripMenuItem.Name = "openFileToolStripMenuItem"; + this.openFileToolStripMenuItem.Size = new System.Drawing.Size(66, 20); + this.openFileToolStripMenuItem.Text = "OpenFile"; + // + // loadGrayScaleToolStripMenuItem + // + this.loadGrayScaleToolStripMenuItem.Name = "loadGrayScaleToolStripMenuItem"; + this.loadGrayScaleToolStripMenuItem.Size = new System.Drawing.Size(157, 22); + this.loadGrayScaleToolStripMenuItem.Text = "Load Gray Scale"; + this.loadGrayScaleToolStripMenuItem.Click += new System.EventHandler(this.openFileToolStripMenuItem_Click); + // + // lodColorToolStripMenuItem + // + this.lodColorToolStripMenuItem.Name = "lodColorToolStripMenuItem"; + this.lodColorToolStripMenuItem.Size = new System.Drawing.Size(157, 22); + this.lodColorToolStripMenuItem.Text = "Lod Color"; + this.lodColorToolStripMenuItem.Click += new System.EventHandler(this.lodColorToolStripMenuItem_Click); + // + // openFileDialog1 + // + this.openFileDialog1.FileName = "openFileDialog1"; + // + // statusStrip1 + // + this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripStatusLabel1}); + this.statusStrip1.Location = new System.Drawing.Point(0, 476); + this.statusStrip1.Name = "statusStrip1"; + this.statusStrip1.Size = new System.Drawing.Size(691, 22); + this.statusStrip1.TabIndex = 2; + this.statusStrip1.Text = "statusStrip1"; + // + // toolStripStatusLabel1 + // + this.toolStripStatusLabel1.Name = "toolStripStatusLabel1"; + this.toolStripStatusLabel1.Size = new System.Drawing.Size(118, 17); + this.toolStripStatusLabel1.Text = "toolStripStatusLabel1"; + // + // trackBar1 + // + this.trackBar1.Location = new System.Drawing.Point(526, 12); + this.trackBar1.Maximum = 100; + this.trackBar1.Name = "trackBar1"; + this.trackBar1.Size = new System.Drawing.Size(104, 45); + this.trackBar1.TabIndex = 3; + this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll); + // + // FormImageProcessing + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(691, 498); + this.Controls.Add(this.trackBar1); + this.Controls.Add(this.statusStrip1); + this.Controls.Add(this.pictureBox1); + this.Controls.Add(this.menuStrip1); + this.MainMenuStrip = this.menuStrip1; + this.Name = "FormImageProcessing"; + this.Text = "Form1"; + this.Load += new System.EventHandler(this.Form1_Load); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); + this.menuStrip1.ResumeLayout(false); + this.menuStrip1.PerformLayout(); + this.statusStrip1.ResumeLayout(false); + this.statusStrip1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.PictureBox pictureBox1; + private System.Windows.Forms.MenuStrip menuStrip1; + private System.Windows.Forms.ToolStripMenuItem showToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem orinigalToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem normalizedToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem horizontalThresholdToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem localMeanToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem diffToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem medianOnMaskToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem openFileToolStripMenuItem; + private System.Windows.Forms.OpenFileDialog openFileDialog1; + private System.Windows.Forms.StatusStrip statusStrip1; + private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1; + private System.Windows.Forms.TrackBar trackBar1; + private System.Windows.Forms.ToolStripMenuItem maskedInfoWithMedianToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem loadGrayScaleToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem lodColorToolStripMenuItem; + } +} + diff --git a/ImageProcessing/FormImageProcessing.cs b/ImageProcessing/FormImageProcessing.cs new file mode 100644 index 0000000..dc62bfe --- /dev/null +++ b/ImageProcessing/FormImageProcessing.cs @@ -0,0 +1,441 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Windows.Forms; + +namespace ImageProcessing +{ + public partial class FormImageProcessing : Form + { + // string ImageFile = @"1.png"; + string ImageFile = @"strip.png"; + + Bitmap Bmp; + float[] Original; + float[] Normalized; + float[] HorizontalThresholdF; + float[] LocalMeanF; + float[] Diff; + float[] MaskedInfo; + float[] MaskedInfoWithMedian; + + ColorHSL[] OriginalHSL; + + ColorRGB[] OriginalRGB; + + public FormImageProcessing() + { + InitializeComponent(); + } + + + private void ComputeRGBFilter(string p) + { + Bmp = (Bitmap)Bitmap.FromFile(p); + + OriginalRGB = BitmapFilter.ColorRGBBitmap(Bmp); + + float[] threasholdImg = new float[OriginalRGB.Length]; + // chroma histogram 10 steps + // find the 2 max + //if the 2 max> 1 the threashold = min (2 max) + (max2 - max1)/2 + + //Horizontal line + for (int y = 0; y < Bmp.Height; y++) + { + int lenght = Bmp.Width; + for (int x = 0; x < lenght; x++) + { + ColorRGB pix = OriginalRGB[x + y * Bmp.Width]; + + threasholdImg[x + y * Bmp.Width] = 0.50f; //no relevant histogram + + if ((pix.R > 125) && (pix.G < 125) && (pix.B < 125)) + threasholdImg[x + y * Bmp.Width] = 0.00f; //no relevant histogram + if ((pix.R < 125) && (pix.G > 125) && (pix.B < 125)) + threasholdImg[x + y * Bmp.Width] = 1.00f; //no relevant histogram + + } + + + + } + + + // pictureBox1.Image = BitmapFilter.GenerateBitmapFromHSL(OriginalHSL, Bmp.Width, Bmp.Height); + pictureBox1.Image = BitmapFilter.GenerateBitmap(threasholdImg, Bmp.Width, Bmp.Height); + + } + + private void ComputeColorFilter(string p) + { + Bmp = (Bitmap)Bitmap.FromFile(p); + + OriginalHSL = BitmapFilter.ColorLSHBitmap(Bmp); + + float[] threasholdImg = new float[OriginalHSL.Length]; + // chroma histogram 10 steps + // find the 2 max + //if the 2 max> 1 the threashold = min (2 max) + (max2 - max1)/2 + + //Horizontal line + for (int y = 0; y < Bmp.Height; y++) + { + //Find min max mean median + int steps = 10; + int[] chromaHisto = new int[steps + 1]; + int lenght = Bmp.Width; + for (int x = 0; x < lenght; x++) + { + ColorHSL pix = OriginalHSL[x + y * Bmp.Width]; + int histIndexChroma = (int)(pix.H * steps); + chromaHisto[histIndexChroma]++; + } + + //Find the two max + int max1 = -1; + int chroma1 = -1; + int max2 = -1; + int chroma2 = -1; + + //Find max1 + for (int i = 0; i < chromaHisto.Length; i++) + { + if ((chromaHisto[i] > max1) && (chromaHisto[i] != 0)) + { + max1 = chromaHisto[i]; + chroma1 = i; + } + } + //find max2 + for (int i = 0; i < chromaHisto.Length; i++) + { + //circular distance + // int dist = Math.Abs(i - chroma1); + // if (Math.Min(dist, Math.Abs(dist - steps)) > (steps / 3)) //At least delta chroma > steps / 3 + if (CircularDistance(i, chroma1, steps) > (steps / 3)) + if ((chromaHisto[i] > max2) && (chromaHisto[i] != 0)) + { + max2 = chromaHisto[i]; + chroma2 = i; + } + } + + + int peak = chroma1; + if ((max1 != -1) && (max2 != -1) && (chromaHisto[chroma2] > chromaHisto[chroma1])) peak = chroma2; + double delta = 3; + + Console.WriteLine(chroma1 + " " + chroma2); + + //binaries + for (int x = 0; x < Bmp.Width; x++) + { + if ((max1 != -1) && (max2 != -1)) //found a max1 and max 2 ? + { + ColorHSL pix = OriginalHSL[x + y * Bmp.Width]; + int histIndexChroma = (int)(pix.H * steps); + + if (CircularDistance(histIndexChroma, peak, steps) < delta) + threasholdImg[x + y * Bmp.Width] = 1; //Black + else + threasholdImg[x + y * Bmp.Width] = 0; //White + } + else + threasholdImg[x + y * Bmp.Width] = 0.50f; //no relevant histogram + + } + } + + // pictureBox1.Image = BitmapFilter.GenerateBitmapFromHSL(OriginalHSL, Bmp.Width, Bmp.Height); + pictureBox1.Image = BitmapFilter.GenerateBitmap(threasholdImg, Bmp.Width, Bmp.Height); + + } + + int CircularDistance(int index1, int index2, int steps) + { //circular distance + int dist = Math.Abs(index1 - index2); + return Math.Min(dist, Math.Abs(dist - steps)); + } + + + private void ComputeFilter(string p) + { + Bmp = (Bitmap)Bitmap.FromFile(p); + + Original = BitmapFilter.GrayScaleFloat(Bmp); + float min = 0, max = 0, mean = 0; + + Normalized = BitmapFilter.Normalize(Original, out min, out max, out mean); + + Diff = new float[Original.Length]; + float[] imput = Original; + //Horizontal line normalization + for (int y = 0; y < Bmp.Height; y++) + { + //Find min max mean median + float minL = float.MaxValue, maxL = 0, meanL = 0, median = 0; + List line = new List(); + int lenght = Bmp.Width; + for (int x = 0; x < lenght; x++) + { + float pix = imput[x + y * Bmp.Width]; + if (pix < minL) minL = pix; + if (pix > maxL) maxL = pix; + line.Add(pix); + meanL += pix; + } + line.Sort(); + median = line[line.Count / 2]; + meanL /= (float)lenght; + + //Scale and binaries + for (int x = 0; x < Bmp.Width; x++) + { + float pix = imput[x + y * Bmp.Width]; + + Diff[x + y * Bmp.Width] = Original[x + y * Bmp.Width] - minL + meanL; + if (Diff[x + y * Bmp.Width] > 1) Diff[x + y * Bmp.Width] = 1; + if (Diff[x + y * Bmp.Width] < 0) Diff[x + y * Bmp.Width] = 0; + } + } + + //Vertical line normalization + imput = Diff; + for (int x = 0; x < Bmp.Width; x++) + { + //Find min max mean median + float minL = float.MaxValue, maxL = 0, meanL = 0, median = 0; + List line = new List(); + int lenght = Bmp.Height; + for (int y = 0; y < lenght; y++) + { + float pix = imput[x + y * Bmp.Width]; + if (pix < minL) minL = pix; + if (pix > maxL) maxL = pix; + line.Add(pix); + meanL += pix; + } + line.Sort(); + median = line[line.Count / 2]; + meanL /= (float)lenght; + + //Scale and binaries + for (int y = 0; y < lenght; y++) + { + float pix = imput[x + y * Bmp.Width]; + + Diff[x + y * Bmp.Width] = Diff[x + y * Bmp.Width] - minL + meanL; + if (Diff[x + y * Bmp.Width] > 1) Diff[x + y * Bmp.Width] = 1; + if (Diff[x + y * Bmp.Width] < 0) Diff[x + y * Bmp.Width] = 0; + } + } + + + /* for (int x = 0; x < Bmp.Width; x++) + { + float minL = float.MaxValue, maxL = 0, meanL = 0, median = 0; + List line = new List(); + int lenght = Bmp.Height; + for (int y = 0; y < Bmp.Height; y++) + { + float pix = Diff[x + y * Bmp.Width]; + if (pix < minL) minL = pix; + if (pix > maxL) maxL = pix; + line.Add(pix); + meanL += pix; + } + line.Sort(); + median = line[line.Count / 2]; + meanL /= (float)lenght; + + //Scale and binaries + for (int y = 0; y < lenght; y++) + { + + Diff[x + y * Bmp.Width] = BitmapFilter.GenericScaleF(Diff[x + y * Bmp.Width], minL, 0, maxL, 1); + } + } + */ + + /* //Compute the mean and min of each horizontal line + for (int y = 0; y < Bmp.Height; y++) + { + float minL = float.MaxValue, meanL = 0; + for (int x = 0; x < Bmp.Width; x++) + { + float pix = Original[x+y*Bmp.Width]; + if (pix < minL) minL = pix; + meanL += pix; + } + meanL /= (float)Bmp.Width; + for (int x = 0; x < Bmp.Width; x++) + { + Diff[x + y * Bmp.Width] = Original[x + y * Bmp.Width] - minL + meanL; + if (Diff[x + y * Bmp.Width] < 0) Diff[x + y * Bmp.Width] = 0; + if (Diff[x + y * Bmp.Width] > 1) Diff[x + y * Bmp.Width] = 1; + } + } + + //Compute the mean and min of each horizontal line + for (int x = 0; x < Bmp.Width; x++) + { + float minL = float.MaxValue, meanL = 0, median = 0; + List lst = new List(); + for (int y = 0; y < Bmp.Height; y++) + { + float pix = Diff[x + y * Bmp.Width]; + if (pix < minL) minL = pix; + lst.Add(pix); + meanL += pix; + } + meanL /= (float)Bmp.Height; + lst.Sort(); + median = lst[lst.Count / 2]; + + for (int y = 0; y < Bmp.Height; y++) + { + Diff[x + y * Bmp.Width] = Diff[x + y * Bmp.Width] - minL + meanL; + if (Diff[x + y * Bmp.Width] < 0) Diff[x + y * Bmp.Width] = 0; + if (Diff[x + y * Bmp.Width] > 1) Diff[x + y * Bmp.Width] = 1; + } + } + Diff = BitmapFilter.Normalize(Diff, out min, out max, out mean); + */ + + // HorizontalThresholdF = BitmapFilter.HorizontalThreshold(Normalized, Bmp.Width, Bmp.Height); + // LocalMeanF = BitmapFilter.ComputeLocalMean(Normalized, Bmp.Width, Bmp.Height); + + /* Diff = new float[Original.Length]; + for (int i = 0; i < Original.Length; i++) + { + Diff[i] = Normalized[i] - LocalMeanF[i]; + if (Diff[i] < 0) Diff[i] = 0; + else Diff[i] = 1; + + }*/ + + //Compute the median Threshold only on the mask (HorizontalThresholdF) + /* MaskedInfo = new float[Original.Length]; + List lst = new List(); + for (int i = 0; i < Original.Length; i++) + { + if (HorizontalThresholdF[i] == 0) + { + MaskedInfo[i] = Normalized[i]; + lst.Add(Normalized[i]); + } + else + MaskedInfo[i] = 1; + } + + lst.Sort(); + float median = lst[lst.Count / 2]; + + SUperCompute(median); + */ + + + } + + private void SUperCompute(float median) + { + toolStripStatusLabel1.Text = median.ToString("0.000"); + MaskedInfoWithMedian = new float[Original.Length]; + + for (int i = 0; i < Original.Length; i++) + { + if (MaskedInfo[i] > median) MaskedInfoWithMedian[i] = 1.0f; + else + MaskedInfoWithMedian[i] = 0.0f; + + } + + + } + + private void Form1_Load(object sender, EventArgs e) + { + ComputeFilter(ImageFile); + + pictureBox1.Image = BitmapFilter.GenerateBitmap(Diff, Bmp.Width, Bmp.Height); + pictureBox1.Invalidate(); + + } + + private void orinigalToolStripMenuItem_Click(object sender, EventArgs e) + { + pictureBox1.Image = BitmapFilter.GenerateBitmap(Original, Bmp.Width, Bmp.Height); + pictureBox1.Invalidate(); + } + + private void normalizedToolStripMenuItem_Click(object sender, EventArgs e) + { + pictureBox1.Image = BitmapFilter.GenerateBitmap(Normalized, Bmp.Width, Bmp.Height); + pictureBox1.Invalidate(); + + } + + private void horizontalThresholdToolStripMenuItem_Click(object sender, EventArgs e) + { + pictureBox1.Image = BitmapFilter.GenerateBitmap(HorizontalThresholdF, Bmp.Width, Bmp.Height); + pictureBox1.Invalidate(); + + } + + private void localMeanToolStripMenuItem_Click(object sender, EventArgs e) + { + pictureBox1.Image = BitmapFilter.GenerateBitmap(LocalMeanF, Bmp.Width, Bmp.Height); + pictureBox1.Invalidate(); + } + + private void diffToolStripMenuItem_Click(object sender, EventArgs e) + { + pictureBox1.Image = BitmapFilter.GenerateBitmap(Diff, Bmp.Width, Bmp.Height); + pictureBox1.Invalidate(); + + } + + private void medianOnMaskToolStripMenuItem_Click(object sender, EventArgs e) + { + pictureBox1.Image = BitmapFilter.GenerateBitmap(MaskedInfo, Bmp.Width, Bmp.Height); + pictureBox1.Invalidate(); + + } + + private void openFileToolStripMenuItem_Click(object sender, EventArgs e) + { + + } + + private void trackBar1_Scroll(object sender, EventArgs e) + { + float median = (float)trackBar1.Value / (float)trackBar1.Maximum; + SUperCompute(median); + } + + private void maskedInfoWithMedianToolStripMenuItem_Click(object sender, EventArgs e) + { + pictureBox1.Image = BitmapFilter.GenerateBitmap(MaskedInfoWithMedian, Bmp.Width, Bmp.Height); + pictureBox1.Invalidate(); + + } + + private void lodColorToolStripMenuItem_Click(object sender, EventArgs e) + { + if (openFileDialog1.ShowDialog() == DialogResult.OK) + { + //ComputeColorFilter(openFileDialog1.FileName); + ComputeRGBFilter(openFileDialog1.FileName); + } + } + + + + + } +} diff --git a/ImageProcessing/FormImageProcessing.resx b/ImageProcessing/FormImageProcessing.resx new file mode 100644 index 0000000..c68d348 --- /dev/null +++ b/ImageProcessing/FormImageProcessing.resx @@ -0,0 +1,129 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + + 132, 17 + + + 272, 17 + + \ No newline at end of file diff --git a/ImageProcessing/ImageProcessing.csproj b/ImageProcessing/ImageProcessing.csproj new file mode 100644 index 0000000..af9797c --- /dev/null +++ b/ImageProcessing/ImageProcessing.csproj @@ -0,0 +1,90 @@ + + + + Debug + x86 + 8.0.30703 + 2.0 + {525828F6-94FC-4D3B-8A96-C0AFF306F081} + WinExe + Properties + ImageProcessing + ImageProcessing + v4.0 + Client + 512 + + + x86 + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + true + + + x86 + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + Form + + + FormImageProcessing.cs + + + + + FormImageProcessing.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + \ No newline at end of file diff --git a/ImageProcessing/Program.cs b/ImageProcessing/Program.cs new file mode 100644 index 0000000..afd6001 --- /dev/null +++ b/ImageProcessing/Program.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Windows.Forms; + +namespace ImageProcessing +{ + static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new FormImageProcessing()); + } + } +} diff --git a/ImageProcessing/Properties/AssemblyInfo.cs b/ImageProcessing/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..c7db90f --- /dev/null +++ b/ImageProcessing/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("ImageProcessing")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("ImageProcessing")] +[assembly: AssemblyCopyright("Copyright © Microsoft 2011")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("dc072d3d-4f78-49db-8d07-b832a77dd820")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ImageProcessing/Properties/Resources.Designer.cs b/ImageProcessing/Properties/Resources.Designer.cs new file mode 100644 index 0000000..55384e4 --- /dev/null +++ b/ImageProcessing/Properties/Resources.Designer.cs @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.235 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace ImageProcessing.Properties +{ + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ImageProcessing.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git a/ImageProcessing/Properties/Resources.resx b/ImageProcessing/Properties/Resources.resx new file mode 100644 index 0000000..ffecec8 --- /dev/null +++ b/ImageProcessing/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ImageProcessing/Properties/Settings.Designer.cs b/ImageProcessing/Properties/Settings.Designer.cs new file mode 100644 index 0000000..278b796 --- /dev/null +++ b/ImageProcessing/Properties/Settings.Designer.cs @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.235 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace ImageProcessing.Properties +{ + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git a/ImageProcessing/Properties/Settings.settings b/ImageProcessing/Properties/Settings.settings new file mode 100644 index 0000000..abf36c5 --- /dev/null +++ b/ImageProcessing/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + diff --git a/ImageProcessing/bin/Debug/1.png b/ImageProcessing/bin/Debug/1.png new file mode 100644 index 0000000..075350f Binary files /dev/null and b/ImageProcessing/bin/Debug/1.png differ diff --git a/ImageProcessing/bin/Debug/2.png b/ImageProcessing/bin/Debug/2.png new file mode 100644 index 0000000..ceecd1a Binary files /dev/null and b/ImageProcessing/bin/Debug/2.png differ diff --git a/ImageProcessing/bin/Debug/3.png b/ImageProcessing/bin/Debug/3.png new file mode 100644 index 0000000..7d83356 Binary files /dev/null and b/ImageProcessing/bin/Debug/3.png differ diff --git a/ImageProcessing/bin/Debug/4.png b/ImageProcessing/bin/Debug/4.png new file mode 100644 index 0000000..63c5525 Binary files /dev/null and b/ImageProcessing/bin/Debug/4.png differ diff --git a/ImageProcessing/bin/Debug/ImageProcessing.exe b/ImageProcessing/bin/Debug/ImageProcessing.exe new file mode 100644 index 0000000..ed8bfb1 Binary files /dev/null and b/ImageProcessing/bin/Debug/ImageProcessing.exe differ diff --git a/ImageProcessing/bin/Debug/ImageProcessing.pdb b/ImageProcessing/bin/Debug/ImageProcessing.pdb new file mode 100644 index 0000000..068643e Binary files /dev/null and b/ImageProcessing/bin/Debug/ImageProcessing.pdb differ diff --git a/ImageProcessing/bin/Debug/ImageProcessing.vshost.exe b/ImageProcessing/bin/Debug/ImageProcessing.vshost.exe new file mode 100644 index 0000000..bb84a51 Binary files /dev/null and b/ImageProcessing/bin/Debug/ImageProcessing.vshost.exe differ diff --git a/ImageProcessing/bin/Debug/ImageProcessing.vshost.exe.manifest b/ImageProcessing/bin/Debug/ImageProcessing.vshost.exe.manifest new file mode 100644 index 0000000..f96b1d6 --- /dev/null +++ b/ImageProcessing/bin/Debug/ImageProcessing.vshost.exe.manifest @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/ImageProcessing/bin/Debug/grid color.jpg b/ImageProcessing/bin/Debug/grid color.jpg new file mode 100644 index 0000000..9491570 Binary files /dev/null and b/ImageProcessing/bin/Debug/grid color.jpg differ diff --git a/ImageProcessing/bin/Debug/screenshoot1.Jpg b/ImageProcessing/bin/Debug/screenshoot1.Jpg new file mode 100644 index 0000000..bc4690a Binary files /dev/null and b/ImageProcessing/bin/Debug/screenshoot1.Jpg differ diff --git a/ImageProcessing/bin/Debug/screenshoot2.Jpg b/ImageProcessing/bin/Debug/screenshoot2.Jpg new file mode 100644 index 0000000..aa59407 Binary files /dev/null and b/ImageProcessing/bin/Debug/screenshoot2.Jpg differ diff --git a/ImageProcessing/bin/Debug/screenshoot3.Jpg b/ImageProcessing/bin/Debug/screenshoot3.Jpg new file mode 100644 index 0000000..c8b6061 Binary files /dev/null and b/ImageProcessing/bin/Debug/screenshoot3.Jpg differ diff --git a/ImageProcessing/bin/Debug/strip.png b/ImageProcessing/bin/Debug/strip.png new file mode 100644 index 0000000..71ef4cc Binary files /dev/null and b/ImageProcessing/bin/Debug/strip.png differ diff --git a/ImageProcessing/obj/x86/Debug/DesignTimeResolveAssemblyReferences.cache b/ImageProcessing/obj/x86/Debug/DesignTimeResolveAssemblyReferences.cache new file mode 100644 index 0000000..b1d289a Binary files /dev/null and b/ImageProcessing/obj/x86/Debug/DesignTimeResolveAssemblyReferences.cache differ diff --git a/ImageProcessing/obj/x86/Debug/DesignTimeResolveAssemblyReferencesInput.cache b/ImageProcessing/obj/x86/Debug/DesignTimeResolveAssemblyReferencesInput.cache new file mode 100644 index 0000000..a5c76bd Binary files /dev/null and b/ImageProcessing/obj/x86/Debug/DesignTimeResolveAssemblyReferencesInput.cache differ diff --git a/ImageProcessing/obj/x86/Debug/GenerateResource.read.1.tlog b/ImageProcessing/obj/x86/Debug/GenerateResource.read.1.tlog new file mode 100644 index 0000000..8d8f0c3 Binary files /dev/null and b/ImageProcessing/obj/x86/Debug/GenerateResource.read.1.tlog differ diff --git a/ImageProcessing/obj/x86/Debug/GenerateResource.write.1.tlog b/ImageProcessing/obj/x86/Debug/GenerateResource.write.1.tlog new file mode 100644 index 0000000..8637e0c Binary files /dev/null and b/ImageProcessing/obj/x86/Debug/GenerateResource.write.1.tlog differ diff --git a/ImageProcessing/obj/x86/Debug/ImageProcessing.FormImageProcessing.resources b/ImageProcessing/obj/x86/Debug/ImageProcessing.FormImageProcessing.resources new file mode 100644 index 0000000..6c05a97 Binary files /dev/null and b/ImageProcessing/obj/x86/Debug/ImageProcessing.FormImageProcessing.resources differ diff --git a/ImageProcessing/obj/x86/Debug/ImageProcessing.Properties.Resources.resources b/ImageProcessing/obj/x86/Debug/ImageProcessing.Properties.Resources.resources new file mode 100644 index 0000000..6c05a97 Binary files /dev/null and b/ImageProcessing/obj/x86/Debug/ImageProcessing.Properties.Resources.resources differ diff --git a/ImageProcessing/obj/x86/Debug/ImageProcessing.csproj.FileListAbsolute.txt b/ImageProcessing/obj/x86/Debug/ImageProcessing.csproj.FileListAbsolute.txt new file mode 100644 index 0000000..7783500 --- /dev/null +++ b/ImageProcessing/obj/x86/Debug/ImageProcessing.csproj.FileListAbsolute.txt @@ -0,0 +1,18 @@ +E:\SaveData\Projects\Anoto\ImageProcessing\obj\x86\Debug\ResolveAssemblyReference.cache +E:\SaveData\Projects\Anoto\ImageProcessing\obj\x86\Debug\ImageProcessing.Properties.Resources.resources +E:\SaveData\Projects\Anoto\ImageProcessing\obj\x86\Debug\GenerateResource.read.1.tlog +E:\SaveData\Projects\Anoto\ImageProcessing\obj\x86\Debug\GenerateResource.write.1.tlog +E:\SaveData\Projects\Anoto\ImageProcessing\bin\Debug\ImageProcessing.exe +E:\SaveData\Projects\Anoto\ImageProcessing\bin\Debug\ImageProcessing.pdb +E:\SaveData\Projects\Anoto\ImageProcessing\obj\x86\Debug\ImageProcessing.exe +E:\SaveData\Projects\Anoto\ImageProcessing\obj\x86\Debug\ImageProcessing.pdb +E:\SaveData\Projects\Anoto\ImageProcessing\obj\x86\Debug\ImageProcessing.FormImageProcessing.resources +C:\Anoto\Anoto\ImageProcessing\obj\x86\Debug\ImageProcessing.exe +C:\Anoto\Anoto\ImageProcessing\obj\x86\Debug\ImageProcessing.pdb +C:\Anoto\Anoto\ImageProcessing\bin\Debug\ImageProcessing.exe +C:\Anoto\Anoto\ImageProcessing\bin\Debug\ImageProcessing.pdb +C:\Anoto\Anoto\ImageProcessing\obj\x86\Debug\ResolveAssemblyReference.cache +C:\Anoto\Anoto\ImageProcessing\obj\x86\Debug\ImageProcessing.FormImageProcessing.resources +C:\Anoto\Anoto\ImageProcessing\obj\x86\Debug\ImageProcessing.Properties.Resources.resources +C:\Anoto\Anoto\ImageProcessing\obj\x86\Debug\GenerateResource.read.1.tlog +C:\Anoto\Anoto\ImageProcessing\obj\x86\Debug\GenerateResource.write.1.tlog diff --git a/ImageProcessing/obj/x86/Debug/ImageProcessing.exe b/ImageProcessing/obj/x86/Debug/ImageProcessing.exe new file mode 100644 index 0000000..ed8bfb1 Binary files /dev/null and b/ImageProcessing/obj/x86/Debug/ImageProcessing.exe differ diff --git a/ImageProcessing/obj/x86/Debug/ImageProcessing.pdb b/ImageProcessing/obj/x86/Debug/ImageProcessing.pdb new file mode 100644 index 0000000..068643e Binary files /dev/null and b/ImageProcessing/obj/x86/Debug/ImageProcessing.pdb differ diff --git a/ImageProcessing/obj/x86/Debug/ResolveAssemblyReference.cache b/ImageProcessing/obj/x86/Debug/ResolveAssemblyReference.cache new file mode 100644 index 0000000..9df83bb Binary files /dev/null and b/ImageProcessing/obj/x86/Debug/ResolveAssemblyReference.cache differ diff --git a/ImageProcessing/obj/x86/Release/DesignTimeResolveAssemblyReferencesInput.cache b/ImageProcessing/obj/x86/Release/DesignTimeResolveAssemblyReferencesInput.cache new file mode 100644 index 0000000..c2d8521 Binary files /dev/null and b/ImageProcessing/obj/x86/Release/DesignTimeResolveAssemblyReferencesInput.cache differ diff --git a/ImageProcessing/obj/x86/Release/GenerateResource.read.1.tlog b/ImageProcessing/obj/x86/Release/GenerateResource.read.1.tlog new file mode 100644 index 0000000..b456c2d Binary files /dev/null and b/ImageProcessing/obj/x86/Release/GenerateResource.read.1.tlog differ diff --git a/ImageProcessing/obj/x86/Release/GenerateResource.write.1.tlog b/ImageProcessing/obj/x86/Release/GenerateResource.write.1.tlog new file mode 100644 index 0000000..9a3c77d Binary files /dev/null and b/ImageProcessing/obj/x86/Release/GenerateResource.write.1.tlog differ diff --git a/ImageProcessing/obj/x86/Release/ImageProcessing.FormImageProcessing.resources b/ImageProcessing/obj/x86/Release/ImageProcessing.FormImageProcessing.resources new file mode 100644 index 0000000..6c05a97 Binary files /dev/null and b/ImageProcessing/obj/x86/Release/ImageProcessing.FormImageProcessing.resources differ diff --git a/ImageProcessing/obj/x86/Release/ImageProcessing.Properties.Resources.resources b/ImageProcessing/obj/x86/Release/ImageProcessing.Properties.Resources.resources new file mode 100644 index 0000000..6c05a97 Binary files /dev/null and b/ImageProcessing/obj/x86/Release/ImageProcessing.Properties.Resources.resources differ diff --git a/ImageProcessing/obj/x86/Release/ImageProcessing.csproj.FileListAbsolute.txt b/ImageProcessing/obj/x86/Release/ImageProcessing.csproj.FileListAbsolute.txt new file mode 100644 index 0000000..9ef693a --- /dev/null +++ b/ImageProcessing/obj/x86/Release/ImageProcessing.csproj.FileListAbsolute.txt @@ -0,0 +1,5 @@ +C:\Anoto\Anoto\ImageProcessing\obj\x86\Release\ResolveAssemblyReference.cache +C:\Anoto\Anoto\ImageProcessing\obj\x86\Release\ImageProcessing.FormImageProcessing.resources +C:\Anoto\Anoto\ImageProcessing\obj\x86\Release\ImageProcessing.Properties.Resources.resources +C:\Anoto\Anoto\ImageProcessing\obj\x86\Release\GenerateResource.read.1.tlog +C:\Anoto\Anoto\ImageProcessing\obj\x86\Release\GenerateResource.write.1.tlog -- cgit v1.1