2017-06-22 301 views
0

我正在尝试使用openCV的decomposeHomographyMat函数。我更像一个Swift的人,但为此我必须使用Objective C神秘的语言。功能的签名是使用OpenCV decomposeHomographyMat from Objective C

cv::decomposeHomographyMat(
    <#InputArray H#>, 
    <#InputArray K#>, 
    <#OutputArrayOfArrays rotations#>, 
    <#OutputArrayOfArrays translations#>, 
    <#OutputArrayOfArrays normals#> 
) 

我有一个输入K和H都是NSArray<NSNumber*> *_Nonnull与9个元素每个。

如何创建预期的InputArrayOutputArrayOfArrays,以及如何将OutputArrayOfArrays转换回Objective C可以传递给Swift的类型?

OpenCV Error: Assertion failed (0 <= i && i < (int)vv.size()) in getMat_, 
file .../opencv/modules/core/src/matrix.cpp, line 1287 

的代码是:调用cv::decomposeHomographyMat用时

===编辑===

试图广答案坠毁

+ (nullable NSArray<NSNumber*> *) decomposeHomography:(NSArray<NSNumber*> *_Nonnull) homography intrisics:(NSArray<NSNumber*> *_Nonnull) intrisics 
{ 
    cv::Mat rotations, translations, normals; 
    vector<vector<float> > H(3,vector<float>(3)); 
    for (int i = 0; i<3; ++i){ 
    for (int j = 0; j<3; ++j){ 
     H[i][j] = [homography[i*3 + j] floatValue]; 
    } 
    } 
    vector<vector<float> > K(3,vector<float>(3)); 
    for (int i = 0; i<3; ++i){ 
    for (int j = 0; j<3; ++j){ 
     K[i][j] = [intrisics[i*3 + j] floatValue]; 
    } 
    } 

    cv::decomposeHomographyMat(
       H, 
       K, 
       rotations, 
       translations, 
       normals 
); 
    NSLog(@"ok"); 
} 

回答

1

您需要NSArray转换为std::vector<vector<float> >和喂它到cv::decomposeHomographyMat。例如

- (std::vector<std::vector<float> >) getVectorFrom: (NSArray *) arr{ 

    std::vector<std::vector<float> > ret(3,vector<float>(3)); 

    for (int i = 0; i<3; ++i){ 
     for (int j = 0; j<3; ++j){ 
      ret[i][j] = [arr[i*3 + j] floatValue]; 
     } 
    } 
    return ret; 
} 

- (void) someFunction: (NSArray *)H with: (NSArray *)K{ 

    cv::Mat rotations, translations, normals; 

    cv::decomposeHomographyMat([self.getVectorFrom H], 
           [self.getVectorFrom K], 
           rotations, 
           translations, 
           normals); 
} 
+0

不幸的是它崩溃。我编辑了我的答案。 – Guig

+1

使用'std :: vector '进行旋转,平移,法线;和H,K的作品。感谢您让我走向正确的方向 – Guig