Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting BufferedImage to PIX image #517

Closed
tahaemara opened this issue Jan 30, 2018 · 7 comments
Closed

Converting BufferedImage to PIX image #517

tahaemara opened this issue Jan 30, 2018 · 7 comments

Comments

@tahaemara
Copy link

tahaemara commented Jan 30, 2018

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.

@saudet
Copy link
Member

saudet commented Jan 31, 2018

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

@saudet saudet closed this as completed Jan 31, 2018
@tahaemara
Copy link
Author

Ok, Thank you.

@saudet
Copy link
Member

saudet commented Feb 7, 2018

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!

saudet added a commit that referenced this issue May 20, 2018
…g access to image data of Tesseract (issue #517)
@saudet
Copy link
Member

saudet commented May 21, 2018

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

@thekevshow
Copy link

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.

@saudet
Copy link
Member

saudet commented Jan 31, 2019

@kev2316 See issue bytedeco/javacv#1115.

@adam-osusky
Copy link

adam-osusky commented Feb 29, 2024

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

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

This works. Thanks @saudet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants