Spatial field filter (1) - implementation using GDI (C )
Spatial Filtering
(NewSuppy, this article is the job of image processing courses)
1 Smooth linear filter (or average mean filter) Smoothing (average) Filter
Using a Mask of M * N (note: M and N must be greater than 1 odd), override over each pixel to process the image, and the mask center is in combination with the pixel. Each element on the mask has its own weight (a special mean filter has a total mask weight of 1, or a cartridge filter). The weight value of the mask and the pixel values covered below are each multi-pass, and the resulting results are referred to as a response, and the resulting results are referred to as a response, assigned to the current processing.
2 Medium value filter Median Filter
Also uses a M-N mask to handle each pixel, but the mask is free, but the pixel value under the mask cover is sorted and the intermediate value is assigned to the currently processed pixel.
3 processing details
When the mask is in the edge of the image, it will encounter a case where the non-image portion is covered, and there can be a variety of ways to handle, more common partial filtering and complement method (ZERO-PADDING). Partial filtering is part of the image boundary when the image boundary is encountered, and the boundary pixel is processed by partial mask. The complement method is to supplement the image edge portion corresponding to the zero value of the mask size (or other common value), and then remove this part after the processing is complete, restore to the original image size, but such image edge tends to show ash The case where the degree value is small. Generally speaking, the image edge is often a part of the unimportant, so the disadvantages of these processing can be tolerated.
4 illustration process (program implementation process)
(1) Get an original image that is ready to filter, the size is width * height; preparing a M * N size filter mask requires M, n must be a odd number greater than 1, if it is an even number, automatic plus 1 change to odd digits ( 2) Copy the original image and use the zero-padding to make up the image edge of the copy, make up (m-1) / 2 pixels in both sides, the height is 2 (N-1) / 2 Pixels, the image size after copying is changed to (Width M-1) * (Height N-1). This step is not directly added to the original image because the image processing takes the original value of the neighborhood of each pixel, so after the previous pixel is processed, the original value to be retained is the process of the next pixel. (3) Cover the mask on the copy image, sequentially handle each pixel (non-compensation pixel). (a) For a mean filter, the pixel values under the mask cover are multiplied by the weight of the mask, respectively, and finally the product of the product and divide the weight and the resulting response, assignment to the current Processing pixels. In case of a special full-value mask, it is only necessary to make the pixel value under covering the mask to be covered by M * n, ie in response. (b) For the median filter, the pixel value under the mask cover is taken out into a sequence of M * N elements, and the median value is sorted, that is, the response, assigning the current processing pixel. (4) Complete the processing, delete the copy image.
5 program implementation
The program uses GDI (C ) implementation, and the Bitmap class in GDI provides LockBits and UnlockBits methods to operate BitmapData classes, which can directly operate the image stream of the image to achieve higher performance.
code show as below:
// Author: newsuppy // e-mail: newsuppy@msn.com // Date: Nov. 1, 2004 // Version: V0.1.0 (IT INCLUDE IN NewSuppy's Image Process Toolkit) // Declaration: You Can Copy & RewiRte The CODE yourself, but please put me in your acknowledgments #ifndef SPATIALFILTER_H #define SPATIALFILTER_H #include
Tsupport all the pixelformat define in gdiplus // | supported pixelformat | pixel type | // 1, pixelformat8bppindexed --------------------- Unsigned char (8bit) // 2, Pixelformat16bppargb1555 --------------- unsigned short int (16bit) // 3, Pixelformat16bppgrayscale ---------------- Unsigned Short INT (16bit) // 4, Pixelformat16bppRGB555 ---------------- Unsigned Short Int (16bit) // 5, Pixelformat16bppRGB565 -------------- - unsigned short int (16bit) //6, Pixelformat32bppargb ----------------- unsigned int (32bit) //6, Pixelformat32bpppargb ---------- ------- Unsigned int (32bit) // 6, Pixelformat32bppRGB ---------------- unsigned int (32bit) // Filtermask: Filtermask is a functo r, it should inherit class "__filterMask" // and it must support member function "response" like this: // pixelType :: FilterMask response (); // function parameter: // p_bitmap: a pointer to Gdiplus :: Bitmap class // filtermask: The filtermask's instance // x, y, width: The Rectangle Which Should Be Process // (x Y) Are the Left-Up Point of the Rectangle // Return Value: IF Failed, Return False; Else If Success, Return True; Template <
typename pixelType, Gdiplus :: PixelFormat pixelFormat, class FilterMask> bool SpatialFilterAlgo (Gdiplus :: Bitmap * const p_bitmap, FilterMask filterMask, unsigned int x, unsigned int y, unsigned int width, unsigned int height) {if (p_bitmap == NULL) {RETURN FALSE;} IF ((width x> p_bitmap-> getWidth ()) || (Height Y> p_bitmap-> getHeight ())) {Return False;} gdiplus :: bitmapdata bitmapdata; gdiplus :: Rect Rect (x, y, width, height); if (! p_bitmap-> LockBits (& rect, Gdiplus :: ImageLockModeWrite, pixelFormat, & bitmapData) = Gdiplus :: Ok) {return false;} pixelType * pixels = (pixelType *) bitmapData. Scan0; const unsigned int m = filtermask.d_m; // mask? Width const unsigned int n = filtermask.d_n; // mask's height std :: vector Row * bitmapdata.stride / sizeof (pixeltype)];}} // process every pixel with filtermask for (unsigned int = 0; row 1), d_mask (d_m * d_n) {}}; // special averaging (smoothing) filter mask, its' weights are all 1 template PixelType> {public: MedianFiltermask (unsigned int m, unsigned int N): __filtermask The algorithm is included in the Namespace NSIMGTK. The method is as follows: // Create a Bitmap class instance bitmap bitmapwith39Masksp (L "image / fig3.35 (a) .jpg"); // Create a 3X3 mean filter // Note For the AveraGingFiltermask class, its constructor must pass the third parameter Enter a std :: vector as mask weight, size, and mask size. // 如: std :: Vector legend: (1) The following images are imaged on the left --- original image, the right image --- 9x9 mean filter (box filter) processing. The details in the figure are blurred and mixed with the background. (2) The following images are imaged on the left --- original image, the image ---- 3X3 median filter processing. After processing, you can obviously see the pretzer noise is filtered. 6 Conclusion The role of the average filter is to blur image in order to obtain a general description of the object of interest. The reason is that each pixel is its neighboring pixel weight, so that each pixel and other neighborhood pixels are fused, and the degree of blur is also increased as the mask increases. The mode of median filter is mainly to eliminate pretzer noise, the so-called pepper noise is a widely distributed black and white point in the image, and the maximum minimum value of the grayscale is effectively eliminated by taking the median in the neighborhood. It is not appropriate to take too much mask, which will cause image details to be blurred while filtering the pretzer noise. 7 description This is a job that is referred to in the