Javacpp-presets: Converting BufferedImage to PIX image

Created on 30 Jan 2018  路  6Comments  路  Source: bytedeco/javacpp-presets

I use javac with tesseract in my app, and I want to convert BufferedImage to PIX image to use it like the BasicExample.java,

PIX image = pixRead(args.length > 0 ? args[0] : "/usr/src/tesseract/testing/phototest.tif");
        api.SetImage(image);
        // Get OCR result
        outText = api.GetUTF8Text();

I tried this, but did not work.

Pix image=null; 
        try {
        image = LeptUtils.convertImageToPix(img);
        } catch (IOException ex) {
        Logger.getLogger(OCR.class.getName()).log(Level.SEVERE, null, ex);
        }
api.SetImage(image); 

I searched a lot, but can figure out anything.
Thanks in advance.

duplicate enhancement question

Most helpful comment

I've finally implemented LeptonicaFrameConverter so we can now do something like this to convert efficiently an BufferedImage into a PIX for Tesseract:

    Java2DFrameConverter converter = new Java2DFrameConverter();
    LeptonicaFrameConverter converter2 = new LeptonicaFrameConverter();

    PIX pix = converter2.convert(converter.convert(img));

Please give it a try with the snapshots (http://bytedeco.org/builds/) and let me know if you encounter any issues with it! Thanks

All 6 comments

Duplicate of issue #224.

Until someone (you?) contributes a LeptonicaFrameConverter, you can find code for that here that converts to Mat:
https://github.com/deeplearning4j/DataVec/blob/master/datavec-data/datavec-data-image/src/main/java/org/datavec/image/loader/NativeImageLoader.java#L159
Which we can then convert to BufferedImage with OpenCVFrameConverter and Java2DFrameConverter:
http://bytedeco.org/news/2015/04/04/javacv-frame-converters/
Or less efficiently with Java2DFrameUtils:
http://bytedeco.org/javacv/apidocs/org/bytedeco/javacv/Java2DFrameUtils.html

Ok, Thank you.

BTW, if you end up creating the equivalent of a LeptonicaFrameConverter, please consider making a contribution. If you encounter any issues doing that, let me know and I will help. Thanks!

I've finally implemented LeptonicaFrameConverter so we can now do something like this to convert efficiently an BufferedImage into a PIX for Tesseract:

    Java2DFrameConverter converter = new Java2DFrameConverter();
    LeptonicaFrameConverter converter2 = new LeptonicaFrameConverter();

    PIX pix = converter2.convert(converter.convert(img));

Please give it a try with the snapshots (http://bytedeco.org/builds/) and let me know if you encounter any issues with it! Thanks

Java2DFrameConverter converter = new Java2DFrameConverter(); LeptonicaFrameConverter converter2 = new LeptonicaFrameConverter(); PIX pix = converter2.convert(converter.convert(img));

This doesn't work, have tried with test image. Seems to scramble the image.

Was this page helpful?
0 / 5 - 0 ratings