2012-07-09 42 views
2

我有麻烦,下面的代码转换为javacv:如何OpenCV的转换javacv代码

((int*)(srcArg->imageData + srcArg->widthStep*P2.y))[P2.x]什么的这个tranlation对我来说,它看起来就像一个数组,但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; 
     } 
+2

这就意味着:开始与'srcArg-> imageData'指针,计算整数值'srcArg-> widthStep * P2.y'然后添加这个指针,使用的imageData结构为的宽度抵消。 ImageData看起来是一个char *指针,所以这是一个字节偏移量 - 从该数组中的srcArg-> widthStep * P2.y'中读取一个指针值,然后将其转换为整数数组,最后读取整数值“P2”。 x'来自该阵列。我不知道如何通过JavaCV处理这个问题,但是 - IplImage是一个真正的Java类吗?如果是这样,希望从imageData成员在那里显而易见。 – Rup 2012-07-09 14:11:32

+1

谢谢,IplImage是一个JavaCV类。 JavaCV也有一个BytePointer类。非常丑陋...但我会尝试转换它...感谢您的描述 – 2012-07-09 16:07:34

回答

3

好吧,我觉得平等的翻译是这样的: UPDATE:

私人浮动getMean(IplImage结构srcArg,CvPoint PixArg,INT邻居, int centerVal){ CvPoint P1 = new CvPoint(),P2 = new CvPoint(); 浮动值; (PixArg.x() - 邻域+ 1,PixArg.y() - 邻域+ 1); (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; 
} 
+0

酷!在'pos1'应该是'P2.y()',但?原来2-1-2-1的'pos'数字不是2-1-1-2,把P1和P2的xs和ys混合在一起,这意味着你可以用'pos1'和'pos2'离开并重用它们而不是计算3和4。 – Rup 2012-07-09 16:36:23

+1

哦..,你说得对。我更新我的代码 – 2012-07-09 17:17:32