Browse Source

Fixing loop bounds

Former-commit-id: 369c22807ca4949b98d1c6891e95bf43ab345811
pull/17/head
James South 12 years ago
parent
commit
905d950b56
  1. 65
      src/ImageProcessor/Imaging/EdgeDetection/ConvolutionFilter.cs
  2. 24
      src/ImageProcessor/Imaging/EdgeDetection/CostellaEdgeFilter.cs
  3. 2
      src/ImageProcessorConsole/Program.cs
  4. BIN
      src/ImageProcessorConsole/images/output/Bikesgray.png
  5. BIN
      src/ImageProcessorConsole/images/output/Valve_original_(1).PNG
  6. BIN
      src/ImageProcessorConsole/images/output/circle.png
  7. BIN
      src/ImageProcessorConsole/images/output/circle2.png
  8. 1
      src/ImageProcessorConsole/images/output/monster.png.REMOVED.git-id
  9. 1
      src/ImageProcessorConsole/images/output/night-bridge.png.REMOVED.git-id

65
src/ImageProcessor/Imaging/EdgeDetection/ConvolutionFilter.cs

@ -83,21 +83,19 @@ namespace ImageProcessor.Imaging.EdgeDetection
try
{
double[,] horizontalFilter = this.edgeFilter.HorizontalGradientOperator;
double[,] verticallFilter = this.edgeFilter.VerticalGradientOperator;
double[,] verticalFilter = this.edgeFilter.VerticalGradientOperator;
int filterXOffset = (horizontalFilter.GetLength(1) - 1) / 2;
int filterYOffset = (horizontalFilter.GetLength(0) - 1) / 2;
int maxWidth = width - filterXOffset;
int maxHeight = height - filterYOffset;
int kernelLength = horizontalFilter.GetLength(0);
int radius = kernelLength >> 1;
using (FastBitmap sourceBitmap = new FastBitmap(input))
{
using (FastBitmap destinationBitmap = new FastBitmap(destination))
{
// Loop through the pixels.
for (int y = filterYOffset; y < maxHeight; y++)
for (int y = 0; y < height; y++)
{
for (int x = filterXOffset; x < maxWidth; x++)
for (int x = 0; x < width; x++)
{
double rX = 0;
double rY = 0;
@ -107,23 +105,50 @@ namespace ImageProcessor.Imaging.EdgeDetection
double bY = 0;
// Apply each matrix multiplier to the color components for each pixel.
for (int fy = -1; fy < filterYOffset; fy++)
for (int fy = 0; fy < kernelLength; fy++)
{
for (int fx = -1; fx < filterXOffset; fx++)
{
Color currentColor = sourceBitmap.GetPixel(x + fx, y + fy);
double r = currentColor.R;
double g = currentColor.G;
double b = currentColor.B;
int fyr = fy - radius;
int offsetY = y + fyr;
rX += horizontalFilter[fy + 1, fx + 1] * r;
rY += verticallFilter[fy + 1, fx + 1] * r;
// Skip the current row
if (offsetY < 0)
{
continue;
}
gX += horizontalFilter[fy + 1, fx + 1] * g;
gY += verticallFilter[fy + 1, fx + 1] * g;
// Outwith the current bounds so break.
if (offsetY >= height)
{
break;
}
bX += horizontalFilter[fy + 1, fx + 1] * b;
bY += verticallFilter[fy + 1, fx + 1] * b;
for (int fx = 0; fx < kernelLength; fx++)
{
int fxr = fx - radius;
int offsetX = x + fxr;
// Skip the column
if (offsetX < 0)
{
continue;
}
if (offsetX < width)
{
Color currentColor = sourceBitmap.GetPixel(offsetX, offsetY);
double r = currentColor.R;
double g = currentColor.G;
double b = currentColor.B;
rX += horizontalFilter[fy, fx] * r;
rY += verticalFilter[fy, fx] * r;
gX += horizontalFilter[fy, fx] * g;
gY += verticalFilter[fy, fx] * g;
bX += horizontalFilter[fy, fx] * b;
bY += verticalFilter[fy, fx] * b;
}
}
}

24
src/ImageProcessor/Imaging/EdgeDetection/CostellaEdgeFilter.cs

@ -24,12 +24,12 @@ namespace ImageProcessor.Imaging.EdgeDetection
{
get
{
return new double[,]
{ { -1, -1, -1, -1, -1, },
{ -1, -1, -1, -1, -1, },
{ -1, -1, 24, -1, -1, },
{ -1, -1, -1, -1, -1, },
{ -1, -1, -1, -1, -1 }, };
//return new double[,]
//{ { -1, -1, -1, -1, -1, },
// { -1, -1, -1, -1, -1, },
// { -1, -1, 24, -1, -1, },
// { -1, -1, -1, -1, -1, },
// { -1, -1, -1, -1, -1 }, };
return new double[,]
{
{ -1, -3, 0, 3, 1 },
@ -48,12 +48,12 @@ namespace ImageProcessor.Imaging.EdgeDetection
{
get
{
return new double[,]
{ { -1, -1, -1, -1, -1, },
{ -1, -1, -1, -1, -1, },
{ -1, -1, 24, -1, -1, },
{ -1, -1, -1, -1, -1, },
{ -1, -1, -1, -1, -1 }, };
//return new double[,]
//{ { -1, -1, -1, -1, -1, },
// { -1, -1, -1, -1, -1, },
// { -1, -1, 24, -1, -1, },
// { -1, -1, -1, -1, -1, },
// { -1, -1, -1, -1, -1 }, };
return new double[,]
{
{ 1, 1, 1, 1, 1 },

2
src/ImageProcessorConsole/Program.cs

@ -77,7 +77,7 @@ namespace ImageProcessorConsole
//.Constrain(size)
//.ReplaceColor(Color.FromArgb(255, 1, 107, 165), Color.FromArgb(255, 1, 165, 13), 80)
.Resize(layer)
.DetectEdges(new CostellaEdgeFilter(), false)
.DetectEdges(new PrewittEdgeFilter(), false)
//.Filter(MatrixFilters.Comic)
//.Filter(MatrixFilters.HiSatch)
//.Pixelate(8)

BIN
src/ImageProcessorConsole/images/output/Bikesgray.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

BIN
src/ImageProcessorConsole/images/output/Valve_original_(1).PNG

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.0 KiB

BIN
src/ImageProcessorConsole/images/output/circle.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.8 KiB

BIN
src/ImageProcessorConsole/images/output/circle2.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.3 KiB

1
src/ImageProcessorConsole/images/output/monster.png.REMOVED.git-id

@ -1 +0,0 @@
ef1fdff21802713450cf2f81321c0605962ec6f5

1
src/ImageProcessorConsole/images/output/night-bridge.png.REMOVED.git-id

@ -1 +0,0 @@
fad7e5225f13c9d35d9e877c93e2bb44da7404c0
Loading…
Cancel
Save