使用JavaScript重写java OpenCV透视校正方法

问题描述

我在 Java 中发现了 this function,它在给定 4 分的情况下进行透视校正(裁剪)。我正在尝试使用 react-native-opencv3 用 Ja​​vaScript(在 React Native 中)重写它。我不确定我做错了什么,因为结果图像与原始图像相同:

async crop() {
    const coordinates = this.customCrop.crop();
    const tl = new CvPoint(coordinates.topLeft.x,coordinates.topLeft.y);
    const tr = new CvPoint(coordinates.topRight.x,coordinates.topRight.y);
    const bl = new CvPoint(coordinates.bottomLeft.x,coordinates.bottomLeft.y);
    const br = new CvPoint(coordinates.bottomright.x,coordinates.bottomright.y);

    let srcMat = await RNCv.imagetoMat(this.state.initialImage.replace("file://",""));
    srcMat = await RNCv.invokeMethod("cvtColor",{p1: srcMat,p2: srcMat,p3: ColorConv.COLOR_BGR2RGB});

    const ratioAlreadyApplied = tr.x * (this.state.width / 500) < this.state.width;
    const ratio = ratioAlreadyApplied ? this.state.width / 500 : 1;

    const widthA = Math.sqrt(Math.pow(br.x - bl.x,2) + Math.pow(br.y - bl.y,2));
    const widthB = Math.sqrt(Math.pow(tr.x - tl.x,2) + Math.pow(tr.y - tl.y,2));
    const dw = Math.max(widthA,widthB) * ratio;
    const maxWidth = Math.round(dw);

    const heightA = Math.sqrt(Math.pow(tr.x - br.x,2) + Math.pow(tr.y - br.y,2));
    const heightB = Math.sqrt(Math.pow(tl.x - bl.x,2) + Math.pow(tl.y - bl.y,2));

    const dh = Math.max(heightA,heightB) * ratio;
    const maxHeight = Math.round(dh);

    let doc = await new Mat(maxHeight,maxWidth,CvType.CV_8UC4).init();

    const src_mat = await new Mat(4,1,CvType.CV_32FC2).init();
    const dst_mat = await new Mat(4,CvType.CV_32FC2).init();

    await src_mat.put(0,tl.x * ratio,tl.y * ratio,tr.x * ratio,tr.y * ratio,br.x * ratio,br.y * ratio,bl.x * ratio,bl.y * ratio);
    await dst_mat.put(0,0.0,dw,dh,dh);

    const m = await RNCv.invokeMethod("getPerspectiveTransform",{p1: src_mat,p2: dst_mat});

    let res = await RNCv.invokeMethod("warpPerspective",p2: doc,p3: m,p4: doc});

    const newImagePath = this.RNFS.DocumentDirectoryPath + '/test.png';
    const { uri } = await RNCv.matToImage(res,newImagePath);

    this.setState({image: uri});  
}

我使用的是 OpenCV 3.4.4。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)