Home > Net >  How to convert cv Mat to Objective C Mat * in OpenCV
How to convert cv Mat to Objective C Mat * in OpenCV

Time:05-12

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];

CodePudding user response:

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;

CodePudding user response:

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];
  • Related