on
Italy
- Get link
- X
- Other Apps
PixelFormat.Format8bppIndexed
with color palette of 256 entries. To guarantee that your image is in one of the formats, you can use the following code:// load an image
System.Drawing.Bitmap image = (Bitmap) Bitmap.FromFile( fileName );
// format image
AForge.Imaging.Image.FormatImage( ref image );
It is easy to apply any filter to your image:// load an image
System.Drawing.Bitmap image = (Bitmap) Bitmap.FromFile( fileName );
// create filter
AForge.Imaging.Filters.Median filter = new AForge.Imaging.Filters.Median( );
// apply filter
System.Drawing.Bitmap newImage = filter.Apply( image );
Suppose, you want to apply a series of filters to an image. The straight way to do it is to apply filters one after another, but it's not very likely in the case of 3 or more filters. All filters implement the IFilter
interface, so it allows us to create a collection of filters and apply it at once to an image (besides, the collection will also save us from disposing routines on intermediate images):// create filters sequence AForge.Imaging.Filters.FiltersSequence
filter = new AForge.Imaging.Filters.FiltersSequence( );
// add filters to the sequence
filter.Add( new AForge.Imaging.Filters.Sepia( ) );
filter.Add( new AForge.Imaging.Filters.RotateBilinear( 45) );
filter.Add( new AForge.Imaging.Filters.ResizeBilinear( 320, 240 ) );
filter.Add( new AForge.Imaging.Filters.Pixellate( 8 ) );
filter.Add( new AForge.Imaging.Filters.Jitter( 2 ) );
filter.Add( new AForge.Imaging.Filters.Blur( ) );
// apply the sequence to an image
System.Drawing.Bitmap newImage = filter.Apply( image );
It's easy to get such image statistics as mean, standard deviation, median, minimum and maximum values. It can be useful for image brightness/contrast regulation.// get image statistics
AForge.Imaging.ImageStatistics statistics =
new AForge.Imaging.ImageStatistics( image );
// get the red histogram
AForge.Math.Histogram histogram = statistics.Red;
// get the values
double mean = histogram.Mean; // mean red value
double stddev = histogram.StdDev; // standard deviation of red values
int median = histogram.Median; // median red value
int min = histogram.Min; // min red value
int max = histogram.Max; // max value
// get 90% range around the median
AForge.IntRange range = histogram.GetRange( 0.9 );
Image statistics can be easily combined with filters. Suppose that the minimum value of red is 50 on the image and the maximum value is 200. So, we can normalize the contrast of the red channel:// create levels filter
AForge.Imaging.Filters.LevelsLinear filter =
new AForge.Imaging.Filters.LevelsLinear( );
filter.InRed = new IntRange( histogram.Min, histogram.Max );
// apply the filter
System.Drawing.Bitmap newImage = filter.Apply( image );
Or we can normalize the contrast of each channel, getting only the 90% ranges from each channel:// create levels filter
AForge.Imaging.Filters.LevelsLinear filter =
new AForge.Imaging.Filters.LevelsLinear( );
filter.InRed = statistics.Red.GetRange( 0.9 );
filter.InGreen = statistics.Green.GetRange( 0.9 );
filter.InBlue = statistics.Blue.GetRange( 0.9 );
// apply the filter
System.Drawing.Bitmap newImage = filter.Apply( image );
// create filter
AForge.Imaging.Filters.SaturationCorrection filter =
new AForge.Imaging.Filters.SaturationCorrection( 0.1 );
// apply the filter
System.Drawing.Bitmap newImage = filter.Apply( image );
Initial image |
Saturation adjusted |
// create filter
AForge.Imaging.Filters.HueModifier filter =
new AForge.Imaging.Filters.HueModifier( 142 );
// apply the filter
System.Drawing.Bitmap newImage = filter.Apply( image );
It's possible to get much more interesting results using HSL filtering. For example, we can preserve only the specified range of hue values and desaturate all others out of the range. So, it will lead to a black and white image with only some regions colored.// create filter
AForge.Imaging.Filters.HSLFiltering filter =
new AForge.Imaging.Filters.HSLFiltering( );
filter.Hue = new IntRange( 340, 20 );
filter.UpdateHue = false;
filter.UpdateLuminance = false;
// apply the filter
System.Drawing.Bitmap newImage = filter.Apply( image );
Hue modified |
HSL filtering |
// searching for vertical lines
short[,] vse = new short[3, 3] {
{ 0, 1, 0 },
{ 0, 1, 0 },
{ 0, 1, 0 }
};
AForge.Imaging.Filters.HitAndMiss vFilter =
new AForge.Imaging.Filters.HitAndMiss( vse );
System.Drawing.Bitmap vImage = vFilter.Apply( image );
// searching for horizontal lines
short[,] hse = new short[3, 3] {
{ 0, 0, 0 },
{ 1, 1, 1 },
{ 0, 0, 0 }
};
AForge.Imaging.Filters.HitAndMiss hFilter =
new AForge.Imaging.Filters.HitAndMiss( hse );
System.Drawing.Bitmap hImage = hFilter.Apply( image );
Original image |
Searching for vertical lines |
Searching for horizontal lines |
// create filter
AForge.Imaging.Filters.FilterIterator filter =
new AForge.Imaging.Filters.FilterIterator(
new AForge.Imaging.Filters.HitAndMiss(
new short [,] { { 1, 1, 1 }, { -1, 0, -1 }, { -1, -1, -1 } },
HitAndMiss.Modes.Thinning ), 5 );
// apply the filter
System.Drawing.Bitmap newImage = filter.Apply( image );
Original image |
Thickened image |
// create filter sequence
AForge.Imaging.Filters.FiltersSequence filterSequence =
new AForge.Imaging.Filters.FiltersSequence( );
// add 8 thinning filters with different structuring elements
filterSequence.Add( new AForge.Imaging.Filters.HitAndMiss(
new short [,] { { 0, 0, 0 }, { -1, 1, -1 }, { 1, 1, 1 } },
HitAndMiss.Modes.Thinning ) );
filterSequence.Add( new AForge.Imaging.Filters.HitAndMiss(
new short [,] { { -1, 0, 0 }, { 1, 1, 0 }, { -1, 1, -1 } },
HitAndMiss.Modes.Thinning ) );
filterSequence.Add( new AForge.Imaging.Filters.HitAndMiss(
new short [,] { { 1, -1, 0 }, { 1, 1, 0 }, { 1, -1, 0 } },
HitAndMiss.Modes.Thinning ) );
filterSequence.Add( new AForge.Imaging.Filters.HitAndMiss(
new short [,] { { -1, 1, -1 }, { 1, 1, 0 }, { -1, 0, 0 } },
HitAndMiss.Modes.Thinning ) );
filterSequence.Add( new AForge.Imaging.Filters.HitAndMiss(
new short [,] { { 1, 1, 1 }, { -1, 1, -1 }, { 0, 0, 0 } },
HitAndMiss.Modes.Thinning ) );
filterSequence.Add( new AForge.Imaging.Filters.HitAndMiss(
new short [,] { { -1, 1, -1 }, { 0, 1, 1 }, { 0, 0, -1 } },
HitAndMiss.Modes.Thinning ) );
filterSequence.Add(new AForge.Imaging.Filters.HitAndMiss(
new short [,] { { 0, -1, 1 }, { 0, 1, 1 }, { 0, -1, 1 } },
HitAndMiss.Modes.Thinning ) );
filterSequence.Add( new AForge.Imaging.Filters.HitAndMiss(
new short [,] { { 0, 0, -1 }, { 0, 1, 1 }, { -1, 1, -1 } },
HitAndMiss.Modes.Thinning ) );
// create filter iterator for 10 iterations
AForge.Imaging.Filters.FilterIterator filter =
new AForge.Imaging.Filters.FilterIterator( filterSequence, 10 );
// apply the filter
System.Drawing.Bitmap newImage = filter.Apply( image );
Original image |
Thinned image |
// create complex image from bitmap
AForge.Imaging.ComplexImage cimage =
AForge.Imaging.ComplexImage.FromBitmap( bitmap );
// perform forward Fourier transformation
cimage.ForwardFourierTransform( );
// get frequency view
System.Drawing.Bitmap img = cimage.ToBitmap( );
FrequencyFilter
method of the ComplexImage
class:// lowpass filtering
cimage.FrequencyFilter( new Range( 0, 100 ) );
// perform backward Fourier transformation
cimage.BackwardFourierTransform( );
// get filtered image
System.Drawing.Bitmap img = cimage.ToBitmap( );
// create filter
AForge.Imaging.Filters.ConnectedComponentsLabeling filter =
new AForge.Imaging.Filters.ConnectedComponentsLabeling( );
// apply filter
System.Drawing.Bitmap newImage = filter.Apply( image );
// objects count
System.Diagnostics.Debug.WriteLine( "Objects count: " +
filter.ObjectCount );
Here are two images: initial image and colored image. So, it looks like the filter is really able to count objects.// process an image
AForge.Imaging.BlobCounter blobCounter = new BlobCounter( image );
Rectangle[] rects = blobCounter.GetObjectRectangles( );
// objects count
System.Diagnostics.Debug.WriteLine( "Objects count: " + rects.Length );
// objects dimension
foreach ( Rectangle rc in rects )
{
System.Diagnostics.Debug.WriteLine(
string.Format("Position: ({0}, {1}), Size: {2} x {3}",
rc.Left, rc.Top, rc.Width, rc.Height ) );
}
It's possible to extract each object with the GetObjects
method of BlobCounter
:// process an image
AForge.Imaging.BlobCounter blobCounter = new BlobCounter( image );
Blob[] blobs = blobCounter.GetObjects( image );
// process blobs
foreach ( Blob blob in blobs )
{
// ...
// blob.Location - location of the blob
// blob.Image - blob`s image
}
// create filter
YCbCrLinear filter = new YCbCrLinear( );
filter.InCb = new DoubleRange( -0.276, 0.163 );
filter.InCr = new DoubleRange( -0.202, 0.500 );
// apply filter
filter.ApplyInPlace( image );
Textures
namespace of the library, which contains generators for such effects as clouds, wood, marble, labyrinth and textile. All these texture generators implement the ITextureGenerator
interface. For applying textures to images, there are three filters. The fist one, Texturer
, is for texturing images. The second, TexturedFilter
, allows application of any other filter to an image using the texture as a mask. The third, TexturedMerge
, allows merging of two images using the texture as a mask.// 1 - Marble effect
// create texture
ITextureGenerator generator = new MarbleTexture( );
float[,] texture = generator.Generate( image.Width, image.Height );
// create filter
IFilter filter1 = new Texturer( texture );
// apply filter
Bitmap newImage1 = filter1.Apply( image );
// 2 - Wood effect
// create filter
IFilter filter2 = new Texturer( new WoodTexture( ) );
// apply filter
Bitmap newImage2 = filter2.Apply( image );
// 3 - Textile effect
// create filter
IFilter filter3 = new Texturer( new TextileTexture( ) );
// apply filter
Bitmap newImage3 = filter3.Apply( image );
// 4 - Rusty effect
IFilter filter4 = new TexturedFilter( new CloudsTexture( ),
new Sepia( ) , new GrayscaleBT709( ) );
// apply filter
Bitmap newImage4 = filter4.Apply( image );
Comments
Post a Comment