Skip to content

'cvtColor' is crashing the app #99

@tamas-relai

Description

@tamas-relai

Hey there!

I'm using your library to scan documents based on the DocumentDetection example, but whenever it gets to the cvtColor part, it's crashing on native level with EXC_BAD_ACCESS (pointing to cv::cvtColor(*src, *dst, code);)

Please let me know if you have any other questions!

This the code I'm using in my app: (removed the boring RN parts)

    useSkiaFrameProcessor(
        frame => {
          'worklet';

          const ratio = 500 / frame.width;
          const height = frame.height * ratio;
          const width = frame.width * ratio;

          const resized = resize(frame, {
            dataType: 'uint8',
            pixelFormat: 'bgr',
            scale: {
              height,
              width,
            },
          });
          const source = OpenCV.bufferToMat('uint8', height, width, 3, resized);
          const kernel = OpenCV.createObject(ObjectType.Size, 4, 4);
          const blurKernel = OpenCV.createObject(ObjectType.Size, 7, 7);
          const structuringElement = OpenCV.invoke('getStructuringElement', MorphShapes.MORPH_ELLIPSE, kernel);
          const contours = OpenCV.createObject(ObjectType.PointVectorOfVectors);

          OpenCV.invoke('cvtColor', source, source, ColorConversionCodes.COLOR_BGR2GRAY);
          OpenCV.invoke('morphologyEx', source, source, MorphTypes.MORPH_OPEN, structuringElement);
          OpenCV.invoke('morphologyEx', source, source, MorphTypes.MORPH_CLOSE, structuringElement);
          OpenCV.invoke('GaussianBlur', source, source, blurKernel, 0);
          OpenCV.invoke('Canny', source, source, 75, 100);
          OpenCV.invoke(
            'findContours',
            source,
            contours,
            RetrievalModes.RETR_LIST,
            ContourApproximationModes.CHAIN_APPROX_SIMPLE,
          );

          const contoursMats = OpenCV.toJSValue(contours);

          let greatestPolygon: PointVector | null = null;
          let greatestArea = 0;

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

            if (area > 2000 && area > greatestArea) {
              const peri = OpenCV.invoke('arcLength', contour, true);
              const approx = OpenCV.createObject(ObjectType.PointVector);

              OpenCV.invoke('approxPolyDP', contour, approx, 0.1 * peri.value, true);

              greatestPolygon = approx;
              greatestArea = area;
            }
          }

          frame.render();

          if (greatestPolygon === null) {
            overdrawExpiration.current = overdrawExpiration.current - 1;

            if (overdrawExpiration.current > 0) {
              frame.drawPath(rectangleRef.current ?? Skia.Path.Make(), paint);
              frame.drawPoints(PointMode.Polygon, pointRef.current, border);
            } else {
              handleDocumentDetectedJS(false);
            }
          } else {
            const points: Point[] = OpenCV.toJSValue(greatestPolygon).array;

            if (points.length === 4) {
              const path = Skia.Path.Make();
              const pointsToShow: SkPoint[] = [];

              const lastPointX = points[3].x / ratio;
              const lastPointY = points[3].y / ratio;

              path.moveTo(lastPointX, lastPointY);
              pointsToShow.push(vec(lastPointX, lastPointY));

              for (let index = 0; index < 4; index++) {
                const pointX = points[index].x / ratio;
                const pointY = points[index].y / ratio;

                path.lineTo(pointX, pointY);
                pointsToShow.push(vec(pointX, pointY));
              }

              path.close();

              overdrawExpiration.current = highlightExpireAfter;
              pointRef.current = [...pointsToShow];
              rectangleRef.current = path;

              frame.drawPath(path, paint);
              frame.drawPoints(PointMode.Polygon, pointsToShow, border);

              handleDocumentDetectedJS(true);
            }
          }

          OpenCV.clearBuffers();
        },
        [handleDocumentDetectedJS],
      )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions