代码改变世界

VC6.0图形处理10--腐蚀膨胀

2011-09-15 22:22  libing64  阅读(249)  评论(0编辑  收藏  举报
void CBMPViewerDoc::OnMenuitem32799() //图像的腐蚀
{


//自定义3*3的矩阵作为检验的结构
// TODO: Add your command handler code here


int linewidth;
linewidth=(pbi->bmiHeader.biWidth*pbi->bmiHeader.biBitCount+31)/32*4;


unsigned char *lpDest;
unsigned char *lpScr;



HLOCAL hTemp;
hTemp = LocalAlloc(LHND ,linewidth * bi.biHeight );

LPSTR lpTemp;
lpTemp = (char*)LocalLock(hTemp);//以上四句分配一块与源图像数据大小一致的内存块,并返回指针lpTemp
//暂时存储新建的图片数据,完成后,将数据copy给原数据区

int pixel;
// TODO: Add your command handler code here
for(int i = 0 ; i< bi.biHeight ; i++)
{
for(int j = 0 ; j< bi.biWidth ; j++)
{
    lpDest = (unsigned  char *)lpTemp+linewidth*(bi.biHeight - i -1 ) + j ;
if((i == 0 ) || (j ==0) || (i == bi.biHeight) || (j == bi.biWidth) ){
lpScr = (unsigned char*)lpBuf + linewidth*(bi.biHeight - i -1) + j;
*lpDest = *lpScr;




else
{

//lpDest = (unsigned  char *)lpTemp+linewidth*(bi.biHeight - i -1 ) + j ;
*lpDest = 0;

for(int s = -1 ; s<=1; s++)
{
for(int t = -1; t <= 1;t++)
{
lpScr = (unsigned char *)lpBuf + linewidth*(bi.biHeight - i-1 + s) + j + t;
              
pixel = *lpScr;
if(pixel == 255)
{


*lpDest = 255;
}


}


}
   

}

}

}

memcpy(lpBuf , lpTemp, linewidth*bi.biHeight);//新图像数据copy给源数据
// Invalidata(TRUE);
UpdateAllViews(NULL,0,NULL);

}


void CBMPViewerDoc::OnMenuitem32800() //图像的膨胀
{

//自定义结构
// TODO: Add your command handler code here

int linewidth;
linewidth=(pbi->bmiHeader.biWidth*pbi->bmiHeader.biBitCount+31)/32*4;

unsigned char *lpDest;
unsigned char *lpScr;


HLOCAL hTemp;
hTemp = LocalAlloc(LHND ,linewidth * bi.biHeight );

LPSTR lpTemp;
lpTemp = (char*)LocalLock(hTemp);//以上四句分配一块与源图像数据大小一致的内存块,并返回指针lpTemp
//暂时存储新建的图片数据,完成后,将数据copy给原数据区

int pixel;
// TODO: Add your command handler code here
for(int i = 0 ; i< bi.biHeight ; i++)
{
for(int j = 0 ; j< bi.biWidth ; j++)
{
lpDest = (unsigned  char *)lpTemp+linewidth*(bi.biHeight - i -1 ) + j ;
if((i == 0 ) || (j ==0) || (i == bi.biHeight) || (j == bi.biWidth) ){
lpScr = (unsigned char*)lpBuf + linewidth*(bi.biHeight - i -1) + j;
*lpDest = *lpScr;



else
{

//lpDest = (unsigned  char *)lpTemp+linewidth*(bi.biHeight - i -1 ) + j ;
*lpDest = 255;




for(int s = 0 ; s<=1; s++)
{
for(int t = 0; t <= 1;t++)
{
lpScr = (unsigned char *)lpBuf + linewidth*(bi.biHeight - i-1 + s) + j + t;

pixel = *lpScr;
if(pixel == 0)
{
*lpDest = 0;
}

}

}


}

}

}

memcpy(lpBuf , lpTemp, linewidth*bi.biHeight);//新图像数据copy给源数据
// Invalidata(TRUE);
UpdateAllViews(NULL,0,NULL);

}