1117
This commit is contained in:
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user