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 ();
}