📄 morphology.cpp
字号:
// Morphology.cpp : implementation file
//
#include "stdafx.h"
#include "photostar.h"
#include "Morphology.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMorphology dialog
CMorphology::CMorphology(CWnd* pParent /*=NULL*/)
: CDialog(CMorphology::IDD, pParent)
{
//{{AFX_DATA_INIT(CMorphology)
m_iltem = _T("");
//}}AFX_DATA_INIT
for(int i=0;i<25;i++)
{
m_elementselcted[i]=0;
}
}
void CMorphology::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMorphology)
DDX_Text(pDX, IDC_ILTEM, m_iltem);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMorphology, CDialog)
//{{AFX_MSG_MAP(CMorphology)
ON_BN_CLICKED(IDC_ELEMENT1, OnElement1)
ON_BN_CLICKED(IDC_ELEMENT2, OnElement2)
ON_BN_CLICKED(IDC_ELEMENT3, OnElement3)
ON_BN_CLICKED(IDC_ELEMENT4, OnElement4)
ON_BN_CLICKED(IDC_ELEMENT5, OnElement5)
ON_BN_CLICKED(IDC_ELEMENT6, OnElement6)
ON_BN_CLICKED(IDC_ELEMENT7, OnElement7)
ON_BN_CLICKED(IDC_ELEMENT8, OnElement8)
ON_BN_CLICKED(IDC_ELEMENT9, OnElement9)
ON_BN_CLICKED(IDC_ELEMENT10, OnElement10)
ON_BN_CLICKED(IDC_ELEMENT11, OnElement11)
ON_BN_CLICKED(IDC_ELEMENT12, OnElement12)
ON_BN_CLICKED(IDC_ELEMENT13, OnElement13)
ON_BN_CLICKED(IDC_ELEMENT14, OnElement14)
ON_BN_CLICKED(IDC_ELEMENT15, OnElement15)
ON_BN_CLICKED(IDC_ELEMENT16, OnElement16)
ON_BN_CLICKED(IDC_ELEMENT17, OnElement17)
ON_BN_CLICKED(IDC_ELEMENT18, OnElement18)
ON_BN_CLICKED(IDC_ELEMENT19, OnElement19)
ON_BN_CLICKED(IDC_ELEMENT20, OnElement20)
ON_BN_CLICKED(IDC_ELEMENT21, OnElement21)
ON_BN_CLICKED(IDC_ELEMENT22, OnElement22)
ON_BN_CLICKED(IDC_ELEMENT23, OnElement23)
ON_BN_CLICKED(IDC_ELEMENT24, OnElement24)
ON_BN_CLICKED(IDC_ELEMENT25, OnElement25)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMorphology message handlers
void CMorphology::OnElement1()
{
m_elementselcted[0]=1;
}
void CMorphology::OnElement2()
{
m_elementselcted[1]=1;
}
void CMorphology::OnElement3()
{
m_elementselcted[2]=1;
}
void CMorphology::OnElement4()
{
m_elementselcted[3]=1;
}
void CMorphology::OnElement5()
{
m_elementselcted[4]=1;
}
void CMorphology::OnElement6()
{
m_elementselcted[5]=1;
}
void CMorphology::OnElement7()
{
m_elementselcted[6]=1;
}
void CMorphology::OnElement8()
{
m_elementselcted[7]=1;
}
void CMorphology::OnElement9()
{
m_elementselcted[8]=1;
}
void CMorphology::OnElement10()
{
m_elementselcted[9]=1;
}
void CMorphology::OnElement11()
{
m_elementselcted[10]=1;
}
void CMorphology::OnElement12()
{
m_elementselcted[11]=1;
}
void CMorphology::OnElement13()
{
m_elementselcted[12]=1;
}
void CMorphology::OnElement14()
{
m_elementselcted[13]=1;
}
void CMorphology::OnElement15()
{
m_elementselcted[14]=1;
}
void CMorphology::OnElement16()
{
m_elementselcted[15]=1;
}
void CMorphology::OnElement17()
{
m_elementselcted[16]=1;
}
void CMorphology::OnElement18()
{
m_elementselcted[17]=1;
}
void CMorphology::OnElement19()
{
m_elementselcted[18]=1;
}
void CMorphology::OnElement20()
{
m_elementselcted[19]=1;
}
void CMorphology::OnElement21()
{
m_elementselcted[20]=1;
}
void CMorphology::OnElement22()
{
m_elementselcted[21]=1;
}
void CMorphology::OnElement23()
{
m_elementselcted[22]=1;
}
void CMorphology::OnElement24()
{
m_elementselcted[23]=1;
}
void CMorphology::OnElement25()
{
m_elementselcted[24]=1;
}
BOOL CMorphology::OnMorphology()
{
if (m_pImageObject == NULL)
{
return FALSE;
}
int iWidth = m_pImageObject->GetWidth();
int iHeight = m_pImageObject->GetHeight();
int nNumBits = m_pImageObject->GetNumBits();//取得图像的位数
/*************************************the old image*****************************/
unsigned char *pOldBuffer, *pNewBuffer, *pOldBits, *pNewBits;
BITMAPFILEHEADER *pOldBFH, *pNewBFH;
BITMAPINFOHEADER *pOldBIH, *pNewBIH;
RGBQUAD *pOldPalette, *pNewPalette;
int nWidthBytes, nNumColors;
pOldBuffer = (unsigned char *) m_pImageObject->GetDIBPointer(&nWidthBytes, m_pImageObject->GetNumBits());
if (pOldBuffer == NULL)
{
return FALSE;
}
pOldBFH = (BITMAPFILEHEADER *) pOldBuffer;
pOldBIH = (BITMAPINFOHEADER *) &pOldBuffer[sizeof(BITMAPFILEHEADER)];
nNumColors = m_pImageObject->GetNumColors();
pOldPalette = (RGBQUAD *) &pOldBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
pOldBits = (unsigned char *) &pOldBuffer[sizeof(BITMAPFILEHEADER)
+sizeof(BITMAPINFOHEADER)+nNumColors*sizeof(RGBQUAD)];
/*************************************the new image*****************************/
DWORD dwNewSize;
HGLOBAL hNewDib;
int iNewHeight=iHeight;
int iNewWidth=iWidth;
//Allocate a global memory block for the new image
dwNewSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)
+ nNumColors * sizeof(RGBQUAD)
+ nWidthBytes * (m_pImageObject->GetHeight());
hNewDib = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, dwNewSize);
if (hNewDib == NULL)
{
m_pImageObject->m_nLastError = IMAGELIB_MEMORY_ALLOCATION_ERROR;
::GlobalUnlock(m_pImageObject->GetDib());
return FALSE;
}
//Get the pointer to the new memory block
pNewBuffer = (unsigned char *) ::GlobalLock(hNewDib);
if (pNewBuffer == NULL)
{
::GlobalFree( hNewDib );
m_pImageObject->m_nLastError = IMAGELIB_MEMORY_LOCK_ERROR;
::GlobalUnlock(m_pImageObject->GetDib());
return FALSE;
}
pNewBFH = (BITMAPFILEHEADER *) pNewBuffer;
pNewBIH = (BITMAPINFOHEADER *) &pNewBuffer[sizeof(BITMAPFILEHEADER)];
pNewPalette = (RGBQUAD *) &pNewBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
pNewBits = (unsigned char *) &pNewBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nNumColors*sizeof(RGBQUAD)];
//Make a copy of the old image
memcpy(pNewBuffer,pOldBuffer,dwNewSize);
/**********************************morphology processing************************************/
switch(m_morphologytype)
{
case 1://腐蚀运算
{
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=OnErosion(pOldBits,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 2://膨胀运算
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=OnDilation(pOldBits,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 3://开运算
{
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];
}
}
temp=OnErosion(pOldBits,iNewHeight,iNewWidth,nWidthBytes);
temp=OnDilation(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 4://闭运算
{
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=OnDilation(pOldBits,iNewHeight,iNewWidth,nWidthBytes);
temp=OnErosion(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 5://细化
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -