'How to convert cv Mat to Objective C Mat * in OpenCV

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



Solution 1:[1]

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;

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matic Oblak
Solution 2 Sajal Gupta