📄 bmpbasemorph.cpp
字号:
// BmpBaseMorph.cpp: implementation of the BmpBaseMorph class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "shufa.h"
#include "BmpBaseMorph.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBmpBaseMorph::CBmpBaseMorph(CFG_DIB* Dib,int Mask[][2],int n)
{
int i,j,count;
m_MaskPixesCount=n;
m_DibBit=Dib->m_lpDIBits;
m_height=Dib->GetHeight();
m_width=Dib->GetWidth();
count=Dib->GetImageSize();
m_width=(((count/m_height)-m_width)==0)?m_width:((count/m_height));
m_MaskScreen=(int*)malloc(64*64*sizeof(int));
m_MaskG=NULL;
m_CopyImage=(int*)malloc(m_height*m_width*sizeof(int));
for(i=0;i<m_height;i++)
{
for(j=0;j<m_width;j++)
{
m_CopyImage[i*(m_width)+j]=0;
}
}
m_Mask=(int*)Mask;
}
CBmpBaseMorph::~CBmpBaseMorph()
{
delete m_CopyImage;
delete m_MaskScreen;
}
void CBmpBaseMorph::CopyImageToInt()
{
LPBYTE DibBit=m_DibBit;
for(int i=0;i<m_height;i++)
for(int j=0;j<m_width;j++)
{
if((*DibBit)==0xff)
{
m_CopyImage[i*(m_width)+j]=0;
}
else
{
m_CopyImage[i*(m_width)+j]=1;
}
DibBit++;
}
}
BOOL CBmpBaseMorph::ReverseColor()
{
if(m_DibBit!=NULL)
{
LPBYTE bitdata=m_DibBit;
for(int i=0;i<m_height;i++)
for(int j=0;j<m_width;j++)
{
//可以根据直方图来确定阀值
if(((*bitdata)>=0x64)&&((*bitdata)<0xa0))
{
*bitdata=0x00;
}
else
{
if((*bitdata)<=0x64)
*bitdata=0xff;
else
*bitdata=0x00;
}
bitdata++;
}
return true;
}
else
return false;
}
void CBmpBaseMorph::EroseBmp(int BorFcolor)
{
int i,j,k;
CopyImageToInt();
for(j=0;j<m_height;j++)
for(i=0;i<m_width;i++)
for(k=0;k<cout;k++)
{
int temp1= m_Mask[k*2];
int temp2= m_Mask[k*2+1];
if((i+*(m_MaskG+k*2)>=0)&&(i+*(m_MaskG+k*2)<m_width)&&
(j+*(m_MaskG+k*2+1)>=0)&&(j+*(m_MaskG+k*2+1)<m_height))//判断是否超出腐蚀的范围
{
int itemp=*(m_MaskG+k*2);
int jtemp=*(m_MaskG+k*2+1);
if(BorFcolor==1) //判断是对前景还是对背景进行腐蚀
{
if((m_CopyImage[(j+jtemp)*m_width+i+itemp])==0)//1
{
*(m_DibBit+j*m_width+i)=0xff;//0x00
break;
}
}
else
{
if((m_CopyImage[(j+jtemp)*m_width+i+itemp])==1)//1
{
*(m_DibBit+j*m_width+i)=0x00;
break;
}
}
}
else
{
*(m_DibBit+j*m_width+i)=0xff;
break;
}
}
}
void CBmpBaseMorph::DilationBmp(int BorFcolor)
{
int i,j,k;
CopyImageToInt();
for(j=0;j<m_height;j++)
for(i=0;i<m_width;i++)
for(k=0;k<cout;k++)
{
if((i+*(m_MaskG+k*2)>=0)&&(i+*(m_MaskG+k*2)<m_width)&&
(j+*(m_MaskG+k*2+1)>=0)&&(j+*(m_MaskG+k*2+1)<m_height))//判断是否超出膨胀的范围
{
int itemp=*(m_MaskG+k*2);
int jtemp=*(m_MaskG+k*2+1);
if(BorFcolor==1) //判断是对前景还是对背景进行膨胀
{
if((m_CopyImage[(j+jtemp)*m_width+i+itemp])==1)//1
{
*(m_DibBit+j*m_width+i)=0x00;//0x00
break;
}
}
else
{
if((m_CopyImage[(j+jtemp)*m_width+i+itemp])==0)//1
{
*(m_DibBit+j*m_width+i)=0xff;
break;
}
}
}
else
{
*(m_DibBit+j*m_width+i)=0xff;
break;
}
}
}
void CBmpBaseMorph::OpenBmp(int BorFcolor)
{
EroseBmp(BorFcolor);
CopyImageToInt();
DilationBmp(BorFcolor);
}
void CBmpBaseMorph::CloseBmp(int BorFcolor)
{
DilationBmp(BorFcolor);
CopyImageToInt();
EroseBmp(BorFcolor);
}
void CBmpBaseMorph::FindHightCurvaPoint()
{
int i,j;
CopyImageToInt();
int *Inmage1=(int*)malloc(m_height*m_width*sizeof(int));
//开辟新内存copy原骨架图
for(i=0;i<m_height;i++)
for(j=0;j<m_width;j++)
*(Inmage1+i*m_width+j)=*(m_CopyImage+i*m_width+j);
OpenBmp(0);
CopyImageToInt();
//原骨架与开运算后的图与运算 说明:消除原骨架
for(i=0;i<m_height;i++)
for(int j=0;j<m_width;j++)
{
if(*(Inmage1+i*m_width+j)==*(m_CopyImage+i*m_width+j))
*(m_DibBit+i*m_width+j)=0xff;
}
//膨胀关键结构
DilationBmp(1);
CopyImageToInt();
//根据原骨架和膨胀后关键结构图取反
for(i=0;i<m_height;i++)
for(int j=0;j<m_width;j++)
{
if(*(Inmage1+i*m_width+j)!=*(m_CopyImage+i*m_width+j))
*(m_DibBit+i*m_width+j)=0xff;
else
*(m_DibBit+i*m_width+j)=0x00;
//得到骨架上的叉叉
/*if(*(m_CopyImage+i*m_width+j)==1)
{
if(*(Inmage1+i*m_width+j)==*(m_CopyImage+i*m_width+j))
*(m_DibBit+i*m_width+j)=0x00;
else
*(m_DibBit+i*m_width+j)=0xff;
}*/
}
// ReverseColor();
delete Inmage1;
}
void CBmpBaseMorph::StructGenerator(int t,int BorFcolor )
{
int i,j,k,m;
int sum=0;
int temp[64][64];
for(i=0;i<64;i++)
for(j=0;j<64;j++)
{
if((i==31)&(j==31))
{temp[i][j]=1;
*(m_MaskScreen+i*64+j)=1;}
else
{temp[i][j]=0;
*(m_MaskScreen+i*64+j)=0;}
}
for(m=0;m<t;m++)
{
for(j=0;j<64;j++)
for(i=0;i<64;i++)
for(k=0;k<m_MaskPixesCount;k++)
{
if((i+*(m_Mask+k*2)>=0)&&(i+*(m_Mask+k*2)<64)&&
(j+*(m_Mask+k*2+1)>=0)&&(j+*(m_Mask+k*2+1)<64))//判断是否超出膨胀的范围
{
int itemp=*(m_Mask+k*2);
int jtemp=*(m_Mask+k*2+1);
if(BorFcolor==1) //判断是对前景还是对背景进行膨胀
{
if(*(m_MaskScreen+(j+jtemp)*64+(i+itemp))==1)//1
{
// *(m_MaskScreen+j*64+i)=1;
temp[i][j]=1;
break;
}
}
else
{
if(*(m_MaskScreen+(j+jtemp)*64+(i+itemp))==0)//1
{
//*(m_MaskScreen+j*64+i)=0;
temp[i][j]=0;
break;
}
}
}
else
{
//*(m_MaskScreen+j*64+i)=0;
temp[i][j]=0;
break;
}
}
for(i=0;i<64;i++)
for(j=0;j<64;j++)
{
*(m_MaskScreen+i*64+j)=temp[i][j];
}
}
for(i=0;i<64;i++)
for(j=0;j<64;j++)
{
//if(*(m_MaskScreen+i*64+j)==1)
if(temp[i][j]==1)
sum++;
}
m_MaskG=(int*)malloc(2*sum*sizeof(int));
cout=sum;
int s=0;
for(i=0;i<64;i++)
for(j=0;j<64;j++)
{
if(temp[i][j]==1)
{
m_MaskG[2*s]=i-31;
m_MaskG[2*s+1]=j-31;
if(s<(sum))
s++;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -