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