📄 morphology.cpp
字号:
int i,j;
unsigned char *temp;
temp=new BYTE[iNewHeight*nWidthBytes];
for(i=0;i<iNewHeight;i++)
{
for(j=0;j<iNewWidth;j++)
{
if (pOldBits[(iNewHeight-1-i)*nWidthBytes+j]!=0 && pOldBits[(iNewHeight-1-i)*nWidthBytes+j]!=255)
{
MessageBox("此运算要求只由0和255这两个灰度级构成图象");
return FALSE;
}
temp[(iNewHeight-1-i)*nWidthBytes+j]=pOldBits[(iNewHeight-1-i)*nWidthBytes+j];
}
}
for(int w=0;w<25;w++)
{
temp=OnThining(temp,iNewHeight,iNewWidth,nWidthBytes);
}
for(i=0;i<iNewHeight;i++)
{
for(j=0;j<iNewWidth;j++)
{
pNewBits[(iNewHeight-1-i)*nWidthBytes+j]=temp[(iNewHeight-1-i)*nWidthBytes+j];
}
}
delete temp;
temp=NULL;
}
break;
case 6://粗化
{
int i,j;
unsigned char *temp;
temp=new BYTE[iNewHeight*nWidthBytes];
for(i=0;i<iNewHeight;i++)
{
for(j=0;j<iNewWidth;j++)
{
if (pOldBits[(iNewHeight-1-i)*nWidthBytes+j]==0)
{
temp[(iNewHeight-1-i)*nWidthBytes+j]=255;
}
else
{
temp[(iNewHeight-1-i)*nWidthBytes+j]=0;
}
}
}
for(int w=0;w<25;w++)
{
temp=OnThining(temp,iNewHeight,iNewWidth,nWidthBytes);
}
for(i=0;i<iNewHeight;i++)
{
for(j=0;j<iNewWidth;j++)
{
if (temp[(iNewHeight-1-i)*nWidthBytes+j]==0)
{
pNewBits[(iNewHeight-1-i)*nWidthBytes+j]=255;
}
else
{
pNewBits[(iNewHeight-1-i)*nWidthBytes+j]=0;
}
}
}
}
break;
case 7://骨架化
{
int i,j;
int temp =1;
BYTE *pTemp1 = NULL,*pTemp2 = NULL,*pTemp3=NULL;
pTemp1 = new BYTE[iHeight*nWidthBytes];
pTemp2 = new BYTE[iHeight*nWidthBytes];
pTemp3 = new BYTE[iHeight*nWidthBytes];
for (j=2; j<iHeight-2; j++)
{
for (i=2; i<iWidth-2; i++)
{
pTemp1[(iHeight-1-j)*nWidthBytes + i] = pOldBits[(iHeight-1-j)*nWidthBytes + i];
pTemp2[(iHeight-1-j)*nWidthBytes + i] = pOldBits[(iHeight-1-j)*nWidthBytes + i];
pNewBits[(iHeight-1-j)*nWidthBytes + i] = 255;
pTemp3[(iHeight-1-j)*nWidthBytes + i]=255;
}
}
pTemp2 = OnErosion(pTemp2,iHeight,iWidth,nWidthBytes);
pTemp2 = OnDilation(pTemp2,iHeight,iWidth,nWidthBytes);
for(i=0;i<iNewHeight;i++)
{
for(j=0;j<iNewWidth;j++)
{
if ((pTemp1[(iNewHeight-1-i)*nWidthBytes+j]-pTemp2[(iNewHeight-1-i)*nWidthBytes+j])==-255)
{
pNewBits[(iNewHeight-1-i)*nWidthBytes+j]=0;
}
}
}
for(i=0;i<iNewHeight;i++)
{
for(j=0;j<iNewWidth;j++)
{
pTemp2[(iNewHeight-1-i)*nWidthBytes+j]=pOldBits[(iNewHeight-1-i)*nWidthBytes+j];
}
}
while (temp != 0)
{
pTemp1 = OnErosion(pTemp1,iHeight,iWidth,nWidthBytes);
pTemp2 = OnErosion(pTemp2,iHeight,iWidth,nWidthBytes);
pTemp2 = OnErosion(pTemp2,iHeight,iWidth,nWidthBytes);
pTemp2 = OnDilation(pTemp2,iHeight,iWidth,nWidthBytes);
temp = 0;
for (j=0; j<iHeight; j++)
{
for (i=0; i<iWidth; i++)
{
if (pTemp1[(iHeight-1-j)*nWidthBytes + i] == 0)
{
temp++;
}
}
}
for(i=0;i<iNewHeight;i++)
{
for(j=0;j<iNewWidth;j++)
{
if ((pTemp1[(iNewHeight-1-i)*nWidthBytes+j]-pTemp2[(iNewHeight-1-i)*nWidthBytes+j])==-255)
{
pNewBits[(iNewHeight-1-i)*nWidthBytes+j]=0;
}
}
}
for(i=0;i<iNewHeight;i++)
{
for(j=0;j<iNewWidth;j++)
{
if (pTemp3[(iNewHeight-1-i)*nWidthBytes+j]==0)
{
pNewBits[(iNewHeight-1-i)*nWidthBytes+j]=0;
}
}
}
for(i=0;i<iNewHeight;i++)
{
for(j=0;j<iNewWidth;j++)
{
pTemp3[(iNewHeight-1-i)*nWidthBytes+j]=255;
pTemp2[(iNewHeight-1-i)*nWidthBytes+j]=pTemp1[(iNewHeight-1-i)*nWidthBytes+j];
}
}
}
delete [] pTemp1;
delete [] pTemp2;
pTemp1 = NULL;
pTemp2 = NULL;
}
break;
}
//Free the memory for the old image
::GlobalUnlock(m_pImageObject->GetDib());
::GlobalFree(m_pImageObject->GetDib());
//Update the DIB in the object pointed by m_pImaeObject
::GlobalUnlock(hNewDib);
m_pImageObject->SetDib(hNewDib);
return TRUE;
}
inline BYTE* CMorphology::OnErosion(unsigned char *pBits, int iHeight, int iWidth, int nWidthbytes)
{
unsigned char *temp;
int i,j,m,n;
temp=new BYTE[iHeight*nWidthbytes];
for(i=0;i<iHeight;i++)
{
for(j=0;j<iWidth;j++)
{
temp[(iHeight-1-i)*nWidthbytes+j]=pBits[(iHeight-1-i)*nWidthbytes+j];
}
}
for(i=2;i<iHeight-2;i++)
{
for(j=2;j<iWidth-2;j++)
{
if(pBits[(iHeight-1-i)*nWidthbytes+j]==0)
{
for(m=0;m<5;m++)
{
for(n=0;n<5;n++)
{
if (m_elementselcted[m*5+n]==1)
{
if(pBits[(iHeight-1-i-m+2)*nWidthbytes+j-2+n]!=0)
{
temp[(iHeight-1-i)*nWidthbytes+j]=255;
}
}
}
}
}
}
}
return temp;
}
inline BYTE* CMorphology::OnDilation(unsigned char *pBits, int iHeight, int iWidth, int nWidthbytes)
{
unsigned char *temp;
int i,j,m,n;
temp=new BYTE[iHeight*nWidthbytes];
for(i=0;i<iHeight;i++)
{
for(j=0;j<iWidth;j++)
{
temp[(iHeight-1-i)*nWidthbytes+j]=pBits[(iHeight-1-i)*nWidthbytes+j];
}
}
for(i=2;i<iHeight-2;i++)
{
for(j=2;j<iWidth-2;j++)
{
temp[(iHeight-1-i)*nWidthbytes+j]=255;
for(m=0;m<5;m++)
{
for(n=0;n<5;n++)
{
if (m_elementselcted[m*5+n]==1)
{
if(pBits[(iHeight-1-i-m+2)*nWidthbytes+j-2+n]==0)
{
temp[(iHeight-1-i)*nWidthbytes+j]=0;
}
}
}
}
}
}
return temp;
}
inline BYTE* CMorphology::OnThining(unsigned char *pBits, int iHeight, int iWidth, int nWidthbytes)
{
unsigned char* temp;
temp=new BYTE[iHeight*nWidthbytes];
long i,j;
int m,n;
BOOL bCondition1;
BOOL bCondition2;
BOOL bCondition3;
BOOL bCondition4;
unsigned char nCount;
unsigned char neighbour[5][5];
for(i=0;i<iHeight;i++)
{
for(j=0;j<iWidth;j++)
{
temp[(iHeight-1-i)*nWidthbytes+j]=pBits[(iHeight-1-i)*nWidthbytes+j];
}
}
for(i=2;i<iHeight-2;i++)
{
for(j=2;j<iWidth-2;j++)
{
bCondition1=FALSE;
bCondition2=FALSE;
bCondition3=FALSE;
bCondition4=FALSE;
if(pBits[(iHeight-1-i)*nWidthbytes+j]==0)
{
for(m=0;m<5;m++)
{
for(n=0;n<5;n++)
{
neighbour[m][n]=(255-pBits[(iHeight-1-i-m+2)*nWidthbytes+j-2+n])/255;
}
}
//逐个判断条件
//判断条件1:2<=NZ(P1)<=6
nCount= neighbour[1][1] + neighbour[1][2] + neighbour[1][3] \
+ neighbour[2][1] + neighbour[2][3] + \
+ neighbour[3][1] + neighbour[3][2] + neighbour[3][3];
if (nCount>=2 && nCount<=6)
{
bCondition1=TRUE;
}
//判断条件2:Z0(P1)=1
nCount=0;
if (neighbour[1][2] == 0 && neighbour[1][1] == 1)
nCount++;
if (neighbour[1][1] == 0 && neighbour[2][1] == 1)
nCount++;
if (neighbour[2][1] == 0 && neighbour[3][1] == 1)
nCount++;
if (neighbour[3][1] == 0 && neighbour[3][2] == 1)
nCount++;
if (neighbour[3][2] == 0 && neighbour[3][3] == 1)
nCount++;
if (neighbour[3][3] == 0 && neighbour[2][3] == 1)
nCount++;
if (neighbour[2][3] == 0 && neighbour[1][3] == 1)
nCount++;
if (neighbour[1][3] == 0 && neighbour[1][2] == 1)
nCount++;
if (nCount == 1)
bCondition2 = TRUE;
//判断条件3:P2*P4*P8=0 or Z0(P2)!=1
if (neighbour[1][2]*neighbour[2][1]*neighbour[2][3] == 0)
{
bCondition3 = TRUE;
}
else
{
nCount = 0;
if (neighbour[0][2] == 0 && neighbour[0][1] == 1)
nCount++;
if (neighbour[0][1] == 0 && neighbour[1][1] == 1)
nCount++;
if (neighbour[1][1] == 0 && neighbour[2][1] == 1)
nCount++;
if (neighbour[2][1] == 0 && neighbour[2][2] == 1)
nCount++;
if (neighbour[2][2] == 0 && neighbour[2][3] == 1)
nCount++;
if (neighbour[2][3] == 0 && neighbour[1][3] == 1)
nCount++;
if (neighbour[1][3] == 0 && neighbour[0][3] == 1)
nCount++;
if (neighbour[0][3] == 0 && neighbour[0][2] == 1)
nCount++;
if (nCount != 1)
bCondition3 = TRUE;
}
//判断条件4:P2*P4*P6=0 or Z0(P4)!=1
if (neighbour[1][2]*neighbour[2][1]*neighbour[3][2] == 0)
{
bCondition4 = TRUE;
}
else
{
nCount = 0;
if (neighbour[1][1] == 0 && neighbour[1][0] == 1)
nCount++;
if (neighbour[1][0] == 0 && neighbour[2][0] == 1)
nCount++;
if (neighbour[2][0] == 0 && neighbour[3][0] == 1)
nCount++;
if (neighbour[3][0] == 0 && neighbour[3][1] == 1)
nCount++;
if (neighbour[3][1] == 0 && neighbour[3][2] == 1)
nCount++;
if (neighbour[3][2] == 0 && neighbour[2][2] == 1)
nCount++;
if (neighbour[2][2] == 0 && neighbour[1][2] == 1)
nCount++;
if (neighbour[1][2] == 0 && neighbour[1][1] == 1)
nCount++;
if (nCount != 1)
bCondition4 = TRUE;
}
}
if(bCondition1 && bCondition2 && bCondition3 && bCondition4)
{
temp[(iHeight-1-i)*nWidthbytes+j]=255;
}
}
}
return temp;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -