Spatial field filter (1)

xiaoxiao2021-03-06  101

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 #include #include #include namespace nsimgtk {// function: filter a width * height rectangle Portion of a bitmap One Pixel by One Pixel with the filtermask // template parameter: // pixeltype: Pixel's Type Depend On Pixelformat // Pixelformat: Pixel's Format, But this function doesn '

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 tmpimage ((m-1 width) * (N-1 height)); // Extend Image to use Zero-Padding / / COPY Original Bitmap to Extended Image With Zero-PaddingMETHOD for (unsigned int Row = 0; ROW

Row * bitmapdata.stride / sizeof (pixeltype)];}} // process every pixel with filtermask for (unsigned int = 0; row UnlockBits (& bitmapData) = Gdiplus :: Ok) { return false;} return true;} // base class for filterMask, be only used for the library // others filterMask should inherit it template struct __filterMask {const unsigned int d_m; const unsigned int d_n; std :: vector D_Mask; // Filter Mask's Width and Heigh Must Be A ODD, IF NOT, IT WILL PLUS One for the Width or THE HEIGHT __FILTERMASK (Unsigned Int M, Unsigned Int N): D_M (M% 2? M: M 1), D_N (N% 2? N: N

1), d_mask (d_m * d_n) {}}; // special averaging (smoothing) filter mask, its' weights are all 1 template class averagingFilterMaskSp: public __filterMask {public: averagingFilterMaskSp (unsigned int m Unsigned int N): __filtermask (M, N) {} PixelType Response () {Return std :: accumulate (d_mask.begin (), d_mask.end (), 0) / (d_m * d_n);} }; // averaging (smoothing) filter mask template class averagingFilterMask: public __filterMask {private: std :: vector d_weight; // weights' vector (m * n) int d_weight_sum; // All weights' sum public: AveraGingFiltermask (unsigned int m, unsigned int n, const st :: vector & weightvec): __filtermask (M, N), D_ weight (weightVec) {if (weightVec.size ()! = d_mask.size ()) {// if weight's size is not equal to mask's size, it will change filter mask as a special filter mask d_weight.resize (d_mask. Size (), 1);} D_weight_sum = std :: accumulate (D_Weight.begin (), D_Weight.end (), 0);} PixelType Response () {Return std :: inner_product (D_Mask.Begin (), D_Mask. End (), d_weight.begin (), 0) / d_weight_sum;}}; // median filter mask template Class Medianfiltermask: public__filtermask <

PixelType> {public: MedianFiltermask (unsigned int m, unsigned int N): __filtermask (m, n) {} PixelType Response () {std :: sort (D_Mask.begin (), D_Mask.end ()); RETURN D_MASK [D_MASK.SIZE () / 2];}};} #ENDIF Description: SpatialFilteralGO function is a filter processing algorithm frame; AveRagingFiltermask and AveRagingFiltermasksp are a mean filter, the latter mask power is all 1; MedianFiltermask is a median value filter.

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 weight (9); // weight [0] = 1, weight [1] = 2, weight [2] = 1, // Weight [0] = 2, Weight [1] = 4, Weight [2] = 2, // Weight [0] = 1, Weight [1] = 2, Weight [2] = 1; // AveraGingFiltermask avgfiltermask3x3 (3, 3, weight ); AveRagingFiltermasksp avgfiltermasksp3x3 (3, 3); // Using the SpatialFilteralGO algorithm, the first parameter is the above BitMap class instance, because the image is 8-order gray graph, the first Unsigned Char of the template parameter, generally 8 // third template parameter PixelFormat8bppIndexed (GDI reference document), the second parameter is a function of the object mask filter, the other parameters are the image coordinates of the starting point and the width and height SpatialFilterAlgo (& bitmapWith3x3MaskSp, avgfilterMaskSp3x3 , 0, 0, Bitmapwith3Masksp.GetWidth (), Bitmapwith3x3masksp.getHeight ()) == false)

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 2nd, the original Rafael C. Gonzalez and Richard E. Woods, the translator is Qiu Qiuqi, Qi Yu Zhi, Machinery Industry Press. Thanks to them, translate this outstanding book.

转载请注明原文地址:https://www.9cbs.com/read-97174.html

New Post(0)