MatOfKeyPoint.mm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // MatOfKeyPoint.m
  3. //
  4. // Created by Giles Payne on 2019/12/27.
  5. //
  6. #import "MatOfKeyPoint.h"
  7. #import "Range.h"
  8. #import "Point2f.h"
  9. #import "KeyPoint.h"
  10. #import "CvType.h"
  11. #import "ArrayUtil.h"
  12. @implementation MatOfKeyPoint
  13. static const int _depth = CV_32F;
  14. static const int _channels = 7;
  15. #ifdef __cplusplus
  16. - (instancetype)initWithNativeMat:(cv::Mat*)nativeMat {
  17. self = [super initWithNativeMat:nativeMat];
  18. if (self && ![self empty] && [self checkVector:_channels depth:_depth] < 0) {
  19. @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"Incompatible Mat" userInfo:nil];
  20. }
  21. return self;
  22. }
  23. #endif
  24. - (instancetype)initWithMat:(Mat*)mat {
  25. self = [super initWithMat:mat rowRange:[Range all]];
  26. if (self && ![self empty] && [self checkVector:_channels depth:_depth] < 0) {
  27. @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"Incompatible Mat" userInfo:nil];
  28. }
  29. return self;
  30. }
  31. - (instancetype)initWithArray:(NSArray<KeyPoint*>*)array {
  32. self = [super init];
  33. if (self) {
  34. [self fromArray:array];
  35. }
  36. return self;
  37. }
  38. - (void)alloc:(int)elemNumber {
  39. if (elemNumber>0) {
  40. [super create:elemNumber cols:1 type:[CvType makeType:_depth channels:_channels]];
  41. }
  42. }
  43. - (void)fromArray:(NSArray<KeyPoint*>*)array {
  44. NSMutableArray<NSNumber*>* data = [[NSMutableArray alloc] initWithCapacity:array.count * _channels];
  45. for (int index = 0; index < (int)array.count; index++) {
  46. data[_channels * index] = [NSNumber numberWithFloat:array[index].pt.x];
  47. data[_channels * index + 1] = [NSNumber numberWithFloat:array[index].pt.y];
  48. data[_channels * index + 2] = [NSNumber numberWithFloat:array[index].size];
  49. data[_channels * index + 3] = [NSNumber numberWithFloat:array[index].angle];
  50. data[_channels * index + 4] = [NSNumber numberWithFloat:array[index].response];
  51. data[_channels * index + 5] = [NSNumber numberWithFloat:array[index].octave];
  52. data[_channels * index + 6] = [NSNumber numberWithFloat:array[index].classId];
  53. }
  54. [self alloc:(int)array.count];
  55. [self put:0 col:0 data:data];
  56. }
  57. - (NSArray<KeyPoint*>*)toArray {
  58. int length = [self length] / _channels;
  59. NSMutableArray<KeyPoint*>* ret = createArrayWithSize(length, [KeyPoint new]);
  60. if (length > 0) {
  61. NSMutableArray<NSNumber*>* data = createArrayWithSize([self length], @0.0);
  62. [self get:0 col:0 data:data];
  63. for (int index = 0; index < length; index++) {
  64. ret[index] = [[KeyPoint alloc] initWithX:data[index * _channels].floatValue y:data[index * _channels + 1].floatValue size:data[index * _channels + 2].floatValue angle:data[index * _channels + 3].floatValue response:data[index * _channels + 4].floatValue octave:data[index * _channels + 5].intValue classId:data[index * _channels + 6].intValue];
  65. }
  66. }
  67. return ret;
  68. }
  69. - (int)length {
  70. int num = [self checkVector:_channels depth:_depth];
  71. if (num < 0) {
  72. @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Incompatible Mat" userInfo:nil];
  73. }
  74. return num * _channels;
  75. }
  76. @end