Ask Your Question

Revision history [back]

I solve it by the following code:

#define BH_RAD_TO_DEG   180/CV_PI
#define BH_DEG_TO_RAD   CV_PI/180
void bh2Rad(CvMat* srcMat,CvMat* dstMat)
{
    for (int i=0; i < srcMat->rows ;i++)
    {
        float* srcP = srcMat->data.fl + srcMat->width * i;
        float* dstP = dstMat->data.fl + dstMat->width * i;

        for (int j = 0; j < srcMat->cols ;j++)
            dstP[j] = srcP[j] * BH_DEG_TO_RAD;
    }
}

void bh2Deg(CvMat* srcMat,CvMat* dstMat)
{
    for (int i=0; i < srcMat->rows ;i++)
    {
        float* srcP = srcMat->data.fl + srcMat->width * i;
        float* dstP = dstMat->data.fl + dstMat->width * i;

        for (int j = 0; j < srcMat->cols ;j++)
            dstP[j] = srcP[j] * BH_RAD_TO_DEG;
    }
}
void bhSin2Mat(CvMat* srcMat,CvMat* dstMat)
{
    for (int i=0; i < srcMat->rows;i++)
    {
        float* srcP = srcMat->data.fl + srcMat->width * i;
        float* dstP = dstMat->data.fl + dstMat->width * i;

        for (int j=0; j < srcMat->cols;j++)
            dstP[j] = sin(2 * srcP[j]);

    }
}
void bhCos2Mat(CvMat* srcMat,CvMat* dstMat)
{
    for (int i=0; i < srcMat->rows;i++)
    {
        float* srcP = srcMat->data.fl + srcMat->width * i;
        float* dstP = dstMat->data.fl + dstMat->width * i;

        for (int j=0; j < srcMat->cols;j++)
            dstP[j] = cos(2 * srcP[j]);

    }

}

void bhATanMat(CvMat* sinMat,CvMat* cosMat, CvMat* dstMat)
{
    for (int i=0; i < dstMat->rows;i++)
    {
        float* sinP = sinMat->data.fl + sinMat->width * i;
        float* cosP = cosMat->data.fl + cosMat->width * i;

        float* dstP = dstMat->data.fl + dstMat->width * i;

        for (int j=0; j < dstMat->cols;j++)
        {
            dstP[j] = float(atan2(sinP[j],cosP[j])/2 * BH_RAD_TO_DEG);
            if (dstP[j] < 0) dstP[j] = 180 + dstP[j];
        }
    }

}
void bhSmoothOrientation(CvMat* srcMat,CvMat* dstMat,bool normalize, int smoothType,int size1 ,int size2 ,double sigma1 ,double sigma2)
{
    double min_value,max_value;
    if (normalize)
    {
      cvMinMaxLoc(srcMat,&min_value,&max_value);
      cvNormalize(srcMat,dstMat,0,180,CV_MINMAX);
    }

    bh2Rad(dstMat,dstMat);

    CvMat* sinMat = cvCreateMat(dstMat->rows,dstMat->cols,CV_32FC1);
    CvMat* cosMat = cvCreateMat(dstMat->rows,dstMat->cols,CV_32FC1);

    bhSin2Mat(dstMat,sinMat);
    cvSmooth(sinMat,sinMat,smoothType, size1,size2,sigma1,sigma2);
    bhCos2Mat(dstMat,cosMat);
    cvSmooth(cosMat,cosMat,smoothType, size1,size2,sigma1,sigma2);

    bhATanMat(sinMat,cosMat,dstMat);

    bh2Deg(dstMat,dstMat);

    if (normalize)
      cvNormalize(srcMat,dstMat,min_value,max_value,CV_MINMAX);

    cvReleaseMat(&sinMat);
    cvReleaseMat(&cosMat);
}