Browse Source

More speed improvements

Former-commit-id: b2e663bca1492bf9e28a1da8051731646a19feef
Former-commit-id: ec08860c20b0eb2e8423d834b3548fd939d90a76
af/merge-core
James South 12 years ago
parent
commit
0cf4ca8b92
  1. 3
      src/ImageProcessor.Playground/images/input/circle3.png
  2. 10
      src/ImageProcessor/Imaging/Convolution.cs
  3. 6
      src/ImageProcessor/Imaging/FastBitmap.cs
  4. 15
      src/ImageProcessor/Imaging/Filters/Binarization/BinaryThreshold.cs
  5. 13
      src/ImageProcessor/Imaging/Filters/EdgeDetection/ConvolutionFilter.cs
  6. 4
      src/ImageProcessor/Processors/EntropyCrop.cs

3
src/ImageProcessor.Playground/images/input/circle3.png

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:7f6f6f0e7876609f12e7549a4fb605102e62027661a1fe93bbffad96ec5e5731
size 5322

10
src/ImageProcessor/Imaging/Convolution.cs

@ -12,6 +12,7 @@ namespace ImageProcessor.Imaging
{ {
using System; using System;
using System.Drawing; using System.Drawing;
using System.Threading.Tasks;
using ImageProcessor.Common.Extensions; using ImageProcessor.Common.Extensions;
@ -274,7 +275,10 @@ namespace ImageProcessor.Imaging
int threshold = this.Threshold; int threshold = this.Threshold;
// For each line // For each line
for (int y = 0; y < height; y++) Parallel.For(
0,
height,
y =>
{ {
// For each pixel // For each pixel
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
@ -321,6 +325,7 @@ namespace ImageProcessor.Imaging
if (offsetX < width) if (offsetX < width)
{ {
// ReSharper disable once AccessToDisposedClosure
Color color = sourceFastBitmap.GetPixel(offsetX, offsetY); Color color = sourceFastBitmap.GetPixel(offsetX, offsetY);
double k = kernel[i, j]; double k = kernel[i, j];
divider += k; divider += k;
@ -366,9 +371,10 @@ namespace ImageProcessor.Imaging
blue += threshold; blue += threshold;
alpha += threshold; alpha += threshold;
// ReSharper disable once AccessToDisposedClosure
destinationFastBitmap.SetPixel(x, y, Color.FromArgb(alpha.ToByte(), red.ToByte(), green.ToByte(), blue.ToByte())); destinationFastBitmap.SetPixel(x, y, Color.FromArgb(alpha.ToByte(), red.ToByte(), green.ToByte(), blue.ToByte()));
} }
} });
} }
} }

6
src/ImageProcessor/Imaging/FastBitmap.cs

@ -156,6 +156,7 @@ namespace ImageProcessor.Imaging
/// <returns>The <see cref="System.Drawing.Color"/> at the given pixel.</returns> /// <returns>The <see cref="System.Drawing.Color"/> at the given pixel.</returns>
public Color GetPixel(int x, int y) public Color GetPixel(int x, int y)
{ {
#if DEBUG
if ((x < 0) || (x >= this.width)) if ((x < 0) || (x >= this.width))
{ {
throw new ArgumentOutOfRangeException("x", "Value cannot be less than zero or greater than the bitmap width."); throw new ArgumentOutOfRangeException("x", "Value cannot be less than zero or greater than the bitmap width.");
@ -165,7 +166,7 @@ namespace ImageProcessor.Imaging
{ {
throw new ArgumentOutOfRangeException("y", "Value cannot be less than zero or greater than the bitmap height."); throw new ArgumentOutOfRangeException("y", "Value cannot be less than zero or greater than the bitmap height.");
} }
#endif
PixelData* data = this[x, y]; PixelData* data = this[x, y];
return Color.FromArgb(data->A, data->R, data->G, data->B); return Color.FromArgb(data->A, data->R, data->G, data->B);
} }
@ -181,6 +182,7 @@ namespace ImageProcessor.Imaging
/// </param> /// </param>
public void SetPixel(int x, int y, Color color) public void SetPixel(int x, int y, Color color)
{ {
#if DEBUG
if ((x < 0) || (x >= this.width)) if ((x < 0) || (x >= this.width))
{ {
throw new ArgumentOutOfRangeException("x", "Value cannot be less than zero or greater than the bitmap width."); throw new ArgumentOutOfRangeException("x", "Value cannot be less than zero or greater than the bitmap width.");
@ -190,7 +192,7 @@ namespace ImageProcessor.Imaging
{ {
throw new ArgumentOutOfRangeException("y", "Value cannot be less than zero or greater than the bitmap height."); throw new ArgumentOutOfRangeException("y", "Value cannot be less than zero or greater than the bitmap height.");
} }
#endif
PixelData* data = this[x, y]; PixelData* data = this[x, y];
data->R = color.R; data->R = color.R;
data->G = color.G; data->G = color.G;

15
src/ImageProcessor/Imaging/Filters/Binarization/BinaryThreshold.cs

@ -11,6 +11,10 @@
namespace ImageProcessor.Imaging.Filters.Binarization namespace ImageProcessor.Imaging.Filters.Binarization
{ {
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging;
using System.Threading.Tasks;
using ImageProcessor.Imaging.Filters.Photo;
/// <summary> /// <summary>
/// Performs binary threshold filtering against a given greyscale image. /// Performs binary threshold filtering against a given greyscale image.
@ -20,7 +24,7 @@ namespace ImageProcessor.Imaging.Filters.Binarization
/// <summary> /// <summary>
/// The threshold value. /// The threshold value.
/// </summary> /// </summary>
private byte threshold = 128; private byte threshold = 10;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="BinaryThreshold"/> class. /// Initializes a new instance of the <see cref="BinaryThreshold"/> class.
@ -61,14 +65,19 @@ namespace ImageProcessor.Imaging.Filters.Binarization
using (FastBitmap sourceBitmap = new FastBitmap(source)) using (FastBitmap sourceBitmap = new FastBitmap(source))
{ {
for (int y = 0; y < height; y++) Parallel.For(
0,
height,
y =>
{ {
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
// ReSharper disable AccessToDisposedClosure
Color color = sourceBitmap.GetPixel(x, y); Color color = sourceBitmap.GetPixel(x, y);
sourceBitmap.SetPixel(x, y, color.B >= this.threshold ? Color.White : Color.Black); sourceBitmap.SetPixel(x, y, color.B >= this.threshold ? Color.White : Color.Black);
// ReSharper restore AccessToDisposedClosure
} }
} });
} }
return source; return source;

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

@ -49,7 +49,7 @@ namespace ImageProcessor.Imaging.Filters.EdgeDetection
} }
/// <summary> /// <summary>
/// Processes the given bitmap to apply the current instances <see cref="IEdgeFilter"/>. /// Processes the given bitmap to apply the current instance of <see cref="IEdgeFilter"/>.
/// </summary> /// </summary>
/// <param name="source">The image to process.</param> /// <param name="source">The image to process.</param>
/// <returns>A processed bitmap.</returns> /// <returns>A processed bitmap.</returns>
@ -184,7 +184,7 @@ namespace ImageProcessor.Imaging.Filters.EdgeDetection
} }
/// <summary> /// <summary>
/// Processes the given bitmap to apply the current instances <see cref="IEdgeFilter"/>. /// Processes the given bitmap to apply the current instance of <see cref="I2DEdgeFilter"/>.
/// </summary> /// </summary>
/// <param name="source">The image to process.</param> /// <param name="source">The image to process.</param>
/// <returns>A processed bitmap.</returns> /// <returns>A processed bitmap.</returns>
@ -229,7 +229,10 @@ 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,
height,
y =>
{ {
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
@ -271,6 +274,7 @@ namespace ImageProcessor.Imaging.Filters.EdgeDetection
if (offsetX < width) if (offsetX < width)
{ {
// ReSharper disable once AccessToDisposedClosure
Color currentColor = sourceBitmap.GetPixel(offsetX, offsetY); Color currentColor = sourceBitmap.GetPixel(offsetX, offsetY);
double r = currentColor.R; double r = currentColor.R;
double g = currentColor.G; double g = currentColor.G;
@ -294,9 +298,10 @@ namespace ImageProcessor.Imaging.Filters.EdgeDetection
byte blue = Math.Sqrt((bX * bX) + (bY * bY)).ToByte(); byte blue = Math.Sqrt((bX * bX) + (bY * bY)).ToByte();
Color newColor = Color.FromArgb(red, green, blue); Color newColor = Color.FromArgb(red, green, blue);
// ReSharper disable once AccessToDisposedClosure
destinationBitmap.SetPixel(x, y, newColor); destinationBitmap.SetPixel(x, y, newColor);
} }
} });
} }
} }
} }

4
src/ImageProcessor/Processors/EntropyCrop.cs

@ -11,6 +11,7 @@ namespace ImageProcessor.Processors
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO;
using ImageProcessor.Common.Exceptions; using ImageProcessor.Common.Exceptions;
using ImageProcessor.Imaging; using ImageProcessor.Imaging;
@ -59,7 +60,8 @@ namespace ImageProcessor.Processors
try try
{ {
grey = new ConvolutionFilter(new SobelEdgeFilter(), true).ProcessFilter((Bitmap)image); // Detect the edges then strip out middle shades.
grey = new ConvolutionFilter(new SobelEdgeFilter(), true).Process2DFilter((Bitmap)image);
grey = new BinaryThreshold(threshold).ProcessFilter(grey); grey = new BinaryThreshold(threshold).ProcessFilter(grey);
Rectangle rectangle = this.FindBoundingBox(grey, 0); Rectangle rectangle = this.FindBoundingBox(grey, 0);

Loading…
Cancel
Save