|
1 | 1 | # Usage
|
2 | 2 |
|
3 |
| -cleaning of buffers |
| 3 | +> The library is in the early stages of development and not all functions or objects available in OpenCV are supported. Add an issue if you have any problems or questions. |
4 | 4 |
|
5 |
| -Objects |
| 5 | +## **Important**: Cleaning of buffers |
6 | 6 |
|
7 |
| -create object |
| 7 | +OpenCV objects during execution are held in internal memory and managed on the C++ side. Hence, after a calculation has been performed, it is necessary to release unused resources using a function: |
8 | 8 |
|
| 9 | +```js |
| 10 | +OpenCV.clearBuffers(); |
| 11 | +``` |
9 | 12 |
|
10 |
| -toJSValue |
| 13 | +You can do this e.g. after a calculation, or after a specific step. **Remember that not executing this function will result in data being held in memory continuously.** |
11 | 14 |
|
12 |
| -copy object from vector |
13 | 15 |
|
| 16 | +## Objects |
14 | 17 |
|
15 |
| -Functions |
| 18 | +The library currently supports the following OpenCV objects: **Mat, MatVector, Point, Point Vector, Rect, RectVector, Size, Scalar and RotatedRect.** |
16 | 19 |
|
17 |
| -invocation |
| 20 | +> A vector is simply an array in which individual objects are held. |
18 | 21 |
|
| 22 | +### Object creation |
| 23 | + |
| 24 | +To create an object, execute the createObject function, where the first argument will be the type of object (enum ObjectType) and the following parameters specific to a particular type of object. |
| 25 | + |
| 26 | +```js |
| 27 | +const point = OpenCV.createObject(ObjectType.Point, 1, 2); // x, y |
| 28 | +``` |
| 29 | + |
| 30 | +A detailed list of parameters is available [here](). |
| 31 | + |
| 32 | +#### Mat creation from Base64 image |
| 33 | + |
| 34 | +To create a Mat object with image data, you can use a function that creates it from a Base64 image. |
| 35 | + |
| 36 | +```js |
| 37 | +const mat = OpenCV.base64ToMat(base64string); |
| 38 | +``` |
| 39 | + |
| 40 | +To convert an image, such as from a gallery, you can use the [react-native-image-picker](https://github.com/react-native-image-picker/react-native-image-picker) library with selected `includeBase64` option. |
| 41 | + |
| 42 | +#### Mat creation from Vision Camera Frame |
| 43 | + |
| 44 | +To process real-time frames from the vision camera library frame processor, you can use the following method: |
| 45 | + |
| 46 | +```js |
| 47 | +const { resize } = useResizePlugin(); |
| 48 | + |
| 49 | +const frameProcessor = useFrameProcessor((frame) => { |
| 50 | + 'worklet'; |
| 51 | + |
| 52 | + const height = frame.height / 4; |
| 53 | + const width = frame.width / 4; |
| 54 | + |
| 55 | + const resized = resize(frame, { |
| 56 | + scale: { |
| 57 | + width: width, |
| 58 | + height: height, |
| 59 | + }, |
| 60 | + pixelFormat: 'bgr', |
| 61 | + dataType: 'uint8', |
| 62 | + }); |
| 63 | + |
| 64 | + const mat = OpenCV.frameBufferToMat(height, width, resized); |
| 65 | + |
| 66 | + // calculations ... |
| 67 | + |
| 68 | + OpenCV.clearBuffers(); // IMPORTANT! At the end. |
| 69 | +}); |
| 70 | +``` |
| 71 | + |
| 72 | +It is good practice to scale the photo, such as using a frame processor from the [vision-camera-resize-plugin](https://github.com/mrousavy/vision-camera-resize-plugin) library. |
| 73 | + |
| 74 | + |
| 75 | +### Expose and copy object from vector |
| 76 | + |
| 77 | +In some cases, you will need to pull a specific object from a vector, for example, a Mat object from MatVector. To get to it, use the function: |
| 78 | + |
| 79 | +```js |
| 80 | +const object = OpenCV.copyObjectFromVector(vector, 3); // vector, item index |
| 81 | +``` |
| 82 | + |
| 83 | +### Object conversion to JavaScript value |
| 84 | + |
| 85 | +Since the data is stored in memory and managed by C++, to get to know the OpenCV object data in the form of a JS object, use the function: |
| 86 | + |
| 87 | +```js |
| 88 | +const data = OpenCV.toJSValue(object); |
| 89 | +``` |
| 90 | + |
| 91 | + |
| 92 | +## Functions |
| 93 | + |
| 94 | +Not all OpenCV features are currently available and ready for use. You can find how the function works on the official [OpenCV](https://opencv.org) website. The naming is consistent - all functions work in the same way as in the C++ version. |
| 95 | + |
| 96 | +### Invocation |
| 97 | +To execute a function from OpenCV call the invoke method, where the first parameter is the name of the function, and the rest are the necessary parameters. Example: |
| 98 | + |
| 99 | +```js |
| 100 | +OpenCV.invoke('cvtColor', srcMat, dstMat, ColorConversionCodes.COLOR_BGR2HSV); |
| 101 | +``` |
| 102 | + |
| 103 | +To check the list of available features, take a look [here](./availablefunctions.md). |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +## Calculations from separated thread |
| 108 | + |
| 109 | +Operations on images can be time-consuming, and the library works with synchronous functions, hence it is a good idea to move the execution of the operation to another thread, so as not to block the JavaScript thread. Integration with this library allows tasks to be delegated to another thread. |
| 110 | + |
| 111 | + |
| 112 | +#### Example |
| 113 | + |
| 114 | +```js |
| 115 | +import { useRunOnJS, useWorklet } from 'react-native-worklets-core'; |
| 116 | + |
| 117 | +// ... |
| 118 | + |
| 119 | +const [base64, setBase64] = useState<string>(''); |
| 120 | + |
| 121 | +const setImage = useRunOnJS((data: string) => { |
| 122 | + setBase64(data); |
| 123 | +}, []); |
| 124 | + |
| 125 | +const worklet = useWorklet('default', () => { |
| 126 | + 'worklet'; |
| 127 | + if (photo) { |
| 128 | + const src = OpenCV.base64ToMat(photo.base64); |
| 129 | + |
| 130 | + const result = OpenCV.toJSValue(dst); |
| 131 | + setImage(result.base64); |
| 132 | + |
| 133 | + OpenCV.clearBuffers(); // IMPORTANT |
| 134 | + } |
| 135 | +}); |
| 136 | + |
| 137 | +``` |
0 commit comments