MatOfRotatedRect.mm 3.0 KB

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