Median Filtering for Digital Images

Source: Internet
Author: User

8-Bit Bitmap Median Filtering
For (INT I = 0; I <nheight; I ++)
{
For (Int J = (select-1)/2; j <nwidth-(select-1)/2; j ++)
{
// Obtain the nearest Pixel
Int M =-(select-1)/2;
For (INT n = 0; n <select; n ++)
{
Data [N] = * (image + llinebytes * I + J + M );
M ++;
}
// Sort the acquired pixels
For (int K = 0; k <select; k ++)
{
For (int kk = k + 1; KK <select; KK ++)
{
If (data [Kk] <data [k])
{
Int;
A = data [k];
Data [k] = data [Kk];
Data [Kk] =;
}
}
}
// Set the median value to the pixel value of the vertex.
* (Image + llinebytes * I + J) = data [(select-1)/2];
}
}
24-Bit Bitmap Median Filtering
For (INT I = 0; I <nheight; I ++)
{
For (Int J = (select-1)/2) * 3; j <(nwidth-(select-1)/2) * 3; j ++)
{
// Perform median filter on B.
Int M =-(select-1)/2;
// Obtain the value of B on both sides of J
For (INT n = 0; n <select; n ++)
{
Data [N] = * (image + llinebytes * I + J-2 + M );
M ++;
M ++;
M ++;
}
// Sort the obtained B component in ascending order.
For (int K = 0; k <select; k ++)
{
For (int kk = k + 1; KK <select; KK ++)
{
If (data [Kk] <data [k])
{
Int;
A = data [k];
Data [k] = data [Kk];
Data [Kk] =;
}
}
}
// Set the value of B TO THE INTERMEDIATE VALUE OF THE sorted sequence.
* (Image + llinebytes * I + J) = data [(select-1)/2];
// Perform median filter on G Components
J ++;
M =-(select-1)/2;
// Obtain the value of G on both sides of J
For (n = 0; n <select; n ++)
{
Data [N] = * (image + llinebytes * I + J-2 + M );
M ++;
M ++;
M ++;
}
// Sort the obtained g components in ascending order.
For (k = 0; k <select; k ++)
{
For (int kk = k + 1; KK <select; KK ++)
{
If (data [Kk] <data [k])
{
Int;
A = data [k];
Data [k] = data [Kk];
Data [Kk] =;
}
}
}
// Set the G component value to the intermediate value of the sorted Sequence
* (Image + llinebytes * I + J) = data [(select-1)/2];
// Perform median filter on the R component
J ++;
M =-(select-1)/2;
// Obtain the value of the r component on both sides of J
For (n = 0; n <select; n ++)
{
Data [N] = * (image + llinebytes * I + J-2 + M );
M ++;
M ++;
M ++;
}
// Sort the obtained R component in ascending order
For (k = 0; k <select; k ++)
{
For (int kk = k + 1; KK <select; KK ++)
{
If (data [Kk] <data [k])
{
Int;
A = data [k];
Data [k] = data [Kk];
Data [Kk] =;
}
}
}
// Set the value of the r component to the intermediate value of the sorted Sequence
* (Image + llinebytes * I + J) = data [(select-1)/2];
}
}
Filter effect description
The color of the 24-Bit Bitmap changes as the values of each RGB component are filtered in the median value. However, for an 8-Bit Bitmap, there will be no significant changes due to the gray color, and the larger the filtering window is, the blur of the corresponding filtering effect will be obvious.
8-Bit Bitmap Filter
 
 

24-Bit Bitmap Filter

 

Complete median filter program
Void cicetimdlg: onbtnmedianfilter ()
{
// Todo: add your control notification handler code here
If (m_dib.getbitcount () = 0)
{
Afxmessagebox ("Open the bitmap file first ");
Return;
}
Int select;
Cmedianfilterdlg DLG = new cmedianfilterdlg ();
If (DLG. domodal () = idok)
{
Updatedata ();
Select = DLG. m_select;
}
Else
{
Return;
}
Int nheight = m_dib.getheight ();
Int nwidth = m_dib.getwidth ();
Byte * image = m_dib.getdibdata ();
Long llinebytes = m_dib.getlinebytes ();
Byte * Data = new byte [select];
Beginwaitcursor ();
Int nbitcount = m_dib.getbitcount ();
If (nbitcount = 8)
{
For (INT I = 0; I <nheight; I ++)
{
For (Int J = (select-1)/2; j <nwidth-(select-1)/2; j ++)
{
// Obtain the nearest Pixel
Int M =-(select-1)/2;
For (INT n = 0; n <select; n ++)
{
Data [N] = * (image + llinebytes * I + J + M );
M ++;
}
// Sort the acquired pixels
For (int K = 0; k <select; k ++)
{
For (int kk = k + 1; KK <select; KK ++)
{
If (data [Kk] <data [k])
{
Int;
A = data [k];
Data [k] = data [Kk];
Data [Kk] =;
}
}
}
// Set the median value to the pixel value of the vertex.
* (Image + llinebytes * I + J) = data [(select-1)/2];
}
}
}
Else if (nbitcount = 24)
{
For (INT I = 0; I <nheight; I ++)
{
For (Int J = (select-1)/2) * 3; j <(nwidth-(select-1)/2) * 3; j ++)
{
// Perform median filter on B.
Int M =-(select-1)/2;
// Obtain the value of B on both sides of J
For (INT n = 0; n <select; n ++)
{
Data [N] = * (image + llinebytes * I + J-2 + M );
M ++;
M ++;
M ++;
}
// Sort the obtained B component in ascending order.
For (int K = 0; k <select; k ++)
{
For (int kk = k + 1; KK <select; KK ++)
{
If (data [Kk] <data [k])
{
Int;
A = data [k];
Data [k] = data [Kk];
Data [Kk] =;
}
}
}
// Set the value of B TO THE INTERMEDIATE VALUE OF THE sorted sequence.
* (Image + llinebytes * I + J) = data [(select-1)/2];
// Perform median filter on G Components
J ++;
M =-(select-1)/2;
// Obtain the value of G on both sides of J
For (n = 0; n <select; n ++)
{
Data [N] = * (image + llinebytes * I + J-2 + M );
M ++;
M ++;
M ++;
}
// Sort the obtained g components in ascending order.
For (k = 0; k <select; k ++)
{
For (int kk = k + 1; KK <select; KK ++)
{
If (data [Kk] <data [k])
{
Int;
A = data [k];
Data [k] = data [Kk];
Data [Kk] =;
}
}
}
// Set the G component value to the intermediate value of the sorted Sequence
* (Image + llinebytes * I + J) = data [(select-1)/2];
// Perform median filter on the R component
J ++;
M =-(select-1)/2;
// Obtain the value of the r component on both sides of J
For (n = 0; n <select; n ++)
{
Data [N] = * (image + llinebytes * I + J-2 + M );
M ++;
M ++;
M ++;
}
// Sort the obtained R component in ascending order
For (k = 0; k <select; k ++)
{
For (int kk = k + 1; KK <select; KK ++)
{
If (data [Kk] <data [k])
{
Int;
A = data [k];
Data [k] = data [Kk];
Data [Kk] =;
}
}
}
// Set the value of the r component to the intermediate value of the sorted Sequence
* (Image + llinebytes * I + J) = data [(select-1)/2];
}
}
}
Else
{
Afxmessagebox ("only 8 or 24-Bit Bitmap can be processed temporarily ");
Return;
}
 
Showimage (m_dib, "median filter ");
Endwaitcursor ();
}

 

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.