Mat.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // Mat.h
  3. //
  4. // Created by Giles Payne on 2019/10/06.
  5. //
  6. #pragma once
  7. #ifdef __cplusplus
  8. #import "opencv2/core.hpp"
  9. #else
  10. #define CV_EXPORTS
  11. #endif
  12. #import <Foundation/Foundation.h>
  13. #ifdef AVAILABLE_IMGCODECS
  14. #if TARGET_OS_IPHONE
  15. #import <UIKit/UIKit.h>
  16. #elif TARGET_OS_MAC
  17. #import <AppKit/AppKit.h>
  18. #endif
  19. #endif
  20. @class Size2i;
  21. @class Scalar;
  22. @class Range;
  23. @class Rect2i;
  24. @class Point2i;
  25. NS_ASSUME_NONNULL_BEGIN
  26. /**
  27. The class Mat represents an n-dimensional dense numerical single-channel or multi-channel array.
  28. ####Swift Example
  29. ```swift
  30. let mat = Mat(rows: 2, cols: 3, type: CvType.CV_8U)
  31. try! mat.put(row: 0, col: 0, data: [2, 3, 4, 4, 5, 6] as [Int8])
  32. print("mat: \(mat.dump())")
  33. ```
  34. ####Objective-C Example
  35. ```objc
  36. Mat* mat = [[Mat alloc] initWithRows:2 cols:3 type: CV_8U];
  37. [m1 put:0 col:0 data:@[@2, @3, @4, @3, @4, @5]];
  38. NSLog(@"mat: %@", [m1 dump]);
  39. ```
  40. */
  41. CV_EXPORTS @interface Mat : NSObject
  42. #ifdef __cplusplus
  43. @property(readonly) cv::Ptr<cv::Mat> nativePtr;
  44. @property(readonly) cv::Mat& nativeRef;
  45. #endif
  46. #pragma mark - Constructors
  47. - (instancetype)init;
  48. #ifdef __cplusplus
  49. - (instancetype)initWithNativeMat:(cv::Ptr<cv::Mat>)nativeMat;
  50. + (instancetype)fromNativePtr:(cv::Ptr<cv::Mat>)nativePtr;
  51. + (instancetype)fromNative:(cv::Mat&)nativeRef;
  52. #endif
  53. /**
  54. Creates a Mat object with the specified number of rows and columns and Mat type
  55. @param rows Number of rows
  56. @param cols Number of columns
  57. @param type Mat type (refer: `CvType`)
  58. */
  59. - (instancetype)initWithRows:(int)rows cols:(int)cols type:(int)type;
  60. - (instancetype)initWithRows:(int)rows cols:(int)cols type:(int)type data:(NSData*)data;
  61. - (instancetype)initWithRows:(int)rows cols:(int)cols type:(int)type data:(NSData*)data step:(long)step;
  62. - (instancetype)initWithSize:(Size2i*)size type:(int)type;
  63. - (instancetype)initWithSizes:(NSArray<NSNumber*>*)sizes type:(int)type;
  64. - (instancetype)initWithRows:(int)rows cols:(int)cols type:(int)type scalar:(Scalar*)scalar;
  65. - (instancetype)initWithSize:(Size2i*)size type:(int)type scalar:(Scalar*)scalar;
  66. - (instancetype)initWithSizes:(NSArray<NSNumber*>*)sizes type:(int)type scalar:(Scalar*)scalar;
  67. - (instancetype)initWithMat:(Mat*)mat rowRange:(Range*)rowRange colRange:(Range*)colRange;
  68. - (instancetype)initWithMat:(Mat*)mat rowRange:(Range*)rowRange;
  69. - (instancetype)initWithMat:(Mat*)mat ranges:(NSArray<Range*>*)ranges;
  70. - (instancetype)initWithMat:(Mat*)mat rect:(Rect2i*)roi;
  71. #pragma mark - Mat operations
  72. - (Mat*)adjustRoiTop:(int)dtop bottom:(int)dbottom left:(int)dleft right:(int)dright NS_SWIFT_NAME(adjustRoi(top:bottom:left:right:));
  73. - (void)assignTo:(Mat*)mat type:(int)type;
  74. - (void)assignTo:(Mat*)mat;
  75. - (BOOL)isSameMat:(Mat*)mat;
  76. - (int)channels;
  77. - (int)checkVector:(int)elemChannels depth:(int)depth requireContinuous:(BOOL) requireContinuous NS_SWIFT_NAME(checkVector(elemChannels:depth:requireContinuous:));
  78. - (int)checkVector:(int)elemChannels depth:(int)depth NS_SWIFT_NAME(checkVector(elemChannels:depth:));
  79. - (int)checkVector:(int)elemChannels NS_SWIFT_NAME(checkVector(elemChannels:));
  80. - (Mat*)clone;
  81. - (Mat*)col:(int)x;
  82. - (Mat*)colRange:(int)start end:(int)end NS_SWIFT_NAME(colRange(start:end:));
  83. - (Mat*)colRange:(Range*)range;
  84. - (int)dims;
  85. - (int)cols;
  86. - (void)convertTo:(Mat*)mat rtype:(int)rtype alpha:(double)alpha beta:(double)beta;
  87. - (void)convertTo:(Mat*)mat rtype:(int)rtype alpha:(double)alpha;
  88. - (void)convertTo:(Mat*)mat rtype:(int)rtype;
  89. - (void)copyTo:(Mat*)mat;
  90. - (void)copyTo:(Mat*)mat mask:(Mat*)mask;
  91. - (void)create:(int)rows cols:(int)cols type:(int)type NS_SWIFT_NAME(create(rows:cols:type:));
  92. - (void)create:(Size2i*)size type:(int)type NS_SWIFT_NAME(create(size:type:));
  93. - (void)createEx:(NSArray<NSNumber*>*)sizes type:(int)type NS_SWIFT_NAME(create(sizes:type:));
  94. - (void)copySize:(Mat*)mat;
  95. - (Mat*)cross:(Mat*)mat;
  96. - (unsigned char*)dataPtr NS_SWIFT_NAME(dataPointer());
  97. - (int)depth;
  98. - (Mat*)diag:(int)diagonal;
  99. - (Mat*)diag;
  100. + (Mat*)diag:(Mat*)diagonal;
  101. - (double)dot:(Mat*)mat;
  102. - (long)elemSize;
  103. - (long)elemSize1;
  104. - (BOOL)empty;
  105. + (Mat*)eye:(int)rows cols:(int)cols type:(int)type NS_SWIFT_NAME(eye(rows:cols:type:));
  106. + (Mat*)eye:(Size2i*)size type:(int)type NS_SWIFT_NAME(eye(size:type:));
  107. - (Mat*)inv:(int)method;
  108. - (Mat*)inv;
  109. - (BOOL)isContinuous;
  110. - (BOOL)isSubmatrix;
  111. - (void)locateROI:(Size2i*)wholeSize ofs:(Point2i*)offset NS_SWIFT_NAME(locateROI(wholeSize:offset:));
  112. - (Mat*)mul:(Mat*)mat scale:(double)scale;
  113. /**
  114. Performs element-wise multiplication
  115. @param mat operand with with which to perform element-wise multiplication
  116. */
  117. - (Mat*)mul:(Mat*)mat;
  118. /**
  119. Performs matrix multiplication
  120. @param mat operand with with which to perform matrix multiplication
  121. @see `Core.gemm(...)`
  122. */
  123. - (Mat*)matMul:(Mat*)mat;
  124. + (Mat*)ones:(int)rows cols:(int)cols type:(int)type NS_SWIFT_NAME(ones(rows:cols:type:));
  125. + (Mat*)ones:(Size2i*)size type:(int)type NS_SWIFT_NAME(ones(size:type:));
  126. + (Mat*)onesEx:(NSArray<NSNumber*>*)sizes type:(int)type NS_SWIFT_NAME(ones(sizes:type:));
  127. - (void)push_back:(Mat*)mat;
  128. - (Mat*)reshape:(int)channels rows:(int)rows NS_SWIFT_NAME(reshape(channels:rows:));
  129. - (Mat*)reshape:(int)channels NS_SWIFT_NAME(reshape(channels:));
  130. - (Mat*)reshape:(int)channels newshape:(NSArray<NSNumber*>*)newshape NS_SWIFT_NAME(reshape(channels:newshape:));
  131. - (Mat*)row:(int)y;
  132. - (Mat*)rowRange:(int)start end:(int)end NS_SWIFT_NAME(rowRange(start:end:));
  133. - (Mat*)rowRange:(Range*)range;
  134. - (int)rows;
  135. - (Mat*)setToScalar:(Scalar*)scalar NS_SWIFT_NAME(setTo(scalar:));
  136. - (Mat*)setToScalar:(Scalar*)scalar mask:(Mat*)mask NS_SWIFT_NAME(setTo(scalar:mask:));
  137. - (Mat*)setToValue:(Mat*)value mask:(Mat*)mask NS_SWIFT_NAME(setTo(value:mask:));
  138. - (Mat*)setToValue:(Mat*)value NS_SWIFT_NAME(setTo(value:));
  139. - (Size2i*)size;
  140. - (int)size:(int)dim;
  141. - (long)step1:(int)dim;
  142. - (long)step1;
  143. - (Mat*)submat:(int)rowStart rowEnd:(int)rowEnd colStart:(int)colStart colEnd:(int)colEnd NS_SWIFT_NAME(submat(rowStart:rowEnd:colStart:colEnd:));
  144. - (Mat*)submat:(Range*)rowRange colRange:(Range*)colRange NS_SWIFT_NAME(submat(rowRange:colRange:));
  145. - (Mat*)submat:(NSArray<Range*>*)ranges NS_SWIFT_NAME(submat(ranges:));
  146. - (Mat*)submatRoi:(Rect2i*)roi NS_SWIFT_NAME(submat(roi:));
  147. - (Mat*)t;
  148. - (long)total;
  149. - (int)type;
  150. + (Mat*)zeros:(int)rows cols:(int)cols type:(int)type;
  151. + (Mat*)zeros:(Size2i*)size type:(int)type;
  152. + (Mat*)zerosEx:(NSArray<NSNumber*>*)sizes type:(int)type NS_SWIFT_NAME(zeros(sizes:type:));
  153. - (NSString*)description;
  154. - (NSString*)dump;
  155. - (int)height;
  156. - (int)width;
  157. #pragma mark - Accessors
  158. - (int)put:(int)row col:(int)col data:(NSArray<NSNumber*>*)data NS_REFINED_FOR_SWIFT;
  159. - (int)put:(NSArray<NSNumber*>*)indices data:(NSArray<NSNumber*>*)data NS_REFINED_FOR_SWIFT;
  160. - (int)get:(int)row col:(int)col data:(NSMutableArray<NSNumber*>*)data NS_REFINED_FOR_SWIFT;
  161. - (int)get:(NSArray<NSNumber*>*)indices data:(NSMutableArray<NSNumber*>*)data NS_REFINED_FOR_SWIFT;
  162. - (NSArray<NSNumber*>*)get:(int)row col:(int)col NS_REFINED_FOR_SWIFT;
  163. - (NSArray<NSNumber*>*)get:(NSArray<NSNumber*>*)indices NS_REFINED_FOR_SWIFT;
  164. - (int)get:(NSArray<NSNumber*>*)indices count:(int)count byteBuffer:(char*)buffer NS_REFINED_FOR_SWIFT;
  165. - (int)get:(NSArray<NSNumber*>*)indices count:(int)count doubleBuffer:(double*)buffer NS_REFINED_FOR_SWIFT;
  166. - (int)get:(NSArray<NSNumber*>*)indices count:(int)count floatBuffer:(float*)buffer NS_REFINED_FOR_SWIFT;
  167. - (int)get:(NSArray<NSNumber*>*)indices count:(int)count intBuffer:(int*)buffer NS_REFINED_FOR_SWIFT;
  168. - (int)get:(NSArray<NSNumber*>*)indices count:(int)count shortBuffer:(short*)buffer NS_REFINED_FOR_SWIFT;
  169. - (int)put:(NSArray<NSNumber*>*)indices count:(int)count byteBuffer:(const char*)buffer NS_REFINED_FOR_SWIFT;
  170. - (int)put:(NSArray<NSNumber*>*)indices count:(int)count doubleBuffer:(const double*)buffer NS_REFINED_FOR_SWIFT;
  171. - (int)put:(NSArray<NSNumber*>*)indices count:(int)count floatBuffer:(const float*)buffer NS_REFINED_FOR_SWIFT;
  172. - (int)put:(NSArray<NSNumber*>*)indices count:(int)count intBuffer:(const int*)buffer NS_REFINED_FOR_SWIFT;
  173. - (int)put:(NSArray<NSNumber*>*)indices count:(int)count shortBuffer:(const short*)buffer NS_REFINED_FOR_SWIFT;
  174. #pragma mark - Converters
  175. #ifdef AVAILABLE_IMGCODECS
  176. - (CGImageRef)toCGImage CF_RETURNS_RETAINED;
  177. - (instancetype)initWithCGImage:(CGImageRef)image;
  178. - (instancetype)initWithCGImage:(CGImageRef)image alphaExist:(BOOL)alphaExist;
  179. #if TARGET_OS_IPHONE
  180. - (UIImage*)toUIImage;
  181. - (instancetype)initWithUIImage:(UIImage*)image;
  182. - (instancetype)initWithUIImage:(UIImage*)image alphaExist:(BOOL)alphaExist;
  183. #elif TARGET_OS_MAC
  184. - (NSImage*)toNSImage;
  185. - (instancetype)initWithNSImage:(NSImage*)image;
  186. - (instancetype)initWithNSImage:(NSImage*)image alphaExist:(BOOL)alphaExist;
  187. #endif
  188. #endif
  189. #pragma mark - QuickLook
  190. #ifdef AVAILABLE_IMGCODECS
  191. - (id)debugQuickLookObject;
  192. #endif
  193. @end
  194. NS_ASSUME_NONNULL_END