This commit is contained in:
2025-11-12 16:37:01 +08:00
parent d19f606b8d
commit 727d52416d
9 changed files with 266 additions and 18 deletions
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OpenCvSharp4.Windows" Version="4.11.0.20250507" />
</ItemGroup>
</Project>
@@ -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<int>(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");
+7 -1
View File
@@ -1,10 +1,12 @@
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.14.36408.4 d17.14 VisualStudioVersion = 17.14.36408.4
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalImageProcessing", "DigitalImageProcessing\DigitalImageProcessing.csproj", "{CC16DC64-4AF5-4AA5-83CF-EC6C2C137450}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalImageProcessing", "DigitalImageProcessing\DigitalImageProcessing.csproj", "{CC16DC64-4AF5-4AA5-83CF-EC6C2C137450}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DIgitalImageProcessingImageSharp", "DIgitalImageProcessingImageSharp\DIgitalImageProcessingImageSharp.csproj", "{D33ECA25-E9B7-4426-B722-C193BC9943A1}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{CC16DC64-4AF5-4AA5-83CF-EC6C2C137450}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
+8 -8
View File
@@ -71,23 +71,23 @@ namespace DigitalImageProcessing
float r = color.Red / 255f; float r = color.Red / 255f;
float g = color.Green / 255f; float g = color.Green / 255f;
float b = color.Blue / 255f; float b = color.Blue / 255f;
float min = MathF.Min(r, MathF.Min(g, b)); float min = MathF.Min(r, MathF.Min(g, b)); // 最低分量
i = (int)(((r + g + b) / 3f) * 255); i = (int)(((r + g + b) / 3f) * 255); // 平均分量
if (i == 0) if (i == 0) // 黑色
{ {
h = 0; h = 0;
s = 0; s = 0;
return; return;
} }
s = (int)((1 - (min / (i / 255f))) * 255); s = (int)((1 - (min / (i / 255f))) * 255); // 亮度
if (s == 0) if (s == 0) // 黑色*2
{ {
h = 0; h = 0;
return; return;
} }
float numerator = 0.5f * ((r - g) + (r - b)); float numerator = 0.5f * ((r - g) + (r - b)); // 分母
float denominator = MathF.Sqrt((r - g) * (r - g) + (r - b) * (g - b)); float denominator = MathF.Sqrt((r - g) * (r - g) + (r - b) * (g - b)); // 分子
float theta = MathF.Acos(numerator / denominator); float theta = MathF.Acos(numerator / denominator); // 色调
if (b <= g) if (b <= g)
h = (int)((theta / (2 * MathF.PI)) * 360); h = (int)((theta / (2 * MathF.PI)) * 360);
else else
+35
View File
@@ -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;
}
}
}
+34
View File
@@ -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<bool> 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);
}
}
}
@@ -0,0 +1,60 @@
using System;
namespace DigitalImageProcessing
{
public static class MorphologicalOperations
{
public static Image2D<bool> ApplyKernel(Image2D<bool> 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<bool> output = new Image2D<bool>(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;
}
}
}
+11 -9
View File
@@ -1,17 +1,19 @@
using DigitalImageProcessing; using DigitalImageProcessing;
using SkiaSharp; using SkiaSharp;
string testImage = @"C:\Users\Surface\Downloads\" + @"1682575881230001.png"; HSI.Run();
//Alograms.Steps(@"C:\Users\Surface\Downloads\", @"C:\Users\Surface\Downloads\" + @"1682575881230001.png");
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); //var noised = Alograms.Noises(d);
noised.Encode(fs, SKEncodedImageFormat.Png, 100);
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); //var filtered = Alograms.Filt(d);
filtered.Encode(fs2, SKEncodedImageFormat.Png, 100);
//using FileStream fs2 = new(@"C:\Users\Surface\Downloads\" + @"filtered.png", FileMode.Create, FileAccess.Write);
//filtered.Encode(fs2, SKEncodedImageFormat.Png, 100);
+57
View File
@@ -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<T>
{
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<bool> 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<image1.Width; x++)
for(int y=0;y < image1.Height;y++)
{
if (image1[x, y])
bitmap2.SetPixel(x, y, new SKColor(0, 0, 0));
else
bitmap2.SetPixel(x, y, new SKColor(255, 255, 255));
}
using FileStream fs = new(@"C:\Users\Surface\Downloads\" + "output.png", FileMode.Create, FileAccess.Write);
bitmap2.Encode(fs, SKEncodedImageFormat.Png, 100);
}
}
}