Graphics-noise reduction technology-2-dimensional medium value filtering

Source: Internet
Author: User
Tags bmp image

Guidance:
In the image collection process, the instability of electronic devices (such as glitch voltage and electromagnetic interference) may affect the obtained images. This effect is called noise, noise reduction technology is a technology used to eliminate such noise impact in image processing. Two-dimensional median filtering is one of the methods.
Median Filter is a local image smoothing technique. It is a non-linear filter. It can be a one-dimensional or two-dimensional image, because the image is a two-dimensional Pixel matrix, so here we use the 2-Dimension Medium value filter.
The Two-Dimension Medium-value filtering algorithm is:
For the pixel matrix of an image, a sub-matrix window centered on the target pixel can be 3x3, 5x5, and so on, sorts pixels in the window in grayscale order, and takes a value in the middle as the new gray value of the target pixel.
Window example
Ooo
Oxo
Ooo
The above X is the target pixel, which is a 3*3 matrix array consisting of the surrounding O, and then sorts the gray scale of the nine elements, the new gray-scale value of array [4], the intermediate element after sorting, completes the median filtering of object prime X, and then iteratively filters other required pixels.
Based on this theory, we use bcb6 to implement the median filter algorithm.
// ---------------------------------- Bcb6 Program
# Include
# Pragma hdrstop
# Include
# Include "unit1.h"
# Include "file1.h"
# Pragma pack (1)
/*
Program: Graphics-Median Filtering
Author: sboom (lingch)
Date: January 1, December 26
*/
// BMP File Header
Struct bitmapfileheader _
{
Short type;
Int bfsize;
Short RE1, re2;
Int offbits;
};
// BMP Header
Struct bitmapinfo _
{
Long size;
Long width, height;
Short planes, bitcount;
Long comp, sizeimg;
Long xpels, ypels;
Long used, important;
};
// BMP color table item
Struct color _
{
Char blue, green, red;
};
// ------ Correct the data in the BMP color table to the BCB tcolor data.
Tcolor * switchcolor (unsigned char R, unsigned char g, unsigned char B)
{
Tcolor * Re = new tcolor;
* Re = (r | G * Re = * re & 0x00ffffff;
Return re;
}
Void XXX ()
{
File * f = fopen ("F: // 7.bmp", "rb ");
If (F = NULL)/* determines whether the file is successfully opened */
{
Showmessage ("file open error ");
Return;
}
Fseek (F, 0, 0); // move to the beginning
// ---------- Read the BMP File Header
Bitmapfileheader _ * bmp h = new bitmapfileheader _();
If (fread (char *) bmp h, sizeof (bitmapfileheader _), 1, F) = NULL)
{
Showmessage ("file read error ");
Return;
}
// ----------- Read the BMP Header
Bitmapinfo _ * BMP I = new bitmapinfo _();
If (fread (char *) bmp I, sizeof (bitmapinfo _), 1, F) = NULL)
{
Showmessage ("File Read error2 ");
Return;
}
Fseek (F, bmp h-> offbits, 0 );
// ---------- Display some information
Form1-> edit1-> text = inttostr (bmp h-> bfsize );
Form1-> edit2-> text = inttostr (bmp I-> width );
Form1-> edit3-> text = inttostr (bmp I-> height );
Form1-> edit4-> text = inttostr (bmp I-> comp );
Form1-> edit5-> text = inttostr (bmp I-> used );
Int I, J, K, L, WC, Pos;
Long n = bmp h-> bfsize-bmp h-> offbits; // The total number of pixels.
Color _ * image = new color _ [N]; // Bitmap matrix
Color _ * newimage = new color _ [N]; // The filtered Bitmap matrix.
Fread (image, N * 3, 1, F); // read the Bitmap matrix
// --------- Copy the first line of the original image that is not processed first, because the window cannot be obtained from the first line.
Memcpy (void *) newimage, (void *) image, (bmp I-> width + 1) * 3 );
//--!!!!!!!!!!!!!! The START window below is a 3 × 3 median filter !!!!!!!!!!!!!!!!
Int NS = bmp I-> width + 1; // start point of start processing
Int Ne = N-bmp I-> width-2; // end point of start processing
Unsigned char * PSR = new unsigned char [9]; // red window
Unsigned char * PSG = new unsigned char [9]; // green window
Unsigned char * PSB = new unsigned char [9]; // blue window
Unsigned char temp;
Color _ cc;
For (I = ns; I {
// --------- Red window
// --- 3*3 the first line of the window Matrix
PSR [0] = image [I-bmp I-> width + 1]. Red;
PSR [1] = image [I-bmp I-> width + 1]. Red;
PSR [2] = image [I-bmp I-> width + 1]. Red;
// --- 3*3 the second row of the window Matrix
PSR [3] = image [I-1]. Red;
PSR [4] = image [I]. Red;
PSR [5] = image [I + 1]. Red;
// --- 3*3. The third row of the window Matrix
PSR [6] = image [I + bmp I-> width-1]. Red;
PSR [7] = image [I + bmp I-> width]. Red;
PSR [8] = image [I + bmp I-> width + 1]. Red;
// --------- Green window
// --- 3*3 the first line of the window Matrix
PSG [0] = image [I-bmp I-> width + 1]. Green;
PSG [1] = image [I-bmp I-> width + 1]. Green;
PSG [2] = image [I-bmp I-> width + 1]. Green;
// --- 3*3 the second row of the window Matrix
PSG [3] = image [I-1]. Green;
PSG [4] = image [I]. Green;
PSG [5] = image [I + 1]. Green;
// --- 3*3. The third row of the window Matrix
PSG [6] = image [I + bmp I-> width-1]. Green;
PSG [7] = image [I + bmp I-> width]. Green;
PSG [8] = image [I + bmp I-> width + 1]. Green;
// --------- Blue window
// --- 3*3 the first line of the window Matrix
PSB [0] = image [I-bmp I-> width + 1]. blue;
PSB [1] = image [I-bmp I-> width + 1]. blue;
PSB [2] = image [I-bmp I-> width + 1]. blue;
// --- 3*3 the second row of the window Matrix
PSB [3] = image [I-1]. blue;
PSB [4] = image [I]. blue;
PSB [5] = image [I + 1]. blue;
// --- 3*3. The third row of the window Matrix
PSB [6] = image [I + bmp I-> width-1]. blue;
PSB [7] = image [I + bmp I-> width]. blue;
PSB [8] = image [I + bmp I-> width + 1]. blue;
// -------- Select sorting
For (j = 0; J {
// ----------- Red sorting
Pos = J;
For (k = J; k {
If (PSR [k] Pos = K;
}
Temp = PSR [J];
His/her [J] = his/her [POS];
PSR [POS] = temp;
// -------------- Green sorting
Pos = J;
For (k = J; k {
If (PSG [k] Pos = K;
}
Temp = PSG [J];
PSG [J] = PSG [POS];
PSG [POS] = temp;
// -------------- Blue sorting
Pos = J;
For (k = J; k {
If (PSB [k] Pos = K;
}
Temp = PSB [J];
PSB [J] = PSB [POS];
PSB [POS] = temp;
}
// ------ Obtain the value
Newimage [I]. Red = PSR [4];
Newimage [I]. Green = PSG [4];
Newimage [I]. Blue = PSB [4];
}
//!!!!!!!!!!! After filtering, copy cannot filter the last row, because the last row cannot take the window !!!!!!!
Memcpy (void *) & image [Ne + 1], (void *) & image [Ne + 1], (bmp I-> width + 1) * 3 );
// ------ Display image
Color _ color;
Tcolor * TC;
If (bmp I-> width % 4 = 0) // ----------- the BMP image is 4-byte aligned.
WC = bmp I-> width/4*4;
Else
WC = (bmp I-> width/4 + 1) * 4;
Pos = 0;
For (I = 0; I Height; I ++)
{
For (j = 0; j {
// ----- Original Image
Color = image [POS];
TC = switchcolor (color. Red, color. Green, color. Blue );
Form1-> canvas-> pixels [10 + J] [600-i] = * TC;
// ------ New Image
Color = newimage [POS];
TC = switchcolor (color. Red, color. Green, color. Blue );
Form1-> canvas-> pixels [400 + J] [600-i] = * TC;
Pos ++;
}
}
Fclose (f );
}
//-------------------------------------------------------------------------
After actual operation, it is confirmed that the median filter can effectively remove noise points in the image, especially in areas with continuous changes and easing (such as human clothes and skin ), almost 100% remove the gray Burst Point (which can be considered as a noise point). Because of this, the median filter is not suitable for images with many details, such as vertices and lines of detail, because details may be removed as noise points.
The window of the median filter can also have multiple shapes. The above program selects a rectangle (which is easy to calculate). In fact, the window can also be a diamond, circle, cross, etc, different window shapes have different filtering effects. rectangular or prototype windows are suitable for objects with slow and long contour lines. Cross-shaped windows are suitable for images with sharp objects.
The median filter can be linearly combined, and filters of different window shapes can be linearly combined.
G (x, y) = E {k = 1, n} AK * medk (f (x, y) EDIT link, left E {k = 1, n} indicates that the subscript K is added from 1 to n, AK is the weight coefficient of each filter, and med is the filter function. In fact, the pixel gray scale is added by all filters. The sum of the AK values of each filter should be 1.
Median Filtering is accompanied by obfuscation.

This article is transferred from
Http://3pcode.com/code/2006/09/80704.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.