Σχόλια 0

Το κείμενο του εγγράφου

Image Processing Lab in C# - The Code Project - Multimedia http://www.codeproject.com/cs/media/Image_Processing_Lab.asp?prin...2 di 10 05/09/2006 09:50Mathematical morphology filters (erosion, dilatation, opening, closing, hit & miss, thinning, thickening);Convolution filters (mean, blur, sharpen, edges, Gaussian);2 Source filters (merge, intersect, add, subtract, difference, move towards, morph);Edge detectors (homogeneity, difference, sobel, canny);Blob counter, connected components labeling;Pixellate, simple skeletonization, jitter, shrink, oil painting;Levels linear filter, gamma correction;Median filter, adaptive smoothing, conservative smoothing;Resize and rotate;Texture generators based on Perlin noise;Texture filters (texturer, textured filtering, textured merging);Fourier transformation (lowpass and hipass filters).You can create (save and load) your own convolution filters, or filters based on standard mathematical morphology operators. The colorized grid makes it very convenient to work with custom convolution filters.A preview window allows you to view results of changing the filter parameters on the fly. You can scroll an image using the mouse in the preview area. All filters are applied only to a portion of the image currently viewed, to speed up preview.A PhotoShop like histogram allows you to get information about the mean, standard deviation, median, minimum, and maximum values.The program allows to copy to or paste from clipboard, save and print images.Using the codeMost filters are designed to work with 24bpp RGB images, or with grayscale images. In the case of grayscale images, we usePixelFormat.Format8bppIndexedwith a color palette of 256 entries. Toguarantee that your image is in one of the formats, you can use the following code:It is easy to apply any filter to your 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 theIFilterinterface, so it allows us to create a collection of filters and apply it at once to an image(besides, the collection also will save us from disposing routines on intermediate images):It's easy to get such image statistics as mean, standard deviation, median, minimum, and maximum// load an imageSystem.Drawing.Bitmap image = (Bitmap) Bitmap.FromFile(fileName);// format imageAForge.Imaging.Image.FormatImage(refimage);// load an imageSystem.Drawing.Bitmap image = (Bitmap) Bitmap.FromFile(fileName);// create filterAForge.Imaging.Filters.Median filter =newAForge.Imaging.Filters.Median();// apply filterSystem.Drawing.Bitmap newImage = filter.Apply(image);// create filters sequenceAForge.Imaging.Filters.FiltersSequence filter =newAForge.Imaging.Filters.FiltersSequence();// add filters to the sequencefilter.Add(newAForge.Imaging.Filters.Sepia());filter.Add(newAForge.Imaging.Filters.Rotate(45));filter.Add(newAForge.Imaging.Filters.Resize(320,240));filter.Add(newAForge.Imaging.Filters.Pixellate(8));filter.Add(newAForge.Imaging.Filters.Jitter(2));filter.Add(newAForge.Imaging.Filters.Blur());// apply the sequence to an imageSystem.Drawing.Bitmap newImage = filter.Apply(image);Image Processing Lab in C# - The Code Project - Multimedia http://www.codeproject.com/cs/media/Image_Processing_Lab.asp?prin...3 di 10 05/09/2006 09:50values. It can be useful for image brightness/contrast regulation.Image statistics can be easily combined with filters. Suppose, 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:Or we can normalize the contrast of each channel, getting only 90% ranges from each channel:HSL filtersUsing HSL color space is more obvious for some sort of filters. For example, it's not very clean how to adjust the saturation level of an image using the RGB color space. But it can be done easily, using the HSL color space:Initial imageSaturation adjusted// get image statisticsAForge.Imaging.ImageStatistics statistics =newAForge.Imaging.ImageStatistics(image);// get the red histogramAForge.Math.Histogram histogram = statistics.Red;// get the valuesfloatmean = histogram.Mean;// mean red valuefloatstddev = histogram.StdDev;// standard deviation of red valuesintmedian = histogram.Median;// median red valueintmin = histogram.Min;// min red valueintmax = histogram.Max;// max values// get 90% range around the medianAForge.Math.Range range = histogram.GetRange(0.9);// create levels filterAForge.Imaging.Filters.LevelsLinear filter =newAForge.Imaging.Filters.LevelsLinear();filter.InRed =newRange(histogram.Min, histogram.Max);// apply the filterSystem.Drawing.Bitmap newImage = filter.Apply(image);// create levels filterAForge.Imaging.Filters.LevelsLinear filter =newAForge.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 filterSystem.Drawing.Bitmap newImage = filter.Apply(image);// create filterAForge.Imaging.Filters.SaturationCorrection filter =newAForge.Imaging.Filters.SaturationCorrection(0.1);// apply the filterSystem.Drawing.Bitmap newImage = filter.Apply(image);Image Processing Lab in C# - The Code Project - Multimedia http://www.codeproject.com/cs/media/Image_Processing_Lab.asp?prin...4 di 10 05/09/2006 09:50Using the HSL color space, we can modify the hue value of pixels. Setting all hue values to the same value will lead to an image in gradations of one color: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.Hue modifiedHSL filteringMathematical morphology filtersThere are many tasks which can be solved using mathematical morphology filters. For example, we can reduce the noise on binary images using erosion, or we can separate some objects with the filter. Using dilatation, we can grow some parts of our interests on the image. One of the most interesting morphological operators is known as Hit & Miss. All other morphological operators can be expressed from the Hit & Miss operator. For example, we can use it to search for particular structures on the image:// create filterAForge.Imaging.Filters.HueModifier filter =newAForge.Imaging.Filters.HueModifier(142);// apply the filterSystem.Drawing.Bitmap newImage = filter.Apply(image);// create filterAForge.Imaging.Filters.HSLFiltering filter =newAForge.Imaging.Filters.HSLFiltering();filter.Hue =newRange(340,20);filter.UpdateHue =false;filter.UpdateLuminance =false;// apply the filterSystem.Drawing.Bitmap newImage = filter.Apply(image);// searching for vertical linesshort[,] vse =new

short[3,3] {{0,0,0},{1,1,1},{0,0,0}};AForge.Imaging.Filters.HitAndMiss hFilter =newAForge.Imaging.Filters.HitAndMiss(hse);System.Drawing.Bitmap hImage = hFilter.Apply(image);Image Processing Lab in C# - The Code Project - Multimedia http://www.codeproject.com/cs/media/Image_Processing_Lab.asp?prin...5 di 10 05/09/2006 09:50Original imageSearching for vertical linesSearching for horizontal linesUsing the thickening operator, we can grow some parts of the image in the places we need. For example, the following sample will lead to thickening horizontal lines in the bottom direction:Original imageThickened imageUsing the thinning operator, you can remove some unnecessary parts of the image. For example, you can develop a skeletonization filter with appropriate structuring elements:// create filterAForge.Imaging.Filters.FilterIterator filter =newAForge.Imaging.Filters.FilterIterator(

short[,] {{0,0, -1}, {0,1,1}, {-1,1, -1}}));// create filter iterator for 10 iterationsAForge.Imaging.Filters.FilterIterator filter =newAForge.Imaging.Filters.FilterIterator(filterSequence,10);// apply the filterSystem.Drawing.Bitmap newImage = filter.Apply(image);Image Processing Lab in C# - The Code Project - Multimedia http://www.codeproject.com/cs/media/Image_Processing_Lab.asp?prin...6 di 10 05/09/2006 09:50Original imageThinned imageFourier transformationsIt is easy to perform a Fourier transformation, which is useful for image analysis and filtering with the library:Lowpass and hipass filtering can be performed using theFrequencyFiltermethod of theComplexImageclass:Blob counterBlob counter is a very useful feature, and can be applied in many different applications. What does it do? It can count objects on a binary image and extract them. The idea comes from 'Connected components labeling', a filter which colors each separate object with a different color. Let's look into a small sample:// create complex image from bitmapAForge.Imaging.ComplexImage cimage = AForge.Imaging.ComplexImage.FromBitmap(bitmap);// perform forward Fourier transformationcimage.ForwardFourierTransform();// get frequency viewSystem.Drawing.Bitmap img = cimage.ToBitmap();// lowpass filteringcimage.FrequencyFilter(newRange(0,100));// perform backward Fourier transformationcimage.BackwardFourierTransform();// get filtered imageSystem.Drawing.Bitmap img = cimage.ToBitmap();Image Processing Lab in C# - The Code Project - Multimedia http://www.codeproject.com/cs/media/Image_Processing_Lab.asp?prin...7 di 10 05/09/2006 09:50Here are two images: the initial image and the colored image. So, it looks like the filter is really able to count objects.Here is another example of objects counting and retrieving their position and size:It's possible to extract each object with theGetObjectsmethod ofBlobCounter:YCbCr filteringYCbCr filters provides similar functionality as RGB and HSL filters. The YCbCr linear correction filter performs as its analogues from other color spaces, but operates with the Y, Cb, and Cr components, respectively, providing additional convenient ways of color correction. The following small sample demonstrates the use of the YCbCr linear filter and the use of in-place filtering - a feature which allows you to filter source images instead of creating a new result image:// create filterAForge.Imaging.Filters.ConnectedComponentsLabeling filter =newAForge.Imaging.Filters.ConnectedComponentsLabeling();// apply filterSystem.Drawing.Bitmap newImage = filter.Apply(image);// objects countSystem.Diagnostics.Debug.WriteLine("Objects count: "