diff --git a/DIgitalImageProcessingImageSharp/DIgitalImageProcessingImageSharp.csproj b/DIgitalImageProcessingImageSharp/DIgitalImageProcessingImageSharp.csproj new file mode 100644 index 0000000..8a89192 --- /dev/null +++ b/DIgitalImageProcessingImageSharp/DIgitalImageProcessingImageSharp.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + diff --git a/DIgitalImageProcessingImageSharp/Program.cs b/DIgitalImageProcessingImageSharp/Program.cs new file mode 100644 index 0000000..6d79857 --- /dev/null +++ b/DIgitalImageProcessingImageSharp/Program.cs @@ -0,0 +1,40 @@ +using OpenCvSharp; +using System.Xml.Serialization; + +//string filepath = (@"C:\Users\Surface\Downloads\" + @"1682575881230001.png"); + +int year = 2025; +bool isLeap1 = DateTime.IsLeapYear(year); +bool isLeap2 = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); +bool isLeap3 = year switch +{ + _ when year % 400 == 0 => true, + _ when year % 100 == 0 => false, + _ when year % 4 == 0 => true, + _ => false, +}; +bool isLeap4 = year % 4 == 0 ? (year % 100 == 0 ? (year % 400 == 0 ? true : false) : true) : false; +bool isLeap5 = year % 4 != 0 ? false : year % 100 != 0 ? true : year % 400 == 0 ? true : false; + +Console.WriteLine(isLeap1); +Console.WriteLine(isLeap2); +Console.WriteLine(isLeap3); +Console.WriteLine(isLeap4); +Console.WriteLine(isLeap5); + +string filepath = (@"C:\Users\Surface\Downloads\" + @"sprite-0001.png"); + +Mat image = Cv2.ImRead(filepath); +InputArray kernel = InputArray.Create(new int[,] { + { 0, 1, 0 }, + { 1, 1, 1 }, + { 0, 1, 0 } +}, MatType.CV_8U); +Mat dst = new(); +//Cv2.Filter2D(image, dst, image.Depth(), kernel); + +Cv2.Dilate(image, dst, kernel); + +dst = dst - image; + +dst.ImWrite(@"C:\Users\Surface\Downloads\" + @"filtered_opencv.png"); \ No newline at end of file diff --git a/DigitalImageProcessing.sln b/DigitalImageProcessing.sln index 81bf366..021e669 100644 --- a/DigitalImageProcessing.sln +++ b/DigitalImageProcessing.sln @@ -1,10 +1,12 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 -VisualStudioVersion = 17.14.36408.4 d17.14 +VisualStudioVersion = 17.14.36408.4 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalImageProcessing", "DigitalImageProcessing\DigitalImageProcessing.csproj", "{CC16DC64-4AF5-4AA5-83CF-EC6C2C137450}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DIgitalImageProcessingImageSharp", "DIgitalImageProcessingImageSharp\DIgitalImageProcessingImageSharp.csproj", "{D33ECA25-E9B7-4426-B722-C193BC9943A1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {CC16DC64-4AF5-4AA5-83CF-EC6C2C137450}.Debug|Any CPU.Build.0 = Debug|Any CPU {CC16DC64-4AF5-4AA5-83CF-EC6C2C137450}.Release|Any CPU.ActiveCfg = Release|Any CPU {CC16DC64-4AF5-4AA5-83CF-EC6C2C137450}.Release|Any CPU.Build.0 = Release|Any CPU + {D33ECA25-E9B7-4426-B722-C193BC9943A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D33ECA25-E9B7-4426-B722-C193BC9943A1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D33ECA25-E9B7-4426-B722-C193BC9943A1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D33ECA25-E9B7-4426-B722-C193BC9943A1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/DigitalImageProcessing/Extensions.cs b/DigitalImageProcessing/Extensions.cs index d7bc4c2..fc52566 100644 --- a/DigitalImageProcessing/Extensions.cs +++ b/DigitalImageProcessing/Extensions.cs @@ -71,23 +71,23 @@ namespace DigitalImageProcessing float r = color.Red / 255f; float g = color.Green / 255f; float b = color.Blue / 255f; - float min = MathF.Min(r, MathF.Min(g, b)); - i = (int)(((r + g + b) / 3f) * 255); - if (i == 0) + float min = MathF.Min(r, MathF.Min(g, b)); // 最低分量 + i = (int)(((r + g + b) / 3f) * 255); // 平均分量 + if (i == 0) // 黑色 { h = 0; s = 0; return; } - s = (int)((1 - (min / (i / 255f))) * 255); - if (s == 0) + s = (int)((1 - (min / (i / 255f))) * 255); // 亮度 + if (s == 0) // 黑色*2 { h = 0; return; } - float numerator = 0.5f * ((r - g) + (r - b)); - float denominator = MathF.Sqrt((r - g) * (r - g) + (r - b) * (g - b)); - float theta = MathF.Acos(numerator / denominator); + float numerator = 0.5f * ((r - g) + (r - b)); // 分母 + float denominator = MathF.Sqrt((r - g) * (r - g) + (r - b) * (g - b)); // 分子 + float theta = MathF.Acos(numerator / denominator); // 色调 if (b <= g) h = (int)((theta / (2 * MathF.PI)) * 360); else diff --git a/DigitalImageProcessing/GLobalTools.cs b/DigitalImageProcessing/GLobalTools.cs new file mode 100644 index 0000000..9bdbfcc --- /dev/null +++ b/DigitalImageProcessing/GLobalTools.cs @@ -0,0 +1,35 @@ +using SkiaSharp; + +namespace DigitalImageProcessing +{ + internal static class GLobalTools + { + public static SKBitmap GetFull(string filepath) => SKBitmap.Decode(filepath); + public static SKBitmap GetGray(string filepath) + { + SKBitmap src = SKBitmap.Decode(filepath); + Parallel.For(0, src.Height - 1, (y) => + Parallel.For(0, src.Width - 1, (x) => + { + SKColor color = src.GetPixel(x, y); + byte avg = (byte)((color.Red + color.Green + color.Blue) / 3); + src.SetPixel(x, y, new(avg, avg, avg)); + }) + ); + return src; + } + public static SKBitmap GetBinary(string filepath) + { + SKBitmap src = SKBitmap.Decode(filepath); + Parallel.For(0, src.Height - 1, (y) => + Parallel.For(0, src.Width - 1, (x) => + { + SKColor color = src.GetPixel(x, y); + byte bin = (byte)((color.Red + color.Green + color.Blue) / 3 > 128 ? 256 : 0); + src.SetPixel(x, y, new(bin, bin, bin)); + }) + ); + return src; + } + } +} diff --git a/DigitalImageProcessing/HSI.cs b/DigitalImageProcessing/HSI.cs new file mode 100644 index 0000000..65ea8be --- /dev/null +++ b/DigitalImageProcessing/HSI.cs @@ -0,0 +1,34 @@ +using SkiaSharp; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DigitalImageProcessing +{ + public static class HSI + { + public static void Run() + { + string imagepath = @"C:\Users\Surface\Downloads\" + @"1682575881230001.png"; + SKBitmap bitmap1 = GLobalTools.GetGray(imagepath); + Image2D image1 = new(bitmap1.Width, bitmap1.Height); + for (int x = 0; x < bitmap1.Width; x++) + { + for (int y = 0; y < bitmap1.Height; y++) + { + SKColor color1 = bitmap1.GetPixel(x, y); + color1.ToHsi(out int h, out int s, out int i); + SKColor color2 = new SKColor( + (byte)(h * 256f / 360f), + (byte)(s * 256f / 100f), + (byte)(i * 256f / 100f)); + bitmap1.SetPixel(x, y, color2); + } + } + using FileStream fs = new(@"C:\Users\Surface\Downloads\" + "output_hsi.png", FileMode.Create, FileAccess.Write); + bitmap1.Encode(fs, SKEncodedImageFormat.Png, 100); + } + } +} diff --git a/DigitalImageProcessing/MorphologicalOperations.cs b/DigitalImageProcessing/MorphologicalOperations.cs new file mode 100644 index 0000000..6be0faf --- /dev/null +++ b/DigitalImageProcessing/MorphologicalOperations.cs @@ -0,0 +1,60 @@ +using System; + +namespace DigitalImageProcessing +{ + public static class MorphologicalOperations + { + public static Image2D ApplyKernel(Image2D input, int[,] kernel) + { + int width = input.Width; + int height = input.Height; + int kernelWidth = kernel.GetLength(0); + int kernelHeight = kernel.GetLength(1); + int kernelCenterX = kernelWidth / 2; + int kernelCenterY = kernelHeight / 2; + + Image2D output = new Image2D(width, height); + + for (int x = 0; x < width; x++) + { + for (int y = 0; y < height; y++) + { + bool match = true; + + for (int kx = 0; kx < kernelWidth; kx++) + { + for (int ky = 0; ky < kernelHeight; ky++) + { + int nx = x + kx - kernelCenterX; + int ny = y + ky - kernelCenterY; + + if (nx >= 0 && nx < width && ny >= 0 && ny < height) + { + if (kernel[kx, ky] == 1 && !input[nx, ny]) + { + match = false; + break; + } + else if (kernel[kx, ky] == -1 && input[nx, ny]) + { + match = false; + break; + } + } + else if (kernel[kx, ky] == 1) + { + match = false; + break; + } + } + if (!match) break; + } + + output[x, y] = match; + } + } + + return output; + } + } +} diff --git a/DigitalImageProcessing/Program.cs b/DigitalImageProcessing/Program.cs index 870abba..5aa51f4 100644 --- a/DigitalImageProcessing/Program.cs +++ b/DigitalImageProcessing/Program.cs @@ -1,17 +1,19 @@ using DigitalImageProcessing; using SkiaSharp; -string testImage = @"C:\Users\Surface\Downloads\" + @"1682575881230001.png"; -//Alograms.Steps(@"C:\Users\Surface\Downloads\", @"C:\Users\Surface\Downloads\" + @"1682575881230001.png"); +HSI.Run(); -SKBitmap d = Alograms.GetDefaultBitmap(new(1024, 512)); +//string testImage = @"C:\Users\Surface\Downloads\" + @"1682575881230001.png"; +////Alograms.Steps(@"C:\Users\Surface\Downloads\", @"C:\Users\Surface\Downloads\" + @"1682575881230001.png"); -var noised = Alograms.Noises(d); +//SKBitmap d = Alograms.GetDefaultBitmap(new(1024, 512)); -using FileStream fs = new(@"C:\Users\Surface\Downloads\" + @"noise.png", FileMode.Create, FileAccess.Write); -noised.Encode(fs, SKEncodedImageFormat.Png, 100); +//var noised = Alograms.Noises(d); -var filtered = Alograms.Filt(d); +//using FileStream fs = new(@"C:\Users\Surface\Downloads\" + @"noise.png", FileMode.Create, FileAccess.Write); +//noised.Encode(fs, SKEncodedImageFormat.Png, 100); -using FileStream fs2 = new(@"C:\Users\Surface\Downloads\" + @"filtered.png", FileMode.Create, FileAccess.Write); -filtered.Encode(fs2, SKEncodedImageFormat.Png, 100); \ No newline at end of file +//var filtered = Alograms.Filt(d); + +//using FileStream fs2 = new(@"C:\Users\Surface\Downloads\" + @"filtered.png", FileMode.Create, FileAccess.Write); +//filtered.Encode(fs2, SKEncodedImageFormat.Png, 100); \ No newline at end of file diff --git a/DigitalImageProcessing/Shape.cs b/DigitalImageProcessing/Shape.cs new file mode 100644 index 0000000..50f7302 --- /dev/null +++ b/DigitalImageProcessing/Shape.cs @@ -0,0 +1,57 @@ +using SkiaSharp; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DigitalImageProcessing +{ + public struct Image2D + { + private T[,] data; + public readonly int Width => data.GetLength(0); + public readonly int Height => data.GetLength(1); + public readonly T this[int x, int y] + { + get => data[x, y]; + set => data[x, y] = value; + } + + public Image2D(int width, int height) + { + data = new T[width, height]; + } + } + public static class Shape + { + public static void Run() + { + string imagepath = @"C:\Users\Surface\Downloads\" + @"1682575881230001.png"; + SKBitmap bitmap1 = SKBitmap.Decode(imagepath); + Image2D image1 = new(bitmap1.Width, bitmap1.Height); + for (int x = 0; x < bitmap1.Width; x++) + { + for (int y = 0; y < bitmap1.Height; y++) + { + SKColor color = bitmap1.GetPixel(x, y); + image1[x, y] = color.Red < 128; + } + } + + + + SKBitmap bitmap2 = new SKBitmap(image1.Width, image1.Height); + for(int x = 0;x