Skip to content

Commit

Permalink
ft: update convolution references and fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
bolorundurowb committed Aug 10, 2024
1 parent e1a1487 commit 9bb1439
Show file tree
Hide file tree
Showing 18 changed files with 195 additions and 156 deletions.
3 changes: 2 additions & 1 deletion src/DotNetANPR.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

using DotNetANPR;

const string sourceFilePath = "";
const string sourceFilePath = "/Users/bolorundurowb/Downloads/snapshots/test_093.jpg";
const string reportPath = "/Users/bolorundurowb/Downloads/report";
var result = ANPR.Recognize(sourceFilePath);

Console.WriteLine("The result is: " + result);
1 change: 0 additions & 1 deletion src/dotnetANPR/DotNetANPR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
Expand Down
36 changes: 23 additions & 13 deletions src/dotnetANPR/ImageAnalysis/Band.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,36 @@ public BandGraph Histogram(Bitmap bitmap)

public static void FullEdgeDetector(Bitmap source)
{
float[] verticalMatrix = [-1, 0, 1, -2, 0, 2, -1, 0, 1];
float[] horizontalMatrix = [-1, -2, -1, 0, 0, 0, 1, 2, 1];
var i1 = CreateBlankBitmap(source);
var i2 = CreateBlankBitmap(source);
float[,] verticalMatrix =
{
{ -1, 0, 1 },
{ -2, 0, 2 },
{ -1, 0, 1 }
};

float[,] horizontalMatrix =
{
{ -1, -2, -1 },
{ 0, 0, 0 },
{ 1, 2, 1 }
};

// Apply vertical edge detection
source.ConvolutionFilter(i1, verticalMatrix);
var i1 = source.Convolve(verticalMatrix);

// Apply horizontal edge detection
source.ConvolutionFilter(i2, horizontalMatrix);
var i2 = source.Convolve(horizontalMatrix);

// Combine edge detection results
var width = source.Width;
var height = source.Height;

for (var x = 0; x < width; x++)
for (var y = 0; y < height; y++)
{
var sum = GetBrightness(i1, x, y);
sum += GetBrightness(i2, x, y);
SetBrightness(source, x, y, Math.Min(1f, sum));
}
for (var y = 0; y < height; y++)
{
var sum = GetBrightness(i1, x, y);
sum += GetBrightness(i2, x, y);
SetBrightness(source, x, y, Math.Min(1f, sum));
}
}
}
}
13 changes: 9 additions & 4 deletions src/dotnetANPR/ImageAnalysis/CarSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ public List<Band> Bands()
return response;
}

public void VerticalEdge(Bitmap image)
public Bitmap VerticalEdge(Bitmap image)
{
var imageCopy = DuplicateBitmap(image);
float[] data = [-1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1];
imageCopy.ConvolutionFilter(image, data);
float[,] data = {
{ -1, 0, 1, },
{ -1, 0, 1 },
{ -1, 0, 1 },
{ -1, 0, 1 }
};
return image.Convolve(data);
}

public CarSnapshotGraph Histogram(Bitmap bitmap)
Expand All @@ -69,7 +74,7 @@ private List<Peak> ComputeGraph()
if (_graphHandle == null)
{
var imageCopy = DuplicateBitmap(Image);
VerticalEdge(imageCopy);
imageCopy = VerticalEdge(imageCopy);
Thresholding(imageCopy);

_graphHandle = Histogram(imageCopy);
Expand Down
60 changes: 30 additions & 30 deletions src/dotnetANPR/ImageAnalysis/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,24 @@ public List<double> ExtractEdgeFeatures()
var output = new double[features.Length * 4];

for (var f = 0; f < features.Length; f++)
for (var my = 0; my < height - 1; my++)
for (var mx = 0; mx < width - 1; mx++)
{
double featureMatch = 0;
featureMatch += Math.Abs(array[mx, my] - features[f][0]);
featureMatch += Math.Abs(array[mx + 1, my] - features[f][1]);
featureMatch += Math.Abs(array[mx, my + 1] - features[f][2]);
featureMatch += Math.Abs(array[mx + 1, my + 1] - features[f][3]);
for (var my = 0; my < height - 1; my++)
for (var mx = 0; mx < width - 1; mx++)
{
double featureMatch = 0;
featureMatch += Math.Abs(array[mx, my] - features[f][0]);
featureMatch += Math.Abs(array[mx + 1, my] - features[f][1]);
featureMatch += Math.Abs(array[mx, my + 1] - features[f][2]);
featureMatch += Math.Abs(array[mx + 1, my + 1] - features[f][3]);

var bias = 0;
if (mx >= width / 2)
bias += features.Length; // if we are in the right quadrant, move the bias by one class
var bias = 0;
if (mx >= width / 2)
bias += features.Length; // if we are in the right quadrant, move the bias by one class

if (my >= height / 2)
bias += features.Length * 2; // if we are in the left quadrant, move the bias by two classes
if (my >= height / 2)
bias += features.Length * 2; // if we are in the left quadrant, move the bias by two classes

output[bias + f] += featureMatch < 0.05 ? 1 : 0;
}
output[bias + f] += featureMatch < 0.05 ? 1 : 0;
}

return output.ToList();
}
Expand All @@ -123,8 +123,8 @@ public List<double> ExtractMapFeatures()
{
List<double> vectorInput = [];
for (var y = 0; y < Height; y++)
for (var x = 0; x < Width; x++)
vectorInput.Add(GetBrightness(x, y));
for (var x = 0; x < Width; x++)
vectorInput.Add(GetBrightness(x, y));

return vectorInput;
}
Expand Down Expand Up @@ -180,8 +180,8 @@ private void ComputeStatisticContrast(Bitmap bi)
var w = bi.Width;
var h = bi.Height;
for (var x = 0; x < w; x++)
for (var y = 0; y < h; y++)
sum += Math.Abs(StatisticAverageBrightness - GetBrightness(bi, x, y));
for (var y = 0; y < h; y++)
sum += Math.Abs(StatisticAverageBrightness - GetBrightness(bi, x, y));

StatisticContrast = sum / (w * h);
}
Expand All @@ -195,13 +195,13 @@ private void ComputeStatisticBrightness(Bitmap bi)
var w = bi.Width;
var h = bi.Height;
for (var x = 0; x < w; x++)
for (var y = 0; y < h; y++)
{
var value = GetBrightness(bi, x, y);
sum += value;
min = Math.Min(min, value);
max = Math.Max(max, value);
}
for (var y = 0; y < h; y++)
{
var value = GetBrightness(bi, x, y);
sum += value;
min = Math.Min(min, value);
max = Math.Max(max, value);
}

StatisticAverageBrightness = sum / (w * h);
StatisticMinimumBrightness = min;
Expand All @@ -214,8 +214,8 @@ private void ComputeStatisticHue(Bitmap bi)
var w = bi.Width;
var h = bi.Height;
for (var x = 0; x < w; x++)
for (var y = 0; y < h; y++)
sum += GetHue(bi, x, y);
for (var y = 0; y < h; y++)
sum += GetHue(bi, x, y);

StatisticAverageHue = sum / (w * h);
}
Expand All @@ -226,8 +226,8 @@ private void ComputeStatisticSaturation(Bitmap bi)
var w = bi.Width;
var h = bi.Height;
for (var x = 0; x < w; x++)
for (var y = 0; y < h; y++)
sum += GetSaturation(bi, x, y);
for (var y = 0; y < h; y++)
sum += GetSaturation(bi, x, y);

StatisticAverageSaturation = sum / (w * h);
}
Expand Down
44 changes: 22 additions & 22 deletions src/dotnetANPR/ImageAnalysis/HoughTransformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public HoughTransformation(int width, int height)
_height = height;

for (var x = 0; x < width; x++)
for (var y = 0; y < height; y++)
_bitmap[x, y] = 0;
for (var y = 0; y < height; y++)
_bitmap[x, y] = 0;
}

public void AddLine(int x, int y, float brightness)
Expand Down Expand Up @@ -76,8 +76,8 @@ private float GetAverageValue()
{
float sum = 0;
for (var x = 0; x < _width; x++)
for (var y = 0; y < _height; y++)
sum += _bitmap[x, y];
for (var y = 0; y < _height; y++)
sum += _bitmap[x, y];

return sum / (_width * _height);
}
Expand All @@ -89,16 +89,16 @@ public Bitmap Render(RenderType renderType, ColorType colorType)
var g = Graphics.FromImage(output);

for (var x = 0; x < _width; x++)
for (var y = 0; y < _height; y++)
{
var value = (int)(255 * _bitmap[x, y] / average / 3);
value = Math.Max(0, Math.Min(value, 255));
for (var y = 0; y < _height; y++)
{
var value = (int)(255 * _bitmap[x, y] / average / 3);
value = Math.Max(0, Math.Min(value, 255));

output.SetPixel(x, y,
colorType == ColorType.BlackAndWhite
? Color.FromArgb(value, value, value)
: ColorExtensions.HsbToRgb(0.67f - (float)value / 255 * 2 / 3, 1.0f, 1.0f));
}
output.SetPixel(x, y,
colorType == ColorType.BlackAndWhite
? Color.FromArgb(value, value, value)
: ColorExtensions.HsbToRgb(0.67f - (float)value / 255 * 2 / 3, 1.0f, 1.0f));
}

var maximumPoint = FindMaxPoint();
g.DrawImage(output, 0, 0);
Expand Down Expand Up @@ -138,17 +138,17 @@ private Point FindMaxPoint()
float max = 0;
int maxX = 0, maxY = 0;
for (var x = 0; x < _width; x++)
for (var y = 0; y < _height; y++)
{
var curr = _bitmap[x, y];
for (var y = 0; y < _height; y++)
{
var curr = _bitmap[x, y];

if (!(curr >= max))
continue;
if (!(curr >= max))
continue;

maxX = x;
maxY = y;
max = curr;
}
maxX = x;
maxY = y;
max = curr;
}

return new Point(maxX, maxY);
}
Expand Down
15 changes: 5 additions & 10 deletions src/dotnetANPR/ImageAnalysis/Photo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public class Photo(Bitmap image) : IDisposable, ICloneable
public Bitmap Image
{
get => image;
protected set => image = value;
// TODO: fix this travesty
// protected set => image = value;
internal set => image = value;
}

public static void SetBrightness(Bitmap image, int x, int y, float value)
Expand Down Expand Up @@ -233,13 +235,6 @@ public Bitmap AverageResizeImage(Bitmap origin, int width, int height)

#endregion

public virtual void VerticalEdgeDetector(Bitmap source)
{
var destination = DuplicateBitmap(source);
float[] kernel = [-1, 0, 1, -2, 0, 2, -1, 0, 1];
destination.ConvolutionFilter(source, kernel);
}

public float[,] BitmapToArray(Bitmap image, int width, int height)
{
var array = new float[width, height];
Expand Down Expand Up @@ -307,11 +302,11 @@ public void PlainThresholding(Statistics stat)

public void AdaptiveThresholding()
{
var stat = new Statistics(this);
var statistics = new Statistics(this);
var radius = Configurator.Instance.Get<int>("photo_adaptivethresholdingradius");
if (radius == 0)
{
PlainThresholding(stat);
PlainThresholding(statistics);
return;
}

Expand Down
30 changes: 15 additions & 15 deletions src/dotnetANPR/ImageAnalysis/PixelMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public Bitmap Render()
{
var image = new Bitmap(_width, _height, PixelFormat.Format24bppRgb);
for (var x = 0; x < _width; x++)
for (var y = 0; y < _height; y++)
image.SetPixel(x, y, _matrix[x, y] ? Color.Black : Color.White);
for (var y = 0; y < _height; y++)
image.SetPixel(x, y, _matrix[x, y] ? Color.Black : Color.White);

return image;
}
Expand Down Expand Up @@ -75,9 +75,9 @@ public PixelMap ReduceNoise()
{
PointSet pointsToReduce = [];
for (var x = 0; x < _width; x++)
for (var y = 0; y < _height; y++)
if (N(x, y) < 4)
pointsToReduce.Add(new Point(x, y)); // recommended 4
for (var y = 0; y < _height; y++)
if (N(x, y) < 4)
pointsToReduce.Add(new Point(x, y)); // recommended 4

// remove marked points
foreach (var point in pointsToReduce)
Expand All @@ -92,9 +92,9 @@ public PieceSet FindPieces()
// put all black points into a set
PointSet unsorted = [];
for (var x = 0; x < _width; x++)
for (var y = 0; y < _height; y++)
if (_matrix[x, y])
unsorted.Add(new Point(x, y));
for (var y = 0; y < _height; y++)
if (_matrix[x, y])
unsorted.Add(new Point(x, y));

while (!unsorted.Any())
pieces.Add(CreatePiece(unsorted));
Expand Down Expand Up @@ -137,8 +137,8 @@ private void MatrixInit(Photo bi)
_height = bi.Height;
_matrix = new bool[_width, _height];
for (var x = 0; x < _width; x++)
for (var y = 0; y < _height; y++)
_matrix[x, y] = bi.GetBrightness(x, y) < 0.5;
for (var y = 0; y < _height; y++)
_matrix[x, y] = bi.GetBrightness(x, y) < 0.5;
}

private bool GetPointValue(int x, int y)
Expand Down Expand Up @@ -252,9 +252,9 @@ private void FindBoundaryPoints(PointSet set)
set.Clear();

for (var x = 0; x < _width; x++)
for (var y = 0; y < _height; y++)
if (IsBoundaryPoint(x, y))
set.Add(new Point(x, y));
for (var y = 0; y < _height; y++)
if (IsBoundaryPoint(x, y))
set.Add(new Point(x, y));
}

private bool SeedShouldBeAdded(Piece piece, Point point)
Expand Down Expand Up @@ -393,8 +393,8 @@ public class Piece : PointSet
var image = new Bitmap(Width, Height);

for (var x = MostLeftPoint; x <= MostRightPoint; x++)
for (var y = MostTopPoint; y <= MostBottomPoint; y++)
image.SetPixel(x - MostLeftPoint, y - MostTopPoint, _matrix[x, y] ? Color.Black : Color.White);
for (var y = MostTopPoint; y <= MostBottomPoint; y++)
image.SetPixel(x - MostLeftPoint, y - MostTopPoint, _matrix[x, y] ? Color.Black : Color.White);

return image;
}
Expand Down
Loading

0 comments on commit 9bb1439

Please sign in to comment.