首页 > 代码库 > 连通域去噪

连通域去噪

/* 漫水法填充标定实现
copy from: http://blog.csdn.net/zhjx2314/article/details/1629702
*/

 

写好的去噪,想优化一下代码,结果去噪那部分丢了。。。有空再重写吧

 

floodfill(Mat &src){        struct Seed{        int x;        int y;    };    class mPoint{    public:        mPoint(int xx, int  yy, int mflag)        {            x = xx;            y = yy;            flag = mflag;        }    public:        int x;        int y;        int flag;    };    int Row = src.rows;    int Col = src.cols;    int flag = 30;    Seed *Seeds;    int StackPoint;    int pixe;    //当前像素位置      int curx, cury;    //分配种子空间       Seeds = new Seed[Row*Col];    //计算每个标定值的像素个数      int count[251];    for (int i = 0; i < 252; i++)    {        count[i] = 0; //初始化为0      }    uchar *p = src.data;    for (int i = 0; i < src.rows; i++)    {        for (int j = 0; j < src.cols; j++)        {            if (*(p + i*src.cols + j) == 0)            {                Seeds[1].x = j;                Seeds[1].y = i;                StackPoint = 1;                while (StackPoint != 0)                {                    curx = Seeds[StackPoint].x;                    cury = Seeds[StackPoint].y;                    StackPoint--;                    //取当前指针所处的像素值                    pixe = *(p + cury*Col + curx);                    //将指针所处的像素标定                    *(p + cury*Col + curx) = flag;                    count[flag]++;                    //判断左面的点,如果为黑,则压入堆栈                      //注意防止越界                      if (curx > 0)                    {                        pixe = *(p + cury*Col + curx - 1);                        if (pixe == 0)                        {                            StackPoint++;                            Seeds[StackPoint].x = curx - 1;                            Seeds[StackPoint].y = cury;                        }                    }                    //判断右面的点,如果为黑,则压入堆栈                      //注意防止越界                      if (curx < Col)                    {                        pixe = *(p + cury*Col + curx + 1);                        if (pixe == 0)                        {                            StackPoint++;                            Seeds[StackPoint].x = curx + 1;                            Seeds[StackPoint].y = cury;                        }                    }                    //判断上面的点,如果为黑,则压入堆栈                      //注意防止越界                      if (cury > 0)                    {                        pixe = *(p + (cury - 1)*Col + curx);                        if (pixe == 0)                        {                            StackPoint++;                            Seeds[StackPoint].x = curx;                            Seeds[StackPoint].y = cury - 1;                        }                    }                    //判断下面的点,如果为黑,则压入堆栈                      //注意防止越界                     if (cury < Row)                    {                        pixe = *(p + (cury + 1)*Col + curx);                        if (pixe == 0)                        {                            StackPoint++;                            Seeds[StackPoint].x = curx;                            Seeds[StackPoint].y = cury + 1;                        }                    }                }//end while( StackPoint != 0)                  flag = (flag + 7) % 251;//改变标定值            }//end if        }//end for(i     }//end for(j      //释放堆栈      delete Seeds;}

 

连通域去噪