下面第一個函數就是我想要在第二個函數裡面用的,而第二個函數是第一個引用的
void CZHANGMIN1View::InsertSort(unsigned char pArray, int iLength)
{
unsigned char tmp;
int i, j;
for(i = 1; i < iLength; i++)
{
j = i-1;
tmp = pArray[i];
while(j >= 0 && pArray[j] >= tmp)
{
pArray[j+1] = pArray[j];
j--;
}
pArray[j+1] = tmp;
}//end for i
}
HDIB Mfa(HDIB hDIB,int n)
{
BYTE temp=(BYTE)malloc(n*n*sizeof(BYTE));
//指向圖像起始位置的指針
BYTE *lpDIB=(BYTE)::GlobalLock((HGLOBAL) hDIB);
//指向象素起始位置的指針
BYTE pScrBuff =(BYTE)::FindDIBBits((char*)lpDIB);
//獲取圖像的顏色信息
int numColors=(int) ::DIBNumColors((char *)lpDIB);
//如果圖像不是256色返回
if (numColors!=256)
{
//解除鎖定
::GlobalUnlock((HGLOBAL) hDIB);
//返回
return(hDIB);
}
//將指向圖像象素起始位置的指針,賦值給指針變量
BYTE* oldbuf = pScrBuff;
int w, h, dw;
//獲取圖像的寬度
w = (int) ::DIBWidth((char *)lpDIB);
//獲取圖像的高度
h = (int) ::DIBHeight((char *)lpDIB);
//計算圖像每行的字節數
dw = (w+3)/4*4;
//建立一個和原圖像大小相同的25色灰度位圖
HDIB newhDIB=NewDIB(w,h,8);
//指向新的位圖的指針
BYTE newlpDIB=(BYTE)::GlobalLock((HGLOBAL) newhDIB);
//指向新的位圖的象素起始位置的指針
BYTE destBuf = (BYTE)FindDIBBits((char *)newlpDIB);
//將指向新圖像象素起始位置的指針,賦值給指針變量
BYTE *newbuf=destBuf;
WORD wBitCount;
int m_nBitCount=wBitCount;
if(oldbuf != NULL)
{
delete[] oldbuf;
oldbuf = NULL;
}
int nLineByteOut = (w*m_nBitCount/8 + 3)/4*4;
oldbuf = new unsigned char[nLineByteOut*h];
//濾波窗口的最大值,例如濾波窗口大小從3->5->7->9->11->.......
int iFilterSizeMax = n;
int i, j;
int k;
//給濾波窗口的數組申請空間
unsigned char* pArray = new unsigned char[iFilterSizeMax*iFilterSizeMax];
int Zmin, Zmax, Zmed, Zxy;
int A1, A2, B1, B2;
int iFilterSize;
int W, H;
for(i = 0; i < h; i++)
{
for(j = 0; j < w; j++)
{
for(iFilterSize = 3; iFilterSize <= iFilterSizeMax; iFilterSize += 2)
{
k = 0;
for(H = 0; H < iFilterSize; H++)
{
for(W = 0; W < iFilterSize; W++)
{
if((i-iFilterSize/2+H) >= 0 && (i-iFilterSize/2+H) <= h-1 &&
(j-iFilterSize/2+W) >= 0 && (j-iFilterSize/2+W) <= w-1)
{
pArray[k] = *(oldbuf + (i-iFilterSize/2+H)*nLineByteOut + (j-iFilterSize/2+W));
k++;
}
}
}
//對濾波窗口中的像素排序pDoc->m_hDIB =Mfa(pDoc->m_hDIB,3);
InsertSort(pArray, k);
Zmin = pArray[0];
Zmax = pArray[k-1];
Zmed = pArray[(k-1)/2];
Zxy = *(oldbuf + nLineByteOut*i + j);
A1 = Zmed - Zmin;
A2 = Zmed - Zmax;
//這個if是來判斷濾波窗口裡的像素是否合格來完成濾波,合格則繼續
//不合格就擴大濾波窗口,防止噪聲太密集影響濾波
if(A1 > 0 && A2 < 0)
{
B1 = Zxy - Zmin;
B2 = Zxy - Zmax;
//這個if是來判斷Zxy這點是不是噪聲點的,是噪聲點才要中值替換
if(B1 > 0 && B2 < 0)
{
//不是噪聲點,直接跳出循環
*(oldbuf + nLineByteOut*i + j) = Zxy;
break;
}
else
{
*(oldbuf + nLineByteOut*i + j) = Zmed;
break;
}
}
}//end for iFilterSize
}//end for j
}//end for i
delete[] pArray;
//解除鎖定
::GlobalUnlock((HGLOBAL)hDIB);
//返回新的位圖的句柄
return(newhDIB);
}
void CZHANGMIN1View::OnMfadaptive()
{
//自適應中值濾波
CZHANGMIN1Doc* pDoc = GetDocument();
pDoc->m_hDIB =Mfa(pDoc->m_hDIB,3);
Invalidate(TRUE);
}
每次調試都說Insertsort沒有定義
error C2065: 'InsertSort' : undeclared identifier
Mfa不是 CZHANGMIN1View類的成員函數,不能直接使用InsertSort
可以把Mfa函數改為CZHANGMIN1View類的成員函數
void CZHANGMIN1View::InsertSort(unsigned char *pArray, int iLength)
{
...
}
HDIB Mfa(HDIB hDIB,int n)
{
...
InsertSort(pArray, k);//不能直接使用InsertSort
...
return(newhDIB);
}
void CZHANGMIN1View::OnMfadaptive()
{
//自適應中值濾波
CZHANGMIN1Doc* pDoc = GetDocument();
pDoc->m_hDIB =Mfa(pDoc->m_hDIB,3);
Invalidate(TRUE);
}