我要实现的功能是对一灰度图像(已经经过二值化)进行腐蚀和膨化,实际运行后未报错,但最后得到的图像竟然全是白色的,我的检查结果是在memcpy(lpDIBBits,lpNewDIBBits,lWidth*lHeight);
这句出错,因为我先前对lpNewDIBBits进行了初始化://initialize the memory,set the initial value to 255
lpDst=(char*)lpNewDIBBits;
memset(lpDst,(BYTE)255,lWidth*lHeight);
将其都置成了白色,但我在前面对lpDst进行了处理,有的应该是黑色的。这是怎么回事?急求解答!
腐蚀膨化函数代码如下:bool CImage33Dlg::OpenDIB(LPSTR lpDIBBits, long lWidth, long lHeight)
{
LPSTR lpSrc;
LPSTR lpDst;
LPSTR lpNewDIBBits;
HLOCAL hNewDIBBits;
long i,j,m,n;
unsigned char pixel; //allocate memory to store new image
hNewDIBBits=LocalAlloc(LHND,lWidth*lHeight);
if(hNewDIBBits==NULL)
{
  return FALSE;
}
lpNewDIBBits=(char*)LocalLock(hNewDIBBits); //initialize the memory,set the initial value to 255
lpDst=(char*)lpNewDIBBits;
memset(lpDst,(BYTE)255,lWidth*lHeight); //erosion
for(j=1;j<lHeight-1;j++)
{
 for(i=0;i<lWidth;i++)
 {
   //to avoid over boundary,do not process some pixels
 //point to row j, clm i of source image
 lpSrc=(char*)lpDIBBits+lWidth*j+i;
 //point to row j, clm i of objective image
 lpDst=(char*)lpNewDIBBits+lWidth*j+i;
 //get the current pixel
 pixel=(unsigned char)*lpSrc;
 / t the current pixel to black
 *lpDst=(unsigned char)0;
 //如果原图像中对应结构元素中为黑色的那些点中有一个不是黑色,则将目标图像中
 //的当前点设为白色,主意DIB图像中的内容是倒置的
 for(m=0;m<3;m++)
 {
   for(n=0;n<3;n++)
   {
     //if(structure[m][n]==-1)
 //continue;
 pixel=*(lpSrc+((2-m)-1)*lWidth+(n-1));
 if(pixel==255)
 {
   *lpDst=(unsigned char)255;
   break;
 }
   }
 }
 }
}
memcpy(lpDIBBits,lpNewDIBBits,lWidth*lHeight); //reinitialize the memory and set the value to 255
lpDst=(char*)lpNewDIBBits;
memset(lpDst,(BYTE)255,lWidth*lHeight);
for(j=1;j<lHeight-1;j++)
{
  for(i=0;i<lWidth;i++)
  {
    lpSrc=(char*)lpDIBBits+lWidth*j+i;
lpDst=(char*)lpNewDIBBits+lWidth*j+i;
pixel=(unsigned char)*lpSrc;
*lpDst=(unsigned char)255;
for(m=0;m<3;m++)
{
  for(n=0;n<3;n++)
  {
    //if(structure[m][n]==-1)
//continue;
pixel=*(lpSrc+((2-m)-1)*lWidth+(n-1));
if(pixel==0)
{
  *lpDst=(unsigned char)0;
  break;
}
  }
}
  }
}
memcpy(lpDIBBits,lpNewDIBBits,lWidth*lHeight);
LocalUnlock(hNewDIBBits);
LocalFree(hNewDIBBits);
return TRUE;
}

解决方案 »

  1.   

    直接用Opencv的函数进行腐蚀膨胀
      

  2.   

    for(j=1;j<lHeight-1;j++)
    {
    for(i=0;i<lWidth;i++)//显然你是要对图像矩阵数据进行处理,但是lwidth代表每行数据总的字节数,不是每行的数据数,把lwidth改成width应该就可以了~
    {
    建议你把bmp文件格式分析好好再看看~
      

  3.   

    http://blog.csdn.net/luckyboy101/article/details/6803596
      

  4.   

    width是什么?我这样的模板去匹配应该会有些效果的啊