解决方案 »

  1.   

    你是说膨胀法和腐蚀法吧?先膨后腐,就可以实现邻域合并了
    bool CSBmpFilter::DilContra(CSBitmap* SrcBmp)
    {
    if(!(SrcBmp->pBitData&&SrcBmp->BitCount==1))
    return false;
    CSBitmap TempBmp;
    TempBmp=*SrcBmp;
    int Endy=SrcBmp->Height-1,Endx=SrcBmp->Width-1;
    for(int y=1;y<Endy;y++)           //Dilate
    {
    for(int x=1;x<Endx;x++)
    {
    if(SrcBmp->GetPixel(x,y))
    {
    TempBmp.SetPixel(x-1,y-1,1);
    TempBmp.SetPixel(x,y-1,1);
    TempBmp.SetPixel(x+1,y-1,1);
    TempBmp.SetPixel(x-1,y,1);
    TempBmp.SetPixel(x+1,y,1);
    TempBmp.SetPixel(x-1,y+1,1);
    TempBmp.SetPixel(x,y+1,1);
    TempBmp.SetPixel(x+1,y+1,1);
    }
    }
    } *SrcBmp=TempBmp;
    for(int y=1;y<Endy;y++)           //Contra
    {
    for(int x=1;x<Endy;x++)
    {
    if(TempBmp.GetPixel(x,y))
    {
    if(!TempBmp.GetPixel(x-1,y))
    SrcBmp->SetPixel(x,y,0);
    if(!TempBmp.GetPixel(x+1,y))
    SrcBmp->SetPixel(x,y,0);
    if(!TempBmp.GetPixel(x,y-1))
    SrcBmp->SetPixel(x,y,0);
    if(!TempBmp.GetPixel(x,y+1))
    SrcBmp->SetPixel(x,y,0);
    }
    }
    }
    return true;
    }
      

  2.   

    四向连通递归填充算法:
    void BoundaryFill4(int x, int y, long FilledColor, long BoundaryColor)
    {
    long CurrentColor;
    CurrentColor = GetPixelColor(x,y);
    if (CurrentColor != BoundaryColor && CurrentColor != FilledColor)
    {
    SetColor(FilledColor);
    SetPixel (x,y);
    BoundaryFill4(x+1, y, FilledColor, BoundaryColor);
    BoundaryFill4(x-1, y, FilledColor, BoundaryColor);
    BoundaryFill4(x, y+1, FilledColor, BoundaryColor);
    BoundaryFill4(x, y-1, FilledColor, BoundaryColor);
    }
    }