2017-06-22 84 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