lukaszkurantdev / react-native-fast-opencv

A powerful port of OpenCV for React Native.
https://lukaszkurantdev.github.io/react-native-fast-opencv/
MIT License
40 stars 5 forks source link

Question: Reading the points returned by findContours #6

Closed martinbooth closed 3 weeks ago

martinbooth commented 3 weeks ago

In your example, you are using findBoundingRect to get a bounding rectangle of points... is there anyway to get the actual points in the contour? it seems contour is typed as Mat, and the only thing possible to do with that is convert it to a buffer

lukaszkurantdev commented 3 weeks ago

Hi @martinbooth , you asked for a very good case. I have therefore added a new structure in the new version (0.2.4): PointVectorOfVectors i.e. now we can create a vector of point vectors and put it as a parameter of the findContours function.

const contours = OpenCV.createObject(ObjectType.PointVectorOfVectors);
OpenCV.invoke(
  'findContours',
  grayChannel,
  contours,
  RetrievalModes.RETR_TREE,
  ContourApproximationModes.CHAIN_APPROX_SIMPLE
);

Of course we can also convert to an array of arrays of objects:

const contoursVector = OpenCV.toJSValue(contours);

Or simply copy a specific element into a PointVector and use it in functions such as contourArea or boundingRect.

for (let i = 0; i < contoursVector.array.length; i++) {
  const contour = OpenCV.copyObjectFromVector(contours, i);
  const { value: area } = OpenCV.invoke('contourArea', contour, false);

  if (area > 3000) {
    // console.log(OpenCV.toJSValue(contour))
    const rect = OpenCV.invoke('boundingRect', contour);
  }
}

If you have other problems, feel free to reopen this thread or add new issues. Thanks!