skip to Main Content

I have instance of cvMat like cv::Mat sourceImage; Is it possible to convert this into Objective C Mat object like Mat *dst = [[Mat alloc] init];

2

Answers


  1. Chosen as BEST ANSWER

    I have figure it out by myself

    There is initialiser available in OpenCV it takes the C++ Mat instance type and create Objective C Mat instance.

    + (instancetype)fromNative:(cv::Mat&)nativeRef;
    
    Mat *objectiveC_Mat = [Mat fromNative: sourceImage];
    

  2. If you wish to create a custom ObjectiveC class that contains your structure you will either need to wrap around it or you will need to copy all values you are interested in.

    Wrapping should be pretty simple:

    Header:

    NS_ASSUME_NONNULL_BEGIN
    
    @interface Mat : NSObject
    
    @property (readonly) cv::Mat cvMat;
    
    - (id)init:(cv::Mat)inputData;
    
    @end
    
    NS_ASSUME_NONNULL_END
    

    Source

    @interface Mat ()
    
    @property cv::Mat internalValue;
    
    @end
    
    @implementation Mat
    
    - (id)init:(cv::Mat)inputData {
        if((self = [super init])) {
            self.internalValue = inputData;
        }
        return self;
    }
    
    - (cv::Mat)cvMat {
        return self.internalValue;
    }
    
    @end
    

    So this would make your code look like:

    Mat *dst = [[Mat alloc] init:sourceImage];
    cv::Mat rawImage = dst.cvMat;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search