Leptonica OpenCV Java convert Mat to Pix and vise versa

前端 未结 1 2071
野性不改
野性不改 2021-01-21 15:36

I use the following lept4j and OpenCV Maven dependencies:



    net.sourceforg         


        
相关标签:
1条回答
  • 2021-01-21 15:47

    Try the following:

    public static Pix convertMatToPix(Mat mat) {
        MatOfByte bytes = new MatOfByte();
        Imgcodecs.imencode(".tif", mat, bytes);
        ByteBuffer buff = ByteBuffer.wrap(bytes.toArray());
        return Leptonica1.pixReadMem(buff, new NativeSize(buff.capacity()));
    }
    
    public static Mat convertPixToMat(Pix pix) {
        PointerByReference pdata = new PointerByReference();
        NativeSizeByReference psize = new NativeSizeByReference();
        Leptonica1.pixWriteMem(pdata, psize, pix, ILeptonica.IFF_TIFF);
        byte[] b = pdata.getValue().getByteArray(0, psize.getValue().intValue());
        Leptonica1.lept_free(pdata.getValue());
        return Imgcodecs.imdecode(new MatOfByte(b), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
    }
    
    0 讨论(0)
提交回复
热议问题