如何将opencv转换为javacv代码

时间:2012-07-09 14:00:50

标签: java c++ opencv javacv code-translation

我无法将以下代码转换为javacv:

((int*)(srcArg->imageData + srcArg->widthStep*P2.y))[P2.x]对此进行了翻译?对我来说,它看起来像一个数组,但value的类型是浮点数。

我不知道如何将值设置为value

float Saliency::getMean(IplImage * srcArg, CvPoint PixArg, int neighbourhood,    int centerVal)
{
CvPoint P1, P2;
float value;

P1.x = PixArg.x - neighbourhood + 1;
P1.y = PixArg.y - neighbourhood + 1;
P2.x = PixArg.x + neighbourhood + 1;
P2.y = PixArg.y + neighbourhood + 1;

if(P1.x < 0)
    P1.x = 0;
else if(P1.x > srcArg->width - 1)
    P1.x = srcArg->width - 1;
if(P2.x < 0)
    P2.x = 0;
else if(P2.x > srcArg->width - 1)
    P2.x = srcArg->width - 1;
if(P1.y < 0)
    P1.y = 0;
else if(P1.y > srcArg->height - 1)
    P1.y = srcArg->height - 1;
if(P2.y < 0)
    P2.y = 0;
else if(P2.y > srcArg->height - 1)
    P2.y = srcArg->height - 1;

// we use the integral image to compute fast features
value = (float) (
        ((int*)(srcArg->imageData + srcArg->widthStep*P2.y))[P2.x] +
        ((int*)(srcArg->imageData + srcArg->widthStep*P1.y))[P1.x] -
        ((int*)(srcArg->imageData + srcArg->widthStep*P2.y))[P1.x] -
        ((int*)(srcArg->imageData + srcArg->widthStep*P1.y))[P2.x] 
);

value = (value - centerVal)/  (( (P2.x - P1.x) * (P2.y - P1.y))-1)  ;

return value;

}

我的翻译:

 
        private float getMean(IplImage srcArg, CvPoint PixArg, int neighbourhood, int centerVal)
        {
         CvPoint P1, P2;
         float value;
         P1.put( PixArg.x() - neighbourhood + 1, PixArg.y() - neighbourhood + 1);
         P2.put( PixArg.x() + neighbourhood + 1, PixArg.y() + neighbourhood + 1);


         if(P1.x() < 0)
             P1.position(0).put(0);
         else if(P1.x() > srcArg.width()- 1)
             P1.position(0).put(srcArg.width()- 1);
         if(P2.x() < 0)
             P2.position(0).put(0);
         else if(P2.x() > srcArg.width()- 1)
             P2.position(0).put(srcArg.width()- 1);
         if(P1.y() < 0)
             P1.position(1).put(0);
         else if(P1.y() > srcArg.height()- 1)
             P1.position(1).put(srcArg.height()- 1);
         if(P2.y() < 0)
             P2.position(1).put(0);
         else if(P2.y() > srcArg.height()- 1)
             P2.position(1).put(srcArg.height()- 1);

         // we use the integral image to compute fast features
         value = (float) (
                 ((int)(srcArg.imageData().get() + srcArg.widthStep()*P2.y()))[P2.x()] +
                 ((int)(srcArg.imageData().get()  + srcArg.widthStep()*P1.y()))[P1.x()] -
                 ((int)(srcArg.imageData().get()  + srcArg.widthStep()*P2.y()))[P1.x()] -
                 ((int)(srcArg.imageData().get()  + srcArg.widthStep()*P1.y()))[P2.x()] );
            );
         float[] bla= (srcArg.imageData().get()+srcArg.widthStep())[P2.x()];
         value = (value - centerVal)/  (( (P2.x - P1.x) * (P2.y - P1.y))-1)  ;

         return value;
        }

1 个答案:

答案 0 :(得分:3)

好吧,我认为平等的翻译是这样的: UPDATE:

private float getMean(IplImage srcArg,CvPoint PixArg,int neighborhood,             int centerVal){         CvPoint P1 = new CvPoint(),P2 = new CvPoint();         浮动值;         P1.put(PixArg.x() - 邻域+ 1,PixArg.y() - 邻域+ 1);         P2.put(PixArg.x()+邻域+ 1,PixArg.y()+邻域+ 1);

    if (P1.x() < 0)
        P1.x(0);
    else if (P1.x() > srcArg.width() - 1)
        P1.x(srcArg.width() - 1);
    if (P2.x() < 0)
        P2.x(0);
    else if (P2.x() > srcArg.width() - 1)
        P2.x(srcArg.width() - 1);
    if (P1.y() < 0)
        P1.y(0);
    else if (P1.y() > srcArg.height() - 1)
        P1.y(srcArg.height() - 1);
    if (P2.y() < 0)
        P2.y(0);
    else if (P2.y() > srcArg.height() - 1)
        P2.y(srcArg.height() - 1);
    BytePointer src = new BytePointer(srcArg.getByteBuffer());

    int pos1 = (int) srcArg.widthStep() * P2.y();
    int pos2 = (int) (srcArg.widthStep() * P1.y());
    int pos3 = (int) (srcArg.widthStep() * P2.y());
    int pos4 = (int) (srcArg.widthStep() * P1.y());
    value = (float) (src.position(pos1).get(P2.x())
            + src.position(pos2).get(P1.x())
            - src.position(pos3).get(P1.x())
            - src.position(pos4).get(P2.x()));

    value = (value - centerVal)
            / (((P2.x() - P1.x()) * (P2.y() - P1.y())) - 1);
    // System.out.println(value);
    return value;
}