Skip to content

Commit 089899d

Browse files
Merge pull request #7 from lukaszkurantdev/feat/point-vector-of-vectors
feat: point vector of vectors with changes in contours functions
2 parents 2afe090 + 55b39ae commit 089899d

File tree

10 files changed

+103
-15
lines changed

10 files changed

+103
-15
lines changed

cpp/FOCV_Function.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,8 +1183,10 @@ jsi::Object FOCV_Function::invoke(jsi::Runtime& runtime, const jsi::Value* argum
11831183

11841184
if(args.isMat(1)) {
11851185
rect = cv::boundingRect(*args.asMatPtr(1));
1186-
} else {
1186+
} else if(args.isMatVector(1)) {
11871187
rect = cv::boundingRect(*args.asMatVectorPtr(1));
1188+
} else {
1189+
rect = cv::boundingRect(*args.asPointVectorPtr(1));
11881190
}
11891191

11901192
std::string id = FOCV_Storage::save(rect);
@@ -1208,10 +1210,15 @@ jsi::Object FOCV_Function::invoke(jsi::Runtime& runtime, const jsi::Value* argum
12081210
value.setProperty(runtime, "value", jsi::Value(result));
12091211
} break;
12101212
case hashString("contourArea", 11): {
1211-
auto src = args.asMatPtr(1);
12121213
auto oriented = args.asBool(2);
12131214

1214-
value.setProperty(runtime, "value", contourArea(*src, oriented));
1215+
if(args.isMat(1)) {
1216+
auto src = args.asMatPtr(1);
1217+
value.setProperty(runtime, "value", contourArea(*src, oriented));
1218+
} else {
1219+
auto src = args.asPointVectorPtr(1);
1220+
value.setProperty(runtime, "value", contourArea(*src, oriented));
1221+
}
12151222
} break;
12161223
case hashString("convexHull", 10): {
12171224
auto src = args.asMatPtr(1);
@@ -1228,11 +1235,16 @@ jsi::Object FOCV_Function::invoke(jsi::Runtime& runtime, const jsi::Value* argum
12281235
} break;
12291236
case hashString("findContours", 12): {
12301237
auto src = args.asMatPtr(1);
1231-
auto dst = args.asMatVectorPtr(2);
12321238
auto mode = args.asNumber(3);
12331239
auto method = args.asNumber(4);
1234-
1235-
cv::findContours(*src, *dst, mode, method);
1240+
1241+
if(args.isMatVector(2)) {
1242+
auto dst = args.asMatVectorPtr(2);
1243+
cv::findContours(*src, *dst, mode, method);
1244+
} else {
1245+
auto dst = args.asPointVectorOfVectorsPtr(2);
1246+
cv::findContours(*src, *dst, mode, method);
1247+
}
12361248
} break;
12371249
case hashString("fitLine", 7): {
12381250
auto points = args.asMatPtr(1);

cpp/FOCV_FunctionArguments.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ std::shared_ptr<std::vector<cv::Point>> FOCV_FunctionArguments::asPointVectorPtr
4343
return FOCV_Storage::get<std::vector<cv::Point>>(FOCV_JsiObject::id_from_wrap(*this->runtime, arguments[index]));
4444
}
4545

46+
std::shared_ptr<std::vector<std::vector<cv::Point>>> FOCV_FunctionArguments::asPointVectorOfVectorsPtr(int index) {
47+
return FOCV_Storage::get<std::vector<std::vector<cv::Point>>>(FOCV_JsiObject::id_from_wrap(*this->runtime, arguments[index]));
48+
}
49+
4650
std::shared_ptr<cv::Rect> FOCV_FunctionArguments::asRectPtr(int index) {
4751
return FOCV_Storage::get<cv::Rect>(FOCV_JsiObject::id_from_wrap(*this->runtime, arguments[index]));
4852
}
@@ -82,3 +86,7 @@ bool FOCV_FunctionArguments::isObject(int index) {
8286
bool FOCV_FunctionArguments::isMat(int index) {
8387
return FOCV_JsiObject::type_from_wrap(*this->runtime, arguments[index]) == "mat";
8488
}
89+
90+
bool FOCV_FunctionArguments::isMatVector(int index) {
91+
return FOCV_JsiObject::type_from_wrap(*this->runtime, arguments[index]) == "mat_vector";
92+
}

cpp/FOCV_FunctionArguments.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class FOCV_FunctionArguments {
4343
std::shared_ptr<std::vector<cv::Mat>> asMatVectorPtr(int index);
4444
std::shared_ptr<cv::Point> asPointPtr(int index);
4545
std::shared_ptr<std::vector<cv::Point>> asPointVectorPtr(int index);
46+
std::shared_ptr<std::vector<std::vector<cv::Point>>> asPointVectorOfVectorsPtr(int index);
4647
std::shared_ptr<cv::Rect> asRectPtr(int index);
4748
std::shared_ptr<std::vector<cv::Rect>> asRectVectorPtr(int index);
4849
std::shared_ptr<cv::Size> asSizePtr(int index);
@@ -54,6 +55,7 @@ class FOCV_FunctionArguments {
5455
bool isString(int index);
5556
bool isObject(int index);
5657
bool isMat(int index);
58+
bool isMatVector(int index);
5759
};
5860

5961
#endif /* FOCV_FunctionArguments_hpp */

cpp/FOCV_Object.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ jsi::Object FOCV_Object::create(jsi::Runtime& runtime, const jsi::Value* argumen
8888
std::vector<cv::Point> object;
8989
id = FOCV_Storage::save(object);
9090
} break;
91+
case hashString("point_vector_vector", 19): {
92+
std::vector<std::vector<cv::Point>> object;
93+
id = FOCV_Storage::save(object);
94+
} break;
9195
case hashString("size", 4): {
9296
int width = arguments[1].asNumber();
9397
int height = arguments[2].asNumber();
@@ -216,6 +220,28 @@ jsi::Object FOCV_Object::convertToJSI(jsi::Runtime& runtime, const jsi::Value* a
216220

217221
value.setProperty(runtime, "array", array);
218222
} break;
223+
case hashString("point_vector_vector", 19): {
224+
auto points_vector = *FOCV_Storage::get<std::vector<std::vector<cv::Point>>>(id);
225+
226+
auto vectors_array = jsi::Array(runtime, points_vector.size());
227+
228+
for (int i = 0; i < points_vector.size(); i++) {
229+
auto points = points_vector.at(i);
230+
auto array = jsi::Array(runtime, points.size());
231+
232+
for (int j = 0; j < points.size(); j++) {
233+
jsi::Object item(runtime);
234+
235+
item.setProperty(runtime, "x", jsi::Value(points.at(j).x));
236+
item.setProperty(runtime, "y", jsi::Value(points.at(j).y));
237+
array.setValueAtIndex(runtime, j, item);
238+
}
239+
240+
vectors_array.setValueAtIndex(runtime, i, array);
241+
}
242+
243+
value.setProperty(runtime, "array", vectors_array);
244+
} break;
219245
case hashString("size", 4): {
220246
auto size = *FOCV_Storage::get<cv::Size>(id);
221247

@@ -278,6 +304,12 @@ jsi::Object FOCV_Object::copyObjectFromVector(jsi::Runtime& runtime, const jsi::
278304
createdId = FOCV_Storage::save(point);
279305
return FOCV_JsiObject::wrap(runtime, "point", createdId);
280306
} break;
307+
case hashString("point_vector_vector", 19): {
308+
auto array = *FOCV_Storage::get<std::vector<std::vector<cv::Point>>>(vectorId);
309+
std::vector<cv::Point> point = array.at(index);
310+
createdId = FOCV_Storage::save(point);
311+
return FOCV_JsiObject::wrap(runtime, "point_vector", createdId);
312+
} break;
281313
}
282314

283315
return value;

docs/pages/apidetails.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ createObject(
1616
createObject(type: ObjectType.MatVector): MatVector;
1717
createObject(type: ObjectType.Point, x: number, y: number): Point;
1818
createObject(type: ObjectType.PointVector): PointVector;
19+
createObject(type: ObjectType.PointVectorOfVectors): PointVectorOfVectors;
1920
createObject(
2021
type: ObjectType.Rect,
2122
x: number,
@@ -49,6 +50,10 @@ Copies an object from a vector to a separate object.
4950
```js
5051
copyObjectFromVector(vector: MatVector, itemIndex: number): Mat;
5152
copyObjectFromVector(vector: PointVector, itemIndex: number): Point;
53+
copyObjectFromVector(
54+
vector: PointVectorOfVectors,
55+
itemIndex: number
56+
): PointVector;
5257
copyObjectFromVector(vector: RectVector, itemIndex: number): Rect;
5358
```
5459

@@ -77,6 +82,12 @@ toJSValue(pointVector: PointVector): {
7782
y: number;
7883
}[];
7984
};
85+
toJSValue(pointVector: PointVectorOfVectors): {
86+
array: {
87+
x: number;
88+
y: number;
89+
}[][];
90+
};
8091
toJSValue(rect: Rect): {
8192
x: number;
8293
y: number;

docs/pages/availablefunctions.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,7 +1998,7 @@ Calculates the up-right bounding rectangle of a point set or non-zero pixels of
19981998
@returns the minimal up-right bounding rectangle for the specified point set or non-zero pixels of gray-scale image
19991999

20002000
```js
2001-
invoke(name: 'boundingRect', array: Mat | MatVector): Rect;
2001+
invoke(name: 'boundingRect', array: Mat | MatVector | PointVector): Rect;
20022002
```
20032003

20042004
### connectedComponents
@@ -2042,7 +2042,7 @@ Calculates a contour area
20422042
```js
20432043
invoke(
20442044
name: 'contourArea',
2045-
contour: Mat | MatVector,
2045+
contour: Mat | MatVector | PointVector,
20462046
oriented?: boolean
20472047
): { value: number };
20482048
```
@@ -2082,7 +2082,7 @@ Finds contours in a binary image
20822082
invoke(
20832083
name: 'findContours',
20842084
image: Mat | MatVector,
2085-
contours: Mat | MatVector,
2085+
contours: MatVector | PointVectorOfVectors,
20862086
mode: RetrievalModes,
20872087
method: ContourApproximationModes
20882088
): void;

docs/pages/usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ You can do this e.g. after a calculation, or after a specific step. **Remember t
1515

1616
## Objects
1717

18-
The library currently supports the following OpenCV objects: **Mat, MatVector, Point, Point Vector, Rect, RectVector, Size, Scalar and RotatedRect.**
18+
The library currently supports the following OpenCV objects: **Mat, MatVector, Point, Point Vector, Point Vector of Vectors, Rect, RectVector, Size, Scalar and RotatedRect.**
1919

2020
> A vector is simply an array in which individual objects are held.
2121

src/functions/ImageProcessing/Shape.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ import type {
44
RetrievalModes,
55
ShapeMatchModes,
66
} from '../../constants/ImageProcessing';
7-
import type { Mat, MatVector, Rect, RotatedRect } from '../../objects/Objects';
7+
import type {
8+
Mat,
9+
MatVector,
10+
PointVector,
11+
PointVectorOfVectors,
12+
Rect,
13+
RotatedRect,
14+
} from '../../objects/Objects';
815

916
export type Shape = {
1017
/**
@@ -39,7 +46,7 @@ export type Shape = {
3946
* @param array Input gray-scale image or 2D point set, stored in std::vector or Mat.
4047
* @returns the minimal up-right bounding rectangle for the specified point set or non-zero pixels of gray-scale image
4148
*/
42-
invoke(name: 'boundingRect', array: Mat | MatVector): Rect;
49+
invoke(name: 'boundingRect', array: Mat | MatVector | PointVector): Rect;
4350

4451
/**
4552
* computes the connected components labeled image of boolean image
@@ -77,7 +84,7 @@ export type Shape = {
7784
*/
7885
invoke(
7986
name: 'contourArea',
80-
contour: Mat | MatVector,
87+
contour: Mat | MatVector | PointVector,
8188
oriented?: boolean
8289
): { value: number };
8390

@@ -110,8 +117,8 @@ export type Shape = {
110117
*/
111118
invoke(
112119
name: 'findContours',
113-
image: Mat | MatVector,
114-
contours: Mat | MatVector,
120+
image: Mat,
121+
contours: MatVector | PointVectorOfVectors,
115122
mode: RetrievalModes,
116123
method: ContourApproximationModes
117124
): void;

src/objects/ObjectType.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export enum ObjectType {
33
MatVector = 'mat_vector',
44
Point = 'point',
55
PointVector = 'point_vector',
6+
PointVectorOfVectors = 'point_vector_vector',
67
Rect = 'rect',
78
RectVector = 'rect_vector',
89
Size = 'size',

src/objects/Objects.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ export type Mat = { id: string; type: ObjectType.Mat };
55
export type MatVector = { id: string; type: ObjectType.MatVector };
66
export type Point = { id: string; type: ObjectType.Point };
77
export type PointVector = { id: string; type: ObjectType.PointVector };
8+
export type PointVectorOfVectors = {
9+
id: string;
10+
type: ObjectType.PointVectorOfVectors;
11+
};
812
export type Rect = { id: string; type: ObjectType.Rect };
913
export type RectVector = { id: string; type: ObjectType.RectVector };
1014
export type Size = { id: string; type: ObjectType.Size };
@@ -27,6 +31,7 @@ export type Objects = {
2731
createObject(type: ObjectType.MatVector): MatVector;
2832
createObject(type: ObjectType.Point, x: number, y: number): Point;
2933
createObject(type: ObjectType.PointVector): PointVector;
34+
createObject(type: ObjectType.PointVectorOfVectors): PointVectorOfVectors;
3035
createObject(
3136
type: ObjectType.Rect,
3237
x: number,
@@ -70,6 +75,12 @@ export type Objects = {
7075
y: number;
7176
}[];
7277
};
78+
toJSValue(pointVector: PointVectorOfVectors): {
79+
array: {
80+
x: number;
81+
y: number;
82+
}[][];
83+
};
7384
toJSValue(rect: Rect): {
7485
x: number;
7586
y: number;
@@ -97,5 +108,9 @@ export type Objects = {
97108

98109
copyObjectFromVector(vector: MatVector, itemIndex: number): Mat;
99110
copyObjectFromVector(vector: PointVector, itemIndex: number): Point;
111+
copyObjectFromVector(
112+
vector: PointVectorOfVectors,
113+
itemIndex: number
114+
): PointVector;
100115
copyObjectFromVector(vector: RectVector, itemIndex: number): Rect;
101116
};

0 commit comments

Comments
 (0)