ImageResizer  3.4.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
Public Member Functions | List of all members
ImageResizer.Plugins.WhitespaceTrimmer.BoundingBoxFinder Class Reference

Public Member Functions

Rectangle FindBoxSobel (Bitmap originalImage, Rectangle lookInside, byte threshold)
 Returns a rectangle inside 'lookInside' that bounds any energy greater than 'threshold'. More...
 
Rectangle FindBoxSobel (Bitmap rgb, byte threshold)
 Requires 24 bit or 32 bit (A) RGB image. More...
 
Rectangle FindBoxExactGrayscale (BitmapData sourceData, byte indexToRemove)
 Returns a bounding box that only excludes the specified color. Only works on 8-bit images. More...
 

Detailed Description

Definition at line 12 of file BoundingBoxFinder.cs.

Member Function Documentation

Rectangle ImageResizer.Plugins.WhitespaceTrimmer.BoundingBoxFinder.FindBoxExactGrayscale ( BitmapData  sourceData,
byte  indexToRemove 
)
inline

Returns a bounding box that only excludes the specified color. Only works on 8-bit images.

Parameters
sourceData
colorToRemoveThe palette index to remove.
Returns

Definition at line 83 of file BoundingBoxFinder.cs.

83  {
84  if (sourceData.PixelFormat != PixelFormat.Format8bppIndexed) throw new ArgumentOutOfRangeException("FindBoxExact only operates on 8-bit grayscale images");
85  // get source image size
86  int width = sourceData.Width;
87  int height = sourceData.Height;
88  int offset = sourceData.Stride - width;
89 
90  int minX = width;
91  int minY = height;
92  int maxX = 0;
93  int maxY = 0;
94 
95  // find rectangle which contains something except color to remove
96  unsafe {
97  byte* src = (byte*)sourceData.Scan0;
98 
99  for (int y = 0; y < height; y++) {
100  if (y > 0) src += offset; //Don't adjust for offset until after first row
101  for (int x = 0; x < width; x++) {
102  if (x > 0 || y > 0) src++; //Don't increment until after the first pixel.
103  if (*src != indexToRemove) {
104  if (x < minX)
105  minX = x;
106  if (x > maxX)
107  maxX = x;
108  if (y < minY)
109  minY = y;
110  if (y > maxY)
111  maxY = y;
112  }
113  }
114  }
115  }
116 
117  // check
118  if ((minX == width) && (minY == height) && (maxX == 0) && (maxY == 0)) {
119  minX = minY = 0;
120  }
121 
122  return new Rectangle(minX,minY,maxX - minX + 1, maxY - minY + 1);
123  }
Rectangle ImageResizer.Plugins.WhitespaceTrimmer.BoundingBoxFinder.FindBoxSobel ( Bitmap  originalImage,
Rectangle  lookInside,
byte  threshold 
)
inline

Returns a rectangle inside 'lookInside' that bounds any energy greater than 'threshold'.

Parameters
image
lookInsideA rectangle of 'image' to look inside.
threshold1-255, the energy threshold to detect activity. 80-150 is a good range.
Returns

Definition at line 21 of file BoundingBoxFinder.cs.

21  {
22 
23  Bitmap image = originalImage;
24  try {
25  //Convert if needed (makes an extra copy)
26  if (image.PixelFormat != PixelFormat.Format24bppRgb &&
27  image.PixelFormat != PixelFormat.Format32bppArgb &&
28  image.PixelFormat != PixelFormat.Format32bppRgb) {
29  image = AForge.Imaging.Image.Clone(image, PixelFormat.Format24bppRgb);
30  }
31 
32  //Crop if needed (makes an extra copy unless we converted too, then only 1 extra copy)
33  if (!lookInside.Equals(new Rectangle(0, 0, image.Width, image.Height))) {
34  Bitmap oldImage = image;
35  try {
36  image = new Crop(lookInside).Apply(image);
37  } finally {
38  if (oldImage != originalImage) oldImage.Dispose(); //Dispose the cloned
39  }
40  }
41 
42 
43  //Makes 1 more copy at 1/3rd the size, in grayscale
44  Rectangle result = FindBoxSobel(image, threshold);
45  return new Rectangle(lookInside.X + result.X, lookInside.Y + result.Y, result.Width, result.Height);
46 
47 
48  } finally {
49  if (image != originalImage) image.Dispose();
50  }
51 
52  }
Rectangle FindBoxSobel(Bitmap originalImage, Rectangle lookInside, byte threshold)
Returns a rectangle inside &#39;lookInside&#39; that bounds any energy greater than &#39;threshold&#39;.
Width and height are considered exact values - cropping is used if there is an aspect ratio differenc...
Rectangle ImageResizer.Plugins.WhitespaceTrimmer.BoundingBoxFinder.FindBoxSobel ( Bitmap  rgb,
byte  threshold 
)
inline

Requires 24 bit or 32 bit (A) RGB image.

Parameters
rgb
threshold
Returns

Definition at line 59 of file BoundingBoxFinder.cs.

59  {
60  using (Bitmap gray = Grayscale.CommonAlgorithms.Y.Apply(rgb)) {
61 
62  //Apply sobel operator to grayscale image
63  new SobelEdgeDetector().ApplyInPlace(gray);
64  //Threshold into black and white.
65  new Threshold(threshold).ApplyInPlace(gray);
66  //Trim only exact black pixels
67  // lock source bitmap data
68  BitmapData data = gray.LockBits(new Rectangle(0, 0, gray.Width, gray.Height), ImageLockMode.ReadOnly, gray.PixelFormat);
69  try {
70  return FindBoxExactGrayscale(data, 0);
71  } finally {
72  gray.UnlockBits(data);
73  }
74  }
75  }
Rectangle FindBoxExactGrayscale(BitmapData sourceData, byte indexToRemove)
Returns a bounding box that only excludes the specified color. Only works on 8-bit images...

The documentation for this class was generated from the following file: