Browse Source

First attempt at energy cropping

Former-commit-id: dff2b626bdbc8ef13e4802bae1980d47123aade9
pull/17/head
James South 12 years ago
parent
commit
3aaa22c05f
  1. 11
      src/ImageProcessor/ImageFactory.cs
  2. 3
      src/ImageProcessor/ImageProcessor.csproj
  3. 78
      src/ImageProcessor/Imaging/Binarization/BinaryThreshold.cs
  4. 26
      src/ImageProcessor/Imaging/FastBitmap.cs
  5. 38
      src/ImageProcessor/Imaging/PixelData.cs
  6. 222
      src/ImageProcessor/Processors/AutoCrop.cs
  7. 7
      src/ImageProcessorConsole/Program.cs
  8. 1
      src/ImageProcessorConsole/images/input/monster-whitebg.png2.REMOVED.git-id
  9. BIN
      src/ImageProcessorConsole/images/input/pixel.png3

11
src/ImageProcessor/ImageFactory.cs

@ -288,6 +288,17 @@ namespace ImageProcessor
return this;
}
public ImageFactory AutoCrop(byte threshold = 128)
{
if (this.ShouldProcess)
{
AutoCrop autoCrop = new AutoCrop() { DynamicParameter = threshold };
this.CurrentImageFormat.ApplyProcessor(autoCrop.ProcessImage, this);
}
return this;
}
/// <summary>
/// Performs auto-rotation to ensure that EXIF defined rotation is reflected in
/// the final image.

3
src/ImageProcessor/ImageProcessor.csproj

@ -110,6 +110,7 @@
<Compile Include="Imaging\GaussianLayer.cs" />
<Compile Include="Imaging\Formats\GifEncoder.cs" />
<Compile Include="Imaging\Formats\GifFrame.cs" />
<Compile Include="Imaging\PixelData.cs" />
<Compile Include="Imaging\Quantizer.cs" />
<Compile Include="Imaging\ResizeLayer.cs" />
<Compile Include="Imaging\Filters\BlackWhiteMatrixFilter.cs" />
@ -129,8 +130,10 @@
<Compile Include="Imaging\TextLayer.cs" />
<Compile Include="Imaging\OctreeQuantizer.cs" />
<Compile Include="Processors\Alpha.cs" />
<Compile Include="Processors\AutoCrop.cs" />
<Compile Include="Processors\BackgroundColor.cs" />
<Compile Include="Processors\AutoRotate.cs" />
<Compile Include="Imaging\Binarization\BinaryThreshold.cs" />
<Compile Include="Processors\DetectEdges.cs" />
<Compile Include="Processors\GaussianBlur.cs" />
<Compile Include="Processors\Brightness.cs" />

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

@ -0,0 +1,78 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BinaryThreshold.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Performs binary threshold filtering against a given greyscale image.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Imaging.Binarization
{
using System.Drawing;
/// <summary>
/// Performs binary threshold filtering against a given greyscale image.
/// </summary>
public class BinaryThreshold
{
/// <summary>
/// The threshold value.
/// </summary>
private byte threshold = 128;
/// <summary>
/// Initializes a new instance of the <see cref="BinaryThreshold"/> class.
/// </summary>
/// <param name="threshold">
/// The threshold.
/// </param>
public BinaryThreshold(byte threshold)
{
this.threshold = threshold;
}
/// <summary>
/// Gets or sets the threshold.
/// </summary>
public byte Threshold
{
get
{
return this.threshold;
}
set
{
this.threshold = value;
}
}
/// <summary>
/// Processes the given bitmap to apply the threshold.
/// </summary>
/// <param name="source">The image to process.</param>
/// <returns>A processed bitmap.</returns>
public Bitmap ProcessFilter(Bitmap source)
{
int width = source.Width;
int height = source.Height;
using (FastBitmap fastBitmap = new FastBitmap(source))
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Color color = fastBitmap.GetPixel(x, y);
fastBitmap.SetPixel(x, y, color.B >= this.threshold ? Color.White : Color.Black);
}
}
}
return source;
}
}
}

26
src/ImageProcessor/Imaging/FastBitmap.cs

@ -270,31 +270,5 @@ namespace ImageProcessor.Imaging
this.bitmapData = null;
this.pixelBuffer = null;
}
/// <summary>
/// The pixel data.
/// </summary>
private struct PixelData
{
/// <summary>
/// The blue component.
/// </summary>
public byte B;
/// <summary>
/// The green component.
/// </summary>
public byte G;
/// <summary>
/// The red component.
/// </summary>
public byte R;
/// <summary>
/// The alpha component.
/// </summary>
public byte A;
}
}
}

38
src/ImageProcessor/Imaging/PixelData.cs

@ -0,0 +1,38 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PixelData.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Contains the component parts that make up a single pixel.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Imaging
{
/// <summary>
/// Contains the component parts that make up a single pixel.
/// </summary>
public struct PixelData
{
/// <summary>
/// The blue component.
/// </summary>
public byte B;
/// <summary>
/// The green component.
/// </summary>
public byte G;
/// <summary>
/// The red component.
/// </summary>
public byte R;
/// <summary>
/// The alpha component.
/// </summary>
public byte A;
}
}

222
src/ImageProcessor/Processors/AutoCrop.cs

@ -0,0 +1,222 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AutoCrop.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Processors
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using ImageProcessor.Common.Exceptions;
using ImageProcessor.Imaging;
using ImageProcessor.Imaging.Binarization;
using ImageProcessor.Imaging.EdgeDetection;
/// <summary>
/// The auto crop.
/// </summary>
public class AutoCrop : IGraphicsProcessor
{
/// <summary>
/// Initializes a new instance of the <see cref="AutoCrop"/> class.
/// </summary>
public AutoCrop()
{
this.Settings = new Dictionary<string, string>();
}
/// <summary>
/// Gets or sets the dynamic parameter.
/// </summary>
public dynamic DynamicParameter { get; set; }
/// <summary>
/// Gets or sets any additional settings required by the processor.
/// </summary>
public Dictionary<string, string> Settings { get; set; }
/// <summary>
/// Processes the image.
/// </summary>
/// <param name="factory">
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing
/// the image to process.
/// </param>
/// <returns>
/// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public Image ProcessImage(ImageFactory factory)
{
Bitmap newImage = null;
Bitmap grey = null;
Image image = factory.Image;
byte threshold = this.DynamicParameter;
try
{
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);
// lock source bitmap data
//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);
using (Graphics graphics = Graphics.FromImage(newImage))
{
// An unwanted border appears when using InterpolationMode.HighQualityBicubic to resize the image
// as the algorithm appears to be pulling averaging detail from surrounding pixels beyond the edge
// of the image. Using the ImageAttributes class to specify that the pixels beyond are simply mirror
// images of the pixels within solves this problem.
using (ImageAttributes wrapMode = new ImageAttributes())
{
graphics.DrawImage(
image,
new Rectangle(0, 0, rectangle.Width, rectangle.Height),
rectangle.X,
rectangle.Y,
rectangle.Width,
rectangle.Height,
GraphicsUnit.Pixel,
wrapMode);
}
}
// Reassign the image.
//grey.Dispose();
image.Dispose();
image = newImage;
}
catch (Exception ex)
{
if (grey != null)
{
grey.Dispose();
}
if (newImage != null)
{
newImage.Dispose();
}
throw new ImageProcessingException("Error processing image with " + this.GetType().Name, ex);
}
return image;
}
/// <summary>
/// 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");
// get source image size
int width = sourceData.Width;
int height = sourceData.Height;
int offset = sourceData.Stride - width;
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;
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++)
{
if (x > 0 || y > 0) src++; //Don't increment until after the first pixel.
if (*src != indexToRemove)
{
if (x < minX)
minX = x;
if (x > maxX)
maxX = x;
if (y < minY)
minY = y;
if (y > maxY)
maxY = y;
}
}
}
}
// check
if ((minX == width) && (minY == height) && (maxX == 0) && (maxY == 0))
{
minX = minY = 0;
}
return new Rectangle(minX, minY, maxX - minX + 1, maxY - minY + 1);
}
private Rectangle FindBox(Bitmap bitmap, byte indexToRemove)
{
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))
{
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (fastBitmap.GetPixel(x, y).B != indexToRemove)
{
if (x < startX)
{
startX = x;
}
if (x > stopX)
{
stopX = x;
}
if (y < startY)
{
startY = y;
}
if (y > stopY)
{
stopY = y;
}
}
}
}
}
// check
if ((startX == width) && (startY == height) && (stopX == 0) && (stopY == 0))
{
startX = startY = 0;
}
return new Rectangle(startX, startY, stopX - startX + 1, stopY - startY + 1);
}
}
}

7
src/ImageProcessorConsole/Program.cs

@ -46,7 +46,7 @@ namespace ImageProcessorConsole
di.Create();
}
IEnumerable<FileInfo> files = GetFilesByExtensions(di, ".png");
IEnumerable<FileInfo> files = GetFilesByExtensions(di, ".png2");
//IEnumerable<FileInfo> files = GetFilesByExtensions(di, ".gif", ".webp", ".bmp", ".jpg", ".png", ".tif");
foreach (FileInfo fileInfo in files)
@ -76,8 +76,9 @@ namespace ImageProcessorConsole
//.ContentAwareResize(layer)
//.Constrain(size)
//.ReplaceColor(Color.FromArgb(255, 1, 107, 165), Color.FromArgb(255, 1, 165, 13), 80)
.Resize(layer)
.DetectEdges(new KirschEdgeFilter())
//.Resize(layer)
//.DetectEdges(new KirschEdgeFilter())
.AutoCrop()
//.Filter(MatrixFilters.Comic)
//.Filter(MatrixFilters.HiSatch)
//.Pixelate(8)

1
src/ImageProcessorConsole/images/input/monster-whitebg.png2.REMOVED.git-id

@ -0,0 +1 @@
71ccd68b75f913237cdd2047d5f5d0b39d9b16ae

BIN
src/ImageProcessorConsole/images/input/pixel.png3

Binary file not shown.

After

Width:  |  Height:  |  Size: 166 B

Loading…
Cancel
Save