Skip to content

Commit 67d297a

Browse files
Merge pull request #12 from martinbooth/PerspectiveWarp
feat: add getPerspectiveTransform and warpPerspective
2 parents 95b5dec + 1d580ab commit 67d297a

File tree

10 files changed

+162
-0
lines changed

10 files changed

+162
-0
lines changed

cpp/FOCV_Function.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,16 @@ jsi::Object FOCV_Function::invoke(jsi::Runtime& runtime, const jsi::Value* argum
10801080

10811081
return FOCV_JsiObject::wrap(runtime, "mat", id);
10821082
} break;
1083+
case hashString("getPerspectiveTransform", 23): {
1084+
auto src = args.asPoint2fVectorPtr(1);
1085+
auto dest = args.asPoint2fVectorPtr(2);
1086+
auto solveMethod = args.asNumber(3);
1087+
1088+
cv::Mat result = cv::getPerspectiveTransform(*src, *dest, solveMethod);
1089+
std::string id = FOCV_Storage::save(result);
1090+
1091+
return FOCV_JsiObject::wrap(runtime, "mat", id);
1092+
} break;
10831093
case hashString("getStructuringElement", 21): {
10841094
auto shape = args.asNumber(1);
10851095
auto ksize = args.asSizePtr(2);
@@ -1339,6 +1349,17 @@ jsi::Object FOCV_Function::invoke(jsi::Runtime& runtime, const jsi::Value* argum
13391349

13401350
(*src).convertTo(*dst, rtype);
13411351
} break;
1352+
case hashString("warpPerspective", 15): {
1353+
auto src = args.asMatPtr(1);
1354+
auto dst = args.asMatPtr(2);
1355+
auto M = args.asMatPtr(3);
1356+
auto size = args.asSizePtr(4);
1357+
auto flags = args.asNumber(5);
1358+
auto borderMode = args.asNumber(6);
1359+
auto borderValue = args.asScalarPtr(7);
1360+
1361+
cv::warpPerspective(*src, *dst, *M, *size, flags, borderMode, *borderValue);
1362+
} break;
13421363
}
13431364
} catch (cv::Exception& e) {
13441365
std::cout << "Fast OpenCV Invoke Error: " << e.what() << "\n";

cpp/FOCV_FunctionArguments.cpp

+4
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<cv::Point2f>> FOCV_FunctionArguments::asPoint2fVectorPtr(int index) {
47+
return FOCV_Storage::get<std::vector<cv::Point2f>>(FOCV_JsiObject::id_from_wrap(*this->runtime, arguments[index]));
48+
}
49+
4650
std::shared_ptr<std::vector<std::vector<cv::Point>>> FOCV_FunctionArguments::asPointVectorOfVectorsPtr(int index) {
4751
return FOCV_Storage::get<std::vector<std::vector<cv::Point>>>(FOCV_JsiObject::id_from_wrap(*this->runtime, arguments[index]));
4852
}

cpp/FOCV_FunctionArguments.hpp

+1
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<cv::Point2f>> asPoint2fVectorPtr(int index);
4647
std::shared_ptr<std::vector<std::vector<cv::Point>>> asPointVectorOfVectorsPtr(int index);
4748
std::shared_ptr<cv::Rect> asRectPtr(int index);
4849
std::shared_ptr<std::vector<cv::Rect>> asRectVectorPtr(int index);

cpp/FOCV_Object.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,31 @@ 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("point2f", 7): {
92+
int x = arguments[1].asNumber();
93+
int y = arguments[2].asNumber();
94+
cv::Point2f object(x, y);
95+
id = FOCV_Storage::save(object);
96+
} break;
97+
case hashString("point2f_vector", 14): {
98+
std::vector<cv::Point2f> vec;
99+
if (arguments[1].isObject()) {
100+
auto rawArray = arguments[1].asObject(runtime);
101+
auto array = rawArray.asArray(runtime);
102+
103+
auto rawLength = rawArray.getProperty(runtime, "length");
104+
auto length = rawLength.asNumber();
105+
106+
for(auto i = 0; i < length; i++) {
107+
jsi::Value value = array.getValueAtIndex(runtime, i);
108+
std::string id = FOCV_JsiObject::id_from_wrap(runtime, value);
109+
auto point2fPtr = FOCV_Storage::get<cv::Point2f>(id);
110+
111+
vec.push_back(*point2fPtr);
112+
}
113+
}
114+
id = FOCV_Storage::save(vec);
115+
} break;
91116
case hashString("point_vector_vector", 19): {
92117
std::vector<std::vector<cv::Point>> object;
93118
id = FOCV_Storage::save(object);

docs/pages/availablefunctions.md

+41
Original file line numberDiff line numberDiff line change
@@ -1793,6 +1793,22 @@ invoke(
17931793
): Mat;
17941794
```
17951795

1796+
### getPerspectiveTransform
1797+
Calculates a perspective transform from four pairs of the corresponding points.
1798+
- name Function name.
1799+
- src Coordinates of quadrangle vertices in the source image.
1800+
- dst Coordinates of the corresponding quadrangle vertices in the destination image.
1801+
- solveMethod method passed to cv::solve (DecompTypes)
1802+
1803+
```js
1804+
invoke(
1805+
name: 'getPerspectiveTransform',
1806+
src: PointVector,
1807+
dst: PointVector,
1808+
solveMethod: DecompTypes
1809+
): Mat;
1810+
```
1811+
17961812
### getStructuringElement
17971813

17981814
Returns a structuring element of the specified size and shape for morphological operations.
@@ -2198,4 +2214,29 @@ Finds a rotated rectangle of the minimum area enclosing the input 2D point set.
21982214

21992215
```js
22002216
invoke(name: 'minAreaRect', points: Mat): RotatedRect;
2217+
```
2218+
2219+
2220+
### warpPerspective
2221+
Applies a perspective transformation to an image.
2222+
- name Function name.
2223+
- src input image.
2224+
- dst output image that has the size dsize and the same type as src .
2225+
- M 3x3 transformation matrix
2226+
- size size of the output image
2227+
- flags combination of interpolation methods (INTER_LINEAR or INTER_NEAREST) and the optional flag WARP_INVERSE_MAP, that sets M as the inverse transformation
2228+
- borderMode pixel extrapolation method (BORDER_CONSTANT or BORDER_REPLICATE).
2229+
- borderValue value used in case of a constant border; by default, it equals 0.
2230+
2231+
```js
2232+
invoke(
2233+
name: 'warpPerspective',
2234+
src: Mat,
2235+
dst: Mat,
2236+
M: Mat,
2237+
size: Size,
2238+
flags: InterpolationFlags,
2239+
borderMode: BorderTypes.BORDER_CONSTANT | BorderTypes.BORDER_REPLICATE,
2240+
borderValue: Scalar
2241+
): void;
22012242
```

src/constants/ImageTransform.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export enum InterpolationFlags {
2+
INTER_NEAREST = 0,
3+
INTER_LINEAR = 1,
4+
INTER_CUBIC = 2,
5+
INTER_AREA = 3,
6+
INTER_LANCZOS4 = 4,
7+
INTER_LINEAR_EXACT = 5,
8+
INTER_NEAREST_EXACT = 6,
9+
INTER_MAX = 7,
10+
WARP_FILL_OUTLIERS = 8,
11+
WARP_INVERSE_MAP = 16,
12+
WARP_RELATIVE_MAP = 32,
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { BorderTypes, DecompTypes } from '../../constants/Core';
2+
import type { InterpolationFlags } from '../../constants/ImageTransform';
3+
import type { Mat, PointVector, Scalar, Size } from '../../objects/Objects';
4+
5+
export type ImageTransform = {
6+
/**
7+
* Calculates a perspective transform from four pairs of the corresponding points.
8+
* @param name Function name.
9+
* @param src Coordinates of quadrangle vertices in the source image.
10+
* @param dst Coordinates of the corresponding quadrangle vertices in the destination image.
11+
* @param solveMethod method passed to cv::solve (DecompTypes)
12+
*/
13+
invoke(
14+
name: 'getPerspectiveTransform',
15+
src: PointVector,
16+
dst: PointVector,
17+
solveMethod: DecompTypes
18+
): Mat;
19+
20+
/**
21+
* Applies a perspective transformation to an image.
22+
* @param name Function name.
23+
* @param src input image.
24+
* @param dst output image that has the size dsize and the same type as src .
25+
* @param M 3x3 transformation matrix
26+
* @param size size of the output image
27+
* @param flags combination of interpolation methods (INTER_LINEAR or INTER_NEAREST) and the optional flag WARP_INVERSE_MAP, that sets M as the inverse transformation
28+
* @param borderMode pixel extrapolation method (BORDER_CONSTANT or BORDER_REPLICATE).
29+
* @param borderValue value used in case of a constant border; by default, it equals 0.
30+
*/
31+
invoke(
32+
name: 'warpPerspective',
33+
src: Mat,
34+
dst: Mat,
35+
M: Mat,
36+
size: Size,
37+
flags: InterpolationFlags,
38+
borderMode: BorderTypes.BORDER_CONSTANT | BorderTypes.BORDER_REPLICATE,
39+
borderValue: Scalar
40+
): void;
41+
};

src/index.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { ColorConversion } from './functions/ColorConversion';
1010
import type { Core } from './functions/Core';
1111
import type { UtilsFunctions } from './utils/UtilsFunctions';
1212
import type { Objects } from './objects/Objects';
13+
import type { ImageTransform } from './functions/ImageProcessing/ImageTransform';
1314

1415
const LINKING_ERROR =
1516
`The package 'react-native-fast-opencv' doesn't seem to be linked. Make sure: \n\n` +
@@ -53,6 +54,7 @@ export type OpenCVModel = ColorMap &
5354
Drawing &
5455
Feature &
5556
ImageFiltering &
57+
ImageTransform &
5658
Misc &
5759
ObjectDetection &
5860
Shape &
@@ -66,4 +68,5 @@ export type * from './objects/Objects';
6668
export * from './constants/ColorConversionsCodes';
6769
export * from './constants/DataTypes';
6870
export * from './constants/ImageProcessing';
71+
export * from './constants/ImageTransform';
6972
export * from './constants/Core';

src/objects/ObjectType.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ export enum ObjectType {
22
Mat = 'mat',
33
MatVector = 'mat_vector',
44
Point = 'point',
5+
Point2f = 'point2f',
56
PointVector = 'point_vector',
67
PointVectorOfVectors = 'point_vector_vector',
8+
Point2fVector = 'point2f_vector',
79
Rect = 'rect',
810
RectVector = 'rect_vector',
911
Size = 'size',

src/objects/Objects.ts

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ 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 Point2f = { id: string; type: ObjectType.Point2f };
9+
export type Point2fVector = { id: string; type: ObjectType.Point2fVector };
810
export type PointVectorOfVectors = {
911
id: string;
1012
type: ObjectType.PointVectorOfVectors;
@@ -31,6 +33,8 @@ export type Objects = {
3133
createObject(type: ObjectType.MatVector): MatVector;
3234
createObject(type: ObjectType.Point, x: number, y: number): Point;
3335
createObject(type: ObjectType.PointVector): PointVector;
36+
createObject(type: ObjectType.Point2f, x: number, y: number): Point2f;
37+
createObject(type: ObjectType.Point2fVector, points: Point2f[]): PointVector;
3438
createObject(type: ObjectType.PointVectorOfVectors): PointVectorOfVectors;
3539
createObject(
3640
type: ObjectType.Rect,
@@ -105,6 +109,13 @@ export type Objects = {
105109
c?: number;
106110
d?: number;
107111
};
112+
toJSValue(scalar: RotatedRect): {
113+
centerX: number;
114+
centerY: number;
115+
width: number;
116+
height: number;
117+
angle: number;
118+
};
108119

109120
copyObjectFromVector(vector: MatVector, itemIndex: number): Mat;
110121
copyObjectFromVector(vector: PointVector, itemIndex: number): Point;

0 commit comments

Comments
 (0)