This commit is contained in:
2025-11-17 10:32:51 +08:00
parent 727d52416d
commit 503b4a59f0
6 changed files with 619 additions and 61 deletions
+37
View File
@@ -0,0 +1,37 @@
namespace DigitalImageProcessing
{
internal static class Segmentation
{
public static void Run()
{
int thresholdValue = 128;
int thresholdDelta = 5;
float delta = float.MaxValue;
int loopCount = 0;
int[,] grayImage = GLobalTools.GetGrayAsArray(@"C:\Users\Surface\Downloads\OIP.webp");
while (delta > thresholdDelta)
{
loopCount++;
List<int> forePix = [], backPix = [];
for (int i = 0; i < grayImage.GetLength(0); i++)
for (int j = 0; j < grayImage.GetLength(1); j++)
if (grayImage[i, j] >= thresholdValue)
forePix.Add(grayImage[i, j]);
else
backPix.Add(grayImage[i, j]);
int fore = (int)forePix.Average();
int back = (int)backPix.Average();
int thres = (fore + back) / 2;
delta = int.Abs(thres - thresholdValue);
thresholdValue = thres;
Console.WriteLine($"Current Threshold Value: {thres}, Delta: {delta}");
}
Console.WriteLine($"Final Threshold Value: {thresholdValue} found in {loopCount} loops.");
int[,] foreground = grayImage.Select(i => i > thresholdValue ? 255 : 0);
foreground.Save(@"C:\Users\Surface\Downloads\OIP_foreground.webp");
}
}
}