Skip to content

Commit 0e8481f

Browse files
feat: scalar changes
1 parent dfe6b2f commit 0e8481f

File tree

8 files changed

+157
-87
lines changed

8 files changed

+157
-87
lines changed

cpp/FOCV_Function.cpp

+17-18
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,8 @@ jsi::Object FOCV_Function::invoke(jsi::Runtime& runtime, const jsi::Value* argum
429429
} break;
430430
case hashString("inRange", 7): {
431431
auto src = FOCV_Storage::get<cv::Mat>(FOCV_JsiObject::id_from_wrap(runtime, arguments[1]));
432-
auto lowerBound = FOCV_Storage::get<cv::Vec3b>(FOCV_JsiObject::id_from_wrap(runtime, arguments[2]));
433-
auto upperBound = FOCV_Storage::get<cv::Vec3b>(FOCV_JsiObject::id_from_wrap(runtime, arguments[3]));
432+
auto lowerBound = FOCV_Storage::get<cv::Scalar>(FOCV_JsiObject::id_from_wrap(runtime, arguments[2]));
433+
auto upperBound = FOCV_Storage::get<cv::Scalar>(FOCV_JsiObject::id_from_wrap(runtime, arguments[3]));
434434
auto dst = FOCV_Storage::get<cv::Mat>(FOCV_JsiObject::id_from_wrap(runtime, arguments[4]));
435435

436436
cv::inRange(src, lowerBound, upperBound, dst);
@@ -1350,12 +1350,13 @@ jsi::Object FOCV_Function::invoke(jsi::Runtime& runtime, const jsi::Value* argum
13501350
value.setProperty(runtime, "value", jsi::Value(result));
13511351
} break;
13521352
case hashString("boundingRect", 12): {
1353-
// auto src = FOCV_Storage::get<std::vector<cv::Point>>(FOCV_JsiObject::id_from_wrap(runtime, arguments[1]));
1354-
auto array = FOCV_JsiObject::type_from_wrap(runtime, arguments[1]) == "mat" ?
1355-
FOCV_Storage::get<cv::Mat>(FOCV_JsiObject::id_from_wrap(runtime, arguments[1])) :
1356-
FOCV_Storage::get<std::vector<cv::Mat>>(FOCV_JsiObject::id_from_wrap(runtime, arguments[1]));
1353+
cv::Rect rect;
1354+
if(FOCV_JsiObject::type_from_wrap(runtime, arguments[1]) == "mat") {
1355+
rect = cv::boundingRect(FOCV_Storage::get<cv::Mat>(FOCV_JsiObject::id_from_wrap(runtime, arguments[1])));
1356+
} else {
1357+
rect = cv::boundingRect(FOCV_Storage::get<std::vector<cv::Mat>>(FOCV_JsiObject::id_from_wrap(runtime, arguments[1])));
1358+
}
13571359

1358-
cv::Rect rect = cv::boundingRect(array);
13591360
std::string id = FOCV_Storage::save(rect);
13601361

13611362
return FOCV_JsiObject::wrap(runtime, "rect", id);
@@ -1377,9 +1378,9 @@ jsi::Object FOCV_Function::invoke(jsi::Runtime& runtime, const jsi::Value* argum
13771378
value.setProperty(runtime, "value", jsi::Value(result));
13781379
} break;
13791380
case hashString("contourArea", 11): {
1380-
auto src = FOCV_JsiObject::type_from_wrap(runtime, arguments[1]) == "mat" ?
1381-
FOCV_Storage::get<cv::Mat>(FOCV_JsiObject::id_from_wrap(runtime, arguments[1])) :
1382-
FOCV_Storage::get<std::vector<cv::Mat>>(FOCV_JsiObject::id_from_wrap(runtime, arguments[1]));
1381+
auto type = FOCV_JsiObject::type_from_wrap(runtime, arguments[1]);
1382+
auto id = FOCV_JsiObject::id_from_wrap(runtime, arguments[1]);
1383+
auto src = FOCV_Storage::get<cv::Mat>(FOCV_JsiObject::id_from_wrap(runtime, arguments[1]));
13831384
auto oriented = arguments[2].getBool();
13841385

13851386
value.setProperty(runtime, "value", contourArea(src, oriented));
@@ -1404,15 +1405,13 @@ jsi::Object FOCV_Function::invoke(jsi::Runtime& runtime, const jsi::Value* argum
14041405
} break;
14051406
case hashString("findContours", 12): {
14061407
auto src = FOCV_Storage::get<cv::Mat>(FOCV_JsiObject::id_from_wrap(runtime, arguments[1]));
1407-
auto mode = arguments[2].getNumber();
1408-
auto method = arguments[3].getNumber();
1409-
1410-
std::vector<cv::Mat> contours;
1411-
1412-
cv::findContours(src, contours, mode, method);
1408+
auto dst = FOCV_Storage::get<std::vector<cv::Mat>>(FOCV_JsiObject::id_from_wrap(runtime, arguments[2]));
1409+
auto mode = arguments[3].getNumber();
1410+
auto method = arguments[4].getNumber();
1411+
1412+
cv::findContours(src, dst, mode, method);
14131413

1414-
auto id = FOCV_Storage::save(contours);
1415-
return FOCV_JsiObject::wrap(runtime, "mat_vector", id);
1414+
FOCV_Storage::save(FOCV_JsiObject::id_from_wrap(runtime, arguments[2]), dst);
14161415
} break;
14171416
case hashString("fitLine", 7): {
14181417
auto points = FOCV_Storage::get<cv::Mat>(FOCV_JsiObject::id_from_wrap(runtime, arguments[1]));

cpp/FOCV_Object.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ jsi::Object FOCV_Object::create(jsi::Runtime& runtime, const jsi::Value* argumen
8686
object = cv::Scalar(a);
8787
}
8888
} break;
89-
case hashString("rotated_rect", 11): {
89+
case hashString("rotated_rect", 12): {
9090
int x = arguments[1].asNumber();
9191
int y = arguments[2].asNumber();
9292
int width = arguments[3].asNumber();
@@ -115,7 +115,7 @@ jsi::Object FOCV_Object::convertToJSI(jsi::Runtime& runtime, const jsi::Value* a
115115
value.setProperty(runtime, "cols", jsi::Value(mat.cols));
116116
value.setProperty(runtime, "rows", jsi::Value(mat.rows));
117117
} break;
118-
case hashString("mat_vector", 9): {
118+
case hashString("mat_vector", 10): {
119119
std::vector<cv::Mat> mats = FOCV_Storage::get<std::vector<cv::Mat>>(id);
120120

121121
auto array = jsi::Array(runtime, mats.size());
@@ -139,7 +139,7 @@ jsi::Object FOCV_Object::convertToJSI(jsi::Runtime& runtime, const jsi::Value* a
139139
value.setProperty(runtime, "width", jsi::Value(rect.width));
140140
value.setProperty(runtime, "height", jsi::Value(rect.height));
141141
} break;
142-
case hashString("rect_vector", 10): {
142+
case hashString("rect_vector", 11): {
143143
std::vector<cv::Rect> rects = FOCV_Storage::get<std::vector<cv::Rect>>(id);
144144

145145
auto array = jsi::Array(runtime, rects.size());
@@ -162,7 +162,7 @@ jsi::Object FOCV_Object::convertToJSI(jsi::Runtime& runtime, const jsi::Value* a
162162
value.setProperty(runtime, "x", jsi::Value(point.x));
163163
value.setProperty(runtime, "y", jsi::Value(point.y));
164164
} break;
165-
case hashString("point_vector", 11): {
165+
case hashString("point_vector", 12): {
166166
std::vector<cv::Point> points = FOCV_Storage::get<std::vector<cv::Point>>(id);
167167

168168
auto array = jsi::Array(runtime, points.size());
@@ -183,7 +183,7 @@ jsi::Object FOCV_Object::convertToJSI(jsi::Runtime& runtime, const jsi::Value* a
183183
value.setProperty(runtime, "width", jsi::Value(size.width));
184184
value.setProperty(runtime, "height", jsi::Value(size.height));
185185
} break;
186-
case hashString("vec3b", 4): {
186+
case hashString("vec3b", 5): {
187187
cv::Vec3b vec = FOCV_Storage::get<cv::Vec3b>(id);
188188

189189
value.setProperty(runtime, "a", jsi::Value(vec.val[0]));
@@ -198,7 +198,7 @@ jsi::Object FOCV_Object::convertToJSI(jsi::Runtime& runtime, const jsi::Value* a
198198
value.setProperty(runtime, "c", jsi::Value(scalar.val[2]));
199199
value.setProperty(runtime, "d", jsi::Value(scalar.val[3]));
200200
} break;
201-
case hashString("rotated_rect", 11): {
201+
case hashString("rotated_rect", 12): {
202202
cv::RotatedRect rect = FOCV_Storage::get<cv::RotatedRect>(id);
203203

204204
value.setProperty(runtime, "centerX", jsi::Value(rect.center.x));

example/src/ImageExample.tsx

+51-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import { useState } from 'react';
22
import { Button, Image, SafeAreaView, Text } from 'react-native';
33
import {
44
ColorConversionCodes,
5+
ContourApproximationModes,
56
DataTypes,
67
ObjectType,
78
OpenCV,
9+
RetrievalModes,
10+
type Rect,
811
} from 'react-native-fast-opencv';
912
import { launchImageLibrary, type Asset } from 'react-native-image-picker';
1013

@@ -25,15 +28,58 @@ export function ImageExample() {
2528
const src = OpenCV.base64ToMat(photo.base64);
2629
const dst = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8U);
2730

28-
OpenCV.invoke('cvtColor', src, dst, ColorConversionCodes.COLOR_BGR2RGB);
29-
OpenCV.invoke('cvtColor', dst, dst, ColorConversionCodes.COLOR_BGR2HSV);
31+
OpenCV.invoke('cvtColor', src, dst, ColorConversionCodes.COLOR_BGR2HSV);
3032

31-
const lowerBound = OpenCV.createObject(ObjectType.Vec3b, 0, 120, 120);
32-
const upperBound = OpenCV.createObject(ObjectType.Vec3b, 255, 255, 255);
33+
const lowerBound = OpenCV.createObject(ObjectType.Scalar, 30, 60, 60);
34+
const upperBound = OpenCV.createObject(ObjectType.Scalar, 50, 255, 255);
35+
36+
OpenCV.invoke('inRange', dst, lowerBound, upperBound, dst);
37+
38+
const channels = OpenCV.createObject(ObjectType.MatVector);
39+
OpenCV.invoke('split', dst, channels);
40+
const grayChannel = OpenCV.copyObjectFromVector(channels, 0);
41+
42+
const contours = OpenCV.createObject(ObjectType.MatVector);
43+
44+
OpenCV.invoke(
45+
'findContours',
46+
grayChannel,
47+
contours,
48+
RetrievalModes.RETR_TREE,
49+
ContourApproximationModes.CHAIN_APPROX_SIMPLE
50+
);
51+
52+
const contoursMats = OpenCV.toJSValue(contours);
53+
54+
const rectangles: Rect[] = [];
55+
56+
for (let i = 0; i < contoursMats.array.length; i++) {
57+
const contour = OpenCV.copyObjectFromVector(contours, i);
58+
const { value: area } = OpenCV.invoke('contourArea', contour, false);
59+
60+
if (area > 3000) {
61+
const rect = OpenCV.invoke('boundingRect', contour);
62+
rectangles.push(rect);
63+
}
64+
}
65+
66+
console.log(rectangles);
67+
68+
// OpenCV.invoke('cvtColor', dst, dst, ColorConversionCodes.COLOR_HSV2BGR);
69+
// OpenCV.invoke('cvtColor', dst, dst, ColorConversionCodes.COLOR_BGR2GRAY);
70+
71+
// const lowerBound = OpenCV.frameBufferToMat(
72+
// 1,
73+
// 3,
74+
// new Uint8Array([0, 120, 120])
75+
// );
76+
77+
// const lowerBound = OpenCV.createObject(ObjectType.Vec3b, 0, 120, 120);
78+
// const upperBound = OpenCV.createObject(ObjectType.Vec3b, 255, 255, 255);
3379

3480
// OpenCV.invoke('inRange', dst, lowerBound, upperBound, dst);
3581

36-
const result = OpenCV.toJSValue(dst);
82+
const result = OpenCV.toJSValue(grayChannel);
3783
setB64(result.base64);
3884
// console.log(array.length);
3985
}

example/src/VisionCameraExample.tsx

+43-32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PaintStyle, Skia } from '@shopify/react-native-skia';
2-
import { useEffect } from 'react';
3-
import { StyleSheet, Text } from 'react-native';
2+
import { useEffect, useState } from 'react';
3+
import { Button, Image, StyleSheet, Text, View } from 'react-native';
44
import {
55
OpenCV,
66
ObjectType,
@@ -16,12 +16,16 @@ import {
1616
useCameraPermission,
1717
useSkiaFrameProcessor,
1818
} from 'react-native-vision-camera';
19+
import { useSharedValue } from 'react-native-worklets-core';
1920
import { useResizePlugin } from 'vision-camera-resize-plugin';
2021

2122
export function VisionCameraExample() {
2223
const device = useCameraDevice('back');
2324
const { resize } = useResizePlugin();
2425

26+
const [b64, setB64] = useState<string>('');
27+
const r = useSharedValue<string>('');
28+
2529
const { hasPermission, requestPermission } = useCameraPermission();
2630

2731
useEffect(() => {
@@ -43,35 +47,26 @@ export function VisionCameraExample() {
4347
width: width,
4448
height: height,
4549
},
46-
pixelFormat: 'rgb',
50+
pixelFormat: 'bgr',
4751
dataType: 'uint8',
4852
});
53+
const rectangles: Rect[] = [];
4954

5055
const src = OpenCV.frameBufferToMat(height, width, resized);
5156
const dst = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8U);
5257

53-
OpenCV.invoke('cvtColor', src, dst, ColorConversionCodes.COLOR_BGR2RGB);
54-
OpenCV.invoke('cvtColor', dst, dst, ColorConversionCodes.COLOR_BGR2HSV);
55-
56-
const lowerBound = OpenCV.frameBufferToMat(
57-
1,
58-
3,
59-
new Uint8Array([37, 120, 120])
60-
);
61-
const upperBound = OpenCV.frameBufferToMat(
62-
1,
63-
3,
64-
new Uint8Array([60, 255, 255])
65-
);
58+
OpenCV.invoke('cvtColor', src, dst, ColorConversionCodes.COLOR_BGR2HSV);
6659

60+
const lowerBound = OpenCV.createObject(ObjectType.Scalar, 30, 60, 60);
61+
const upperBound = OpenCV.createObject(ObjectType.Scalar, 50, 255, 255);
6762
OpenCV.invoke('inRange', dst, lowerBound, upperBound, dst);
6863

64+
r.value = OpenCV.toJSValue(dst).base64;
65+
6966
const channels = OpenCV.createObject(ObjectType.MatVector);
7067
OpenCV.invoke('split', dst, channels);
7168

7269
const grayChannel = OpenCV.copyObjectFromVector(channels, 0);
73-
const rectangles: Rect[] = [];
74-
7570
const contours = OpenCV.createObject(ObjectType.MatVector);
7671

7772
OpenCV.invoke(
@@ -82,15 +77,17 @@ export function VisionCameraExample() {
8277
ContourApproximationModes.CHAIN_APPROX_SIMPLE
8378
);
8479

85-
// TO CHECK!
86-
// for (const contour of contours) {
87-
// const { value: area } = OpenCV.invoke('contourArea', contour, false);
80+
const contoursMats = OpenCV.toJSValue(contours);
8881

89-
// if (area > 3000) {
90-
// const rect = OpenCV.invoke('boundingRect', contour);
91-
// rectangles.push(rect);
92-
// }
93-
// }
82+
for (let i = 0; i < contoursMats.array.length; i++) {
83+
const contour = OpenCV.copyObjectFromVector(contours, i);
84+
const { value: area } = OpenCV.invoke('contourArea', contour, false);
85+
86+
if (area > 3000) {
87+
const rect = OpenCV.invoke('boundingRect', contour);
88+
rectangles.push(rect);
89+
}
90+
}
9491

9592
frame.render();
9693

@@ -118,11 +115,25 @@ export function VisionCameraExample() {
118115
return <Text>No device</Text>;
119116
}
120117
return (
121-
<Camera
122-
style={StyleSheet.absoluteFill}
123-
device={device}
124-
isActive={true}
125-
frameProcessor={frameProcessor}
126-
/>
118+
<>
119+
<Camera
120+
style={StyleSheet.absoluteFill}
121+
device={device}
122+
isActive={true}
123+
frameProcessor={frameProcessor}
124+
/>
125+
<View
126+
style={{ height: 200, width: 200, top: 100, backgroundColor: 'white' }}
127+
>
128+
<Button title="test" onPress={() => setB64(r.value)} />
129+
{b64 && (
130+
<Image
131+
source={{ uri: 'data:image/jpg;base64,' + b64 }}
132+
height={100}
133+
width={100}
134+
/>
135+
)}
136+
</View>
137+
</>
127138
);
128139
}

src/functions/Core.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -457,15 +457,15 @@ export type Core = {
457457
* Checks if array elements lie between the elements of two other arrays.
458458
* @param name Function name.
459459
* @param src first input array
460-
* @param lowerb inclusive lower boundary array or a scalar
461-
* @param upperb inclusive upper boundary array or a scalar
460+
* @param lowerb inclusive lower boundary scalar
461+
* @param upperb inclusive upper boundary scalar
462462
* @param dst output array of the same size as src and CV_8U type
463463
*/
464464
invoke(
465465
name: 'inRange',
466466
src: Mat,
467-
lowerb: MatVector | Mat,
468-
upperb: MatVector | Mat,
467+
lowerb: Scalar,
468+
upperb: Scalar,
469469
dst: Mat
470470
): void;
471471

src/functions/ImageProcessing/Shape.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export type Shape = {
7979
name: 'contourArea',
8080
contour: Mat | MatVector,
8181
oriented?: boolean
82-
): number;
82+
): { value: number };
8383

8484
/**
8585
* Finds the convex hull of a point set.

0 commit comments

Comments
 (0)