Converters.mm 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // Converters.mm
  3. //
  4. // Created by Giles Payne on 31/05/2020.
  5. //
  6. #import "Converters.h"
  7. #import "ArrayUtil.h"
  8. #import "MatOfPoint2i.h"
  9. #import "MatOfPoint2f.h"
  10. #import "MatOfPoint3.h"
  11. #import "MatOfPoint3f.h"
  12. #import "MatOfFloat.h"
  13. #import "MatOfByte.h"
  14. #import "MatOfInt.h"
  15. #import "MatOfDouble.h"
  16. #import "MatOfRect2i.h"
  17. #import "MatOfRect2d.h"
  18. #import "MatOfKeyPoint.h"
  19. #import "MatOfDMatch.h"
  20. #import "MatOfRotatedRect.h"
  21. @implementation Converters
  22. + (Mat*)vector_Point_to_Mat:(NSArray<Point2i*>*)pts {
  23. return [[MatOfPoint2i alloc] initWithArray:pts];
  24. }
  25. + (NSArray<Point2i*>*)Mat_to_vector_Point:(Mat*)mat {
  26. return [[[MatOfPoint2i alloc] initWithMat:mat] toArray];
  27. }
  28. + (Mat*)vector_Point2f_to_Mat:(NSArray<Point2f*>*)pts {
  29. return [[MatOfPoint2f alloc] initWithArray:pts];
  30. }
  31. + (NSArray<Point2f*>*)Mat_to_vector_Point2f:(Mat*)mat {
  32. return [[[MatOfPoint2f alloc] initWithMat:mat] toArray];
  33. }
  34. + (Mat*)vector_Point2d_to_Mat:(NSArray<Point2d*>*)pts {
  35. Mat* res = [[Mat alloc] initWithRows:(int)pts.count cols:1 type:CV_64FC2];
  36. NSMutableArray<NSNumber*>* buff = [NSMutableArray arrayWithCapacity:pts.count*2];
  37. for (Point2d* pt in pts) {
  38. [buff addObject:[NSNumber numberWithDouble:pt.x]];
  39. [buff addObject:[NSNumber numberWithDouble:pt.y]];
  40. }
  41. [res put:0 col:0 data:buff];
  42. return res;
  43. }
  44. + (NSArray<Point2d*>*)Mat_to_vector_Point2d:(Mat*)mat {
  45. if (mat.cols != 1 || mat.type != CV_64FC2) {
  46. NSException* exception = [NSException
  47. exceptionWithName:@"UnsupportedOperationException"
  48. reason:[NSString stringWithFormat:@"Invalid Mat. Mat must be of type CV_64FC2 and have 1 column."]
  49. userInfo:nil];
  50. @throw exception;
  51. }
  52. NSMutableArray<Point2d*>* ret = [NSMutableArray new];
  53. NSMutableArray<NSNumber*>* buff = createArrayWithSize(mat.rows*2, [NSNumber numberWithInt:0]);
  54. [mat get:0 col:0 data:buff];
  55. for (int i = 0; i < mat.rows; i++) {
  56. [ret addObject:[[Point2d alloc] initWithX:buff[i * 2].doubleValue y:buff[i * 2 + 1].doubleValue]];
  57. }
  58. return ret;
  59. }
  60. + (Mat*)vector_Point3i_to_Mat:(NSArray<Point3i*>*)pts {
  61. return [[MatOfPoint3 alloc] initWithArray:pts];
  62. }
  63. + (NSArray<Point3i*>*)Mat_to_vector_Point3i:(Mat*)mat {
  64. return [[[MatOfPoint3 alloc] initWithMat:mat] toArray];
  65. }
  66. + (Mat*)vector_Point3f_to_Mat:(NSArray<Point3f*>*)pts {
  67. return [[MatOfPoint3f alloc] initWithArray:pts];
  68. }
  69. + (NSArray<Point3f*>*)Mat_to_vector_Point3f:(Mat*)mat {
  70. return [[[MatOfPoint3f alloc] initWithMat:mat] toArray];
  71. }
  72. + (Mat*)vector_Point3d_to_Mat:(NSArray<Point3d*>*)pts {
  73. Mat* res = [[Mat alloc] initWithRows:(int)pts.count cols:1 type:CV_64FC3];
  74. NSMutableArray<NSNumber*>* buff = [NSMutableArray arrayWithCapacity:pts.count*3];
  75. for (Point3d* pt in pts) {
  76. [buff addObject:[NSNumber numberWithDouble:pt.x]];
  77. [buff addObject:[NSNumber numberWithDouble:pt.y]];
  78. [buff addObject:[NSNumber numberWithDouble:pt.z]];
  79. }
  80. [res put:0 col:0 data:buff];
  81. return res;
  82. }
  83. + (NSArray<Point3d*>*)Mat_to_vector_Point3d:(Mat*)mat {
  84. if (mat.cols != 1 || mat.type != CV_64FC3) {
  85. NSException* exception = [NSException
  86. exceptionWithName:@"UnsupportedOperationException"
  87. reason:[NSString stringWithFormat:@"Invalid Mat. Mat must be of type CV_64FC3 and have 1 column."]
  88. userInfo:nil];
  89. @throw exception;
  90. }
  91. NSMutableArray<Point3d*>* ret = [NSMutableArray new];
  92. NSMutableArray<NSNumber*>* buff = createArrayWithSize(mat.rows*3, [NSNumber numberWithInt:0]);
  93. [mat get:0 col:0 data:buff];
  94. for (int i = 0; i < mat.rows; i++) {
  95. [ret addObject:[[Point3d alloc] initWithX:buff[i * 3].doubleValue y:buff[i * 3 + 1].doubleValue z:buff[i * 3 + 2].doubleValue]];
  96. }
  97. return ret;
  98. }
  99. + (Mat*)vector_float_to_Mat:(NSArray<NSNumber*>*)fs {
  100. return [[MatOfFloat alloc] initWithArray:fs];
  101. }
  102. + (NSArray<NSNumber*>*)Mat_to_vector_float:(Mat*)mat {
  103. return [[[MatOfFloat alloc] initWithMat:mat] toArray];
  104. }
  105. + (Mat*)vector_uchar_to_Mat:(NSArray<NSNumber*>*)us {
  106. return [[MatOfByte alloc] initWithArray:us];
  107. }
  108. + (NSArray<NSNumber*>*)Mat_to_vector_uchar:(Mat*)mat {
  109. return [[[MatOfByte alloc] initWithMat:mat] toArray];
  110. }
  111. + (Mat*)vector_char_to_Mat:(NSArray<NSNumber*>*)cs {
  112. Mat* res = [[Mat alloc] initWithRows:(int)cs.count cols:1 type:CV_8S];
  113. [res put:0 col:0 data:cs];
  114. return res;
  115. }
  116. + (NSArray<NSNumber*>*)Mat_to_vector_char:(Mat*)mat {
  117. if (mat.cols != 1 || mat.type != CV_8S) {
  118. NSException* exception = [NSException
  119. exceptionWithName:@"UnsupportedOperationException"
  120. reason:[NSString stringWithFormat:@"Invalid Mat. Mat must be of type CV_8S and have 1 column."]
  121. userInfo:nil];
  122. @throw exception;
  123. }
  124. NSMutableArray<NSNumber*>* ret = createArrayWithSize(mat.rows, @0);
  125. [mat get:0 col:0 data:ret];
  126. return ret;
  127. }
  128. + (Mat*)vector_int_to_Mat:(NSArray<NSNumber*>*)is {
  129. return [[MatOfInt alloc] initWithArray:is];
  130. }
  131. + (NSArray<NSNumber*>*)Mat_to_vector_int:(Mat*)mat {
  132. return [[[MatOfInt alloc] initWithMat:mat] toArray];
  133. }
  134. + (Mat*)vector_Rect_to_Mat:(NSArray<Rect2i*>*)rs {
  135. return [[MatOfRect2i alloc] initWithArray:rs];
  136. }
  137. + (NSArray<Rect2i*>*)Mat_to_vector_Rect:(Mat*)mat {
  138. return [[[MatOfRect2i alloc] initWithMat:mat] toArray];
  139. }
  140. + (Mat*)vector_Rect2d_to_Mat:(NSArray<Rect2d*>*)rs {
  141. return [[MatOfRect2d alloc] initWithArray:rs];
  142. }
  143. + (NSArray<Rect2d*>*)Mat_to_vector_Rect2d:(Mat*)mat {
  144. return [[[MatOfRect2d alloc] initWithMat:mat] toArray];
  145. }
  146. + (Mat*)vector_KeyPoint_to_Mat:(NSArray<KeyPoint*>*)kps {
  147. return [[MatOfKeyPoint alloc] initWithArray:kps];
  148. }
  149. + (NSArray<KeyPoint*>*)Mat_to_vector_KeyPoint:(Mat*)mat {
  150. return [[[MatOfKeyPoint alloc] initWithMat:mat] toArray];
  151. }
  152. + (Mat*)vector_double_to_Mat:(NSArray<NSNumber*>*)ds {
  153. return [[MatOfDouble alloc] initWithArray:ds];
  154. }
  155. + (NSArray<NSNumber*>*)Mat_to_vector_double:(Mat*)mat {
  156. return [[[MatOfDouble alloc] initWithMat:mat] toArray];
  157. }
  158. + (Mat*)vector_DMatch_to_Mat:(NSArray<DMatch*>*)matches {
  159. return [[MatOfDMatch alloc] initWithArray:matches];
  160. }
  161. + (NSArray<DMatch*>*)Mat_to_vector_DMatch:(Mat*)mat {
  162. return [[[MatOfDMatch alloc] initWithMat:mat] toArray];
  163. }
  164. + (Mat*)vector_RotatedRect_to_Mat:(NSArray<RotatedRect*>*)rs {
  165. return [[MatOfRotatedRect alloc] initWithArray:rs];
  166. }
  167. + (NSArray<RotatedRect*>*)Mat_to_vector_RotatedRect:(Mat*)mat {
  168. return [[[MatOfRotatedRect alloc] initWithMat:mat] toArray];
  169. }
  170. @end