图像处理(十一) 细化算法

**细化算法 **

欢迎在下面留言

原理:以后再补,原理差不多,

代码:

/*细化算法
 */
void *Thinning(QImage &image,QImage &optImage)
{
    int height = image.height();
    int width = image.width();

    //四个条件

    bool bCondition1;
    bool bCondition2;
    bool bCondition3;
    bool bCondition4;


    //5 x 5 领域
    unsigned char neighbour[5][5];

    bool bModified = true;

    while (bModified)
    {
        bModified = false;
        for(int i = 2; i=2 && nCount<=6)
                {
                    bCondition1 = true;
                }

                //判断Z0(P1) = 1
                nCount = 0;

                if(neighbour[1][2] == 0 && neighbour[1][1] == 1)
                {
                    nCount ++;
                }

                if(neighbour[1][1] == 0 && neighbour[2][1] == 1)
                {
                    nCount ++;
                }

                if(neighbour[2][1] == 0 && neighbour[3][1] == 1)
                {
                    nCount ++;
                }

                if(neighbour[3][1] == 0 && neighbour[3][2] == 1)
                {
                    nCount ++;
                }

                if(neighbour[3][2] == 0 && neighbour[3][3] == 1)
                {
                    nCount ++;
                }

                if(neighbour[3][3] == 0 && neighbour[2][3] == 1)
                {
                    nCount ++;
                }

                if(neighbour[2][3] == 0 && neighbour[1][3] == 1)
                {
                    nCount ++;
                }

                if(neighbour[1][3] == 0 && neighbour[1][2] == 1)
                {
                    nCount ++;
                }

                if(nCount == 1)
                {
                    bCondition2 = true;
                }

                //P2 x P4 x P8 = 0 或者Z0(P2) != 1
                if(neighbour[1][2] * neighbour[2][1] * neighbour[2][3] == 0)
                {
                    bCondition3 = true;
                }
                else {
                    nCount = 0;

                    if(neighbour[0][2] == 0 && neighbour[0][1] == 1)
                    {
                        nCount ++;
                    }

                    if(neighbour[0][1] == 0 && neighbour[1][1] == 1)
                    {
                        nCount ++;
                    }

                    if(neighbour[1][1] == 0 && neighbour[2][1] == 1)
                    {
                        nCount ++;
                    }

                    if(neighbour[2][1] == 0 && neighbour[2][2] == 1)
                    {
                        nCount ++;
                    }

                    if(neighbour[2][2] == 0 && neighbour[2][3] == 1)
                    {
                        nCount ++;
                    }

                    if(neighbour[2][3] == 0 && neighbour[1][3] == 1)
                    {
                        nCount ++;
                    }

                    if(neighbour[1][3] == 0 && neighbour[0][3] == 1)
                    {
                        nCount ++;
                    }

                    if(neighbour[0][3] == 0 && neighbour[0][2] == 1)
                    {
                        nCount ++;
                    }

                    if(nCount != 1)
                    {
                        bCondition3 = true;
                    }
                }

                //判断P2 x P4 x P6 = 0; 或者Z0(P4) != 1;
                if(neighbour[1][2] * neighbour[2][1] * neighbour[3][2] == 0)
                {
                    bCondition4 = true;
                }
                else {
                    nCount = 0;

                    if(neighbour[1][1] == 0 && neighbour[1][0] == 1)
                    {
                        nCount ++;
                    }

                    if(neighbour[1][0] == 0 && neighbour[2][0] == 1)
                    {
                        nCount ++;
                    }

                    if(neighbour[2][0] == 0 && neighbour[3][0] == 1)
                    {
                        nCount ++;
                    }

                    if(neighbour[3][0] == 0 && neighbour[3][1] == 1)
                    {
                        nCount ++;
                    }

                    if(neighbour[3][1] == 0 && neighbour[3][2] == 1)
                    {
                        nCount ++;
                    }

                    if(neighbour[3][2] == 0 && neighbour[2][2] == 1)
                    {
                        nCount ++;
                    }

                    if(neighbour[2][2] == 0 && neighbour[1][2] == 1)
                    {
                        nCount ++;
                    }

                    if(neighbour[1][2] == 0 && neighbour[1][1] == 1)
                    {
                        nCount ++;
                    }

                    if(nCount != 1)
                    {
                        bCondition4 = true;
                    }
                }

                //如果四个条件满足的话就进行细化
                if(bCondition1 && bCondition2 && bCondition3 && bCondition4)
                {
                    optImage.setPixel(j,i,qRgb(255,255,255));
                    bModified = true;
                }
                else {
                    optImage.setPixel(j,i,qRgb(0,0,0));
                }
            }
        }
    }


    return 0;
}

你可能感兴趣的:(图像处理)