Browse Source

Entropy crop a go go

TODO: Clean up.

Former-commit-id: 168831022aeb9d1db112f6c57d3fb05ff5956366
af/merge-core
James South 12 years ago
parent
commit
96e50d3424
  1. 4
      src/ImageProcessor/ImageFactory.cs
  2. 9
      src/ImageProcessor/Imaging/Binarization/BinaryThreshold.cs
  3. 10
      src/ImageProcessor/Imaging/EdgeDetection/ConvolutionFilter.cs
  4. 173
      src/ImageProcessor/Processors/AutoCrop.cs
  5. 2
      src/ImageProcessorConsole/Program.cs
  6. 1
      src/ImageProcessorConsole/images/input/tower - Copy.jpg2.REMOVED.git-id
  7. 1
      src/ImageProcessorConsole/images/output/monster-whitebg.png.REMOVED.git-id

4
src/ImageProcessor/ImageFactory.cs

@ -288,11 +288,11 @@ namespace ImageProcessor
return this; return this;
} }
public ImageFactory AutoCrop(byte threshold = 128) public ImageFactory EntropyCrop(byte threshold = 128)
{ {
if (this.ShouldProcess) if (this.ShouldProcess)
{ {
AutoCrop autoCrop = new AutoCrop() { DynamicParameter = threshold }; EntropyCrop autoCrop = new EntropyCrop() { DynamicParameter = threshold };
this.CurrentImageFormat.ApplyProcessor(autoCrop.ProcessImage, this); this.CurrentImageFormat.ApplyProcessor(autoCrop.ProcessImage, this);
} }

9
src/ImageProcessor/Imaging/Binarization/BinaryThreshold.cs

@ -11,6 +11,7 @@
namespace ImageProcessor.Imaging.Binarization namespace ImageProcessor.Imaging.Binarization
{ {
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging;
/// <summary> /// <summary>
/// Performs binary threshold filtering against a given greyscale image. /// Performs binary threshold filtering against a given greyscale image.
@ -59,15 +60,15 @@ namespace ImageProcessor.Imaging.Binarization
int width = source.Width; int width = source.Width;
int height = source.Height; int height = source.Height;
using (FastBitmap fastBitmap = new FastBitmap(source)) using (FastBitmap sourceBitmap = new FastBitmap(source))
{ {
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
{ {
Color color = fastBitmap.GetPixel(x, y); Color color = sourceBitmap.GetPixel(x, y);
sourceBitmap.SetPixel(x, y, color.B >= this.threshold ? Color.White : Color.Black);
fastBitmap.SetPixel(x, y, color.B >= this.threshold ? Color.White : Color.Black);
} }
} }
} }

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

@ -170,6 +170,16 @@ namespace ImageProcessor.Imaging.EdgeDetection
input.Dispose(); input.Dispose();
} }
using (Graphics graphics = Graphics.FromImage(destination))
{
// Draw an edge around the image.
using (Pen blackPen = new Pen(Color.Black))
{
blackPen.Width = 4;
graphics.DrawRectangle(blackPen, new Rectangle(0, 0, destination.Width, destination.Height));
}
}
return destination; return destination;
} }
} }

173
src/ImageProcessor/Processors/AutoCrop.cs

@ -20,12 +20,12 @@ namespace ImageProcessor.Processors
/// <summary> /// <summary>
/// The auto crop. /// The auto crop.
/// </summary> /// </summary>
public class AutoCrop : IGraphicsProcessor public class EntropyCrop : IGraphicsProcessor
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="AutoCrop"/> class. /// Initializes a new instance of the <see cref="EntropyCrop"/> class.
/// </summary> /// </summary>
public AutoCrop() public EntropyCrop()
{ {
this.Settings = new Dictionary<string, string>(); this.Settings = new Dictionary<string, string>();
} }
@ -60,38 +60,25 @@ namespace ImageProcessor.Processors
try try
{ {
grey = new ConvolutionFilter(new SobelEdgeFilter(), true).ProcessFilter((Bitmap)image); grey = new ConvolutionFilter(new SobelEdgeFilter(), true).ProcessFilter((Bitmap)image);
grey = new BinaryThreshold(threshold).ProcessFilter(grey);//.Clone(new Rectangle(0, 0, grey.Width, grey.Height), PixelFormat.Format8bppIndexed); grey = new BinaryThreshold(threshold).ProcessFilter(grey);
// lock source bitmap data Rectangle rectangle = this.FindBox(grey, 0);
//BitmapData data = grey.LockBits(new Rectangle(0, 0, grey.Width, grey.Height), ImageLockMode.ReadOnly, grey.PixelFormat);
//Rectangle rectangle = this.FindBoxExactgreyscale(data, 0);
//grey.UnlockBits(data);
Rectangle rectangle = FindBox(grey, 0);
newImage = new Bitmap(rectangle.Width, rectangle.Height, PixelFormat.Format32bppPArgb); newImage = new Bitmap(rectangle.Width, rectangle.Height, PixelFormat.Format32bppPArgb);
using (Graphics graphics = Graphics.FromImage(newImage)) using (Graphics graphics = Graphics.FromImage(newImage))
{ {
// An unwanted border appears when using InterpolationMode.HighQualityBicubic to resize the image graphics.DrawImage(
// as the algorithm appears to be pulling averaging detail from surrounding pixels beyond the edge image,
// of the image. Using the ImageAttributes class to specify that the pixels beyond are simply mirror new Rectangle(0, 0, rectangle.Width, rectangle.Height),
// images of the pixels within solves this problem. rectangle.X,
using (ImageAttributes wrapMode = new ImageAttributes()) rectangle.Y,
{ rectangle.Width,
graphics.DrawImage( rectangle.Height,
image, GraphicsUnit.Pixel);
new Rectangle(0, 0, rectangle.Width, rectangle.Height),
rectangle.X,
rectangle.Y,
rectangle.Width,
rectangle.Height,
GraphicsUnit.Pixel,
wrapMode);
}
} }
// Reassign the image. // Reassign the image.
//grey.Dispose(); grey.Dispose();
image.Dispose(); image.Dispose();
image = newImage; image = newImage;
} }
@ -113,107 +100,87 @@ namespace ImageProcessor.Processors
return image; return image;
} }
/// <summary> private Rectangle FindBox(Bitmap bitmap, byte componentToRemove)
/// Returns a bounding box that only excludes the specified color.
/// Only works on 8-bit images.
/// </summary>
/// <param name="sourceData"></param>
/// <param name="colorToRemove">The palette index to remove.</param>
/// <returns></returns>
private Rectangle FindBoxExactgreyscale(BitmapData sourceData, byte indexToRemove)
{ {
if (sourceData.PixelFormat != PixelFormat.Format8bppIndexed) throw new ArgumentOutOfRangeException("FindBoxExact only operates on 8-bit greyscale images"); int width = bitmap.Width;
// get source image size int height = bitmap.Height;
int width = sourceData.Width; int startX;
int height = sourceData.Height; int startY;
int offset = sourceData.Stride - width; int stopX;
int stopY;
int minX = width;
int minY = height;
int maxX = 0;
int maxY = 0;
// find rectangle which contains something except color to remove
unsafe
{
byte* src = (byte*)sourceData.Scan0;
Func<FastBitmap, int> getMinY = fastBitmap =>
{
for (int y = 0; y < height; y++) for (int y = 0; y < height; y++)
{ {
if (y > 0) src += offset; //Don't adjust for offset until after first row
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
if (x > 0 || y > 0) src++; //Don't increment until after the first pixel. Color c = fastBitmap.GetPixel(x, y);
if (*src != indexToRemove)
if (fastBitmap.GetPixel(x, y).B != componentToRemove)
{ {
if (x < minX) return y;
minX = x;
if (x > maxX)
maxX = x;
if (y < minY)
minY = y;
if (y > maxY)
maxY = y;
} }
} }
} }
}
// check return 0;
if ((minX == width) && (minY == height) && (maxX == 0) && (maxY == 0)) };
Func<FastBitmap, int> getMaxY = fastBitmap =>
{ {
minX = minY = 0; for (int y = height - 1; y > -1; y--)
} {
for (int x = 0; x < width; x++)
{
if (fastBitmap.GetPixel(x, y).B != componentToRemove)
{
return y;
}
}
}
return new Rectangle(minX, minY, maxX - minX + 1, maxY - minY + 1); return height;
} };
Func<FastBitmap, int> getMinX = fastBitmap =>
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
if (fastBitmap.GetPixel(x, y).B != componentToRemove)
{
return x;
}
}
}
private Rectangle FindBox(Bitmap bitmap, byte indexToRemove) return 0;
{ };
int width = bitmap.Width;
int height = bitmap.Height;
int startX = width;
int startY = height;
int stopX = 0;
int stopY = 0;
using (FastBitmap fastBitmap = new FastBitmap(bitmap)) Func<FastBitmap, int> getMaxX = fastBitmap =>
{ {
for (int y = 0; y < height; y++) for (int x = width - 1; x > -1; x--)
{ {
for (int x = 0; x < width; x++) for (int y = 0; y < height; y++)
{ {
if (fastBitmap.GetPixel(x, y).B != indexToRemove) if (fastBitmap.GetPixel(x, y).B != componentToRemove)
{ {
if (x < startX) return x;
{
startX = x;
}
if (x > stopX)
{
stopX = x;
}
if (y < startY)
{
startY = y;
}
if (y > stopY)
{
stopY = y;
}
} }
} }
} }
}
// check return height;
if ((startX == width) && (startY == height) && (stopX == 0) && (stopY == 0)) };
using (FastBitmap fastBitmap = new FastBitmap(bitmap))
{ {
startX = startY = 0; startY = getMinY(fastBitmap);
stopY = getMaxY(fastBitmap);
startX = getMinX(fastBitmap);
stopX = getMaxX(fastBitmap);
} }
return new Rectangle(startX, startY, stopX - startX + 1, stopY - startY + 1); return new Rectangle(startX, startY, stopX - startX + 1, stopY - startY + 1);

2
src/ImageProcessorConsole/Program.cs

@ -78,7 +78,7 @@ namespace ImageProcessorConsole
//.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())
.AutoCrop() .EntropyCrop()
//.Filter(MatrixFilters.Comic) //.Filter(MatrixFilters.Comic)
//.Filter(MatrixFilters.HiSatch) //.Filter(MatrixFilters.HiSatch)
//.Pixelate(8) //.Pixelate(8)

1
src/ImageProcessorConsole/images/input/tower - Copy.jpg2.REMOVED.git-id

@ -0,0 +1 @@
98c3f8fce4fdd9eebafe12431c8e014fd39d243f

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

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