Browse Source

Improving convolution filter speed with Parallel.For

TODO: Check this again and expand to other methods

Former-commit-id: 8a09d4084aaf683b556f5af8d617a005038a604b
Former-commit-id: 25acdfeac0ffc707cce3407a970c5f19eccc63b7
af/merge-core
James South 12 years ago
parent
commit
0425c931a2
  1. 4
      src/ImageProcessor.Playground/Program.cs
  2. 96
      src/ImageProcessor/Imaging/Filters/EdgeDetection/ConvolutionFilter.cs

4
src/ImageProcessor.Playground/Program.cs

@ -77,9 +77,9 @@ namespace ImageProcessor.PlayGround
//.ReplaceColor(Color.FromArgb(255, 1, 107, 165), Color.FromArgb(255, 1, 165, 13), 80) //.ReplaceColor(Color.FromArgb(255, 1, 107, 165), Color.FromArgb(255, 1, 165, 13), 80)
//.Resize(layer) //.Resize(layer)
//.DetectEdges(new KirschEdgeFilter()) //.DetectEdges(new KirschEdgeFilter())
//.DetectEdges(new LaplacianOfGaussianFilter()) .DetectEdges(new LaplacianOfGaussianEdgeFilter())
//.EntropyCrop() //.EntropyCrop()
.Filter(MatrixFilters.Comic) //.Filter(MatrixFilters.Comic)
//.Filter(MatrixFilters.Comic) //.Filter(MatrixFilters.Comic)
//.Filter(MatrixFilters.HiSatch) //.Filter(MatrixFilters.HiSatch)
//.Pixelate(8) //.Pixelate(8)

96
src/ImageProcessor/Imaging/Filters/EdgeDetection/ConvolutionFilter.cs

@ -13,6 +13,7 @@ namespace ImageProcessor.Imaging.Filters.EdgeDetection
using System; using System;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.Threading.Tasks;
using ImageProcessor.Common.Extensions; using ImageProcessor.Common.Extensions;
using ImageProcessor.Imaging.Filters.Photo; using ImageProcessor.Imaging.Filters.Photo;
@ -92,68 +93,73 @@ namespace ImageProcessor.Imaging.Filters.EdgeDetection
using (FastBitmap destinationBitmap = new FastBitmap(destination)) using (FastBitmap destinationBitmap = new FastBitmap(destination))
{ {
// Loop through the pixels. // Loop through the pixels.
for (int y = 0; y < height; y++) Parallel.For(
{ 0,
for (int x = 0; x < width; x++) height,
y =>
{ {
double rX = 0; for (int x = 0; x < width; x++)
double gX = 0;
double bX = 0;
// Apply each matrix multiplier to the color components for each pixel.
for (int fy = 0; fy < kernelLength; fy++)
{ {
int fyr = fy - radius; double rX = 0;
int offsetY = y + fyr; double gX = 0;
double bX = 0;
// Skip the current row
if (offsetY < 0)
{
continue;
}
// Outwith the current bounds so break.
if (offsetY >= height)
{
break;
}
for (int fx = 0; fx < kernelLength; fx++) // Apply each matrix multiplier to the color components for each pixel.
for (int fy = 0; fy < kernelLength; fy++)
{ {
int fxr = fx - radius; int fyr = fy - radius;
int offsetX = x + fxr; int offsetY = y + fyr;
// Skip the column // Skip the current row
if (offsetX < 0) if (offsetY < 0)
{ {
continue; continue;
} }
if (offsetX < width) // Outwith the current bounds so break.
if (offsetY >= height)
{ {
Color currentColor = sourceBitmap.GetPixel(offsetX, offsetY); break;
double r = currentColor.R; }
double g = currentColor.G;
double b = currentColor.B;
rX += horizontalFilter[fy, fx] * r; for (int fx = 0; fx < kernelLength; fx++)
{
int fxr = fx - radius;
int offsetX = x + fxr;
gX += horizontalFilter[fy, fx] * g; // Skip the column
if (offsetX < 0)
{
continue;
}
bX += horizontalFilter[fy, fx] * b; if (offsetX < width)
{
// ReSharper disable once AccessToDisposedClosure
Color currentColor = sourceBitmap.GetPixel(offsetX, offsetY);
double r = currentColor.R;
double g = currentColor.G;
double b = currentColor.B;
rX += horizontalFilter[fy, fx] * r;
gX += horizontalFilter[fy, fx] * g;
bX += horizontalFilter[fy, fx] * b;
}
} }
} }
}
// Apply the equation and sanitize. // Apply the equation and sanitize.
byte red = rX.ToByte(); byte red = rX.ToByte();
byte green = gX.ToByte(); byte green = gX.ToByte();
byte blue = bX.ToByte(); byte blue = bX.ToByte();
Color newColor = Color.FromArgb(red, green, blue); Color newColor = Color.FromArgb(red, green, blue);
destinationBitmap.SetPixel(x, y, newColor); // ReSharper disable once AccessToDisposedClosure
} destinationBitmap.SetPixel(x, y, newColor);
} }
});
} }
} }
} }

Loading…
Cancel
Save