bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.49k stars 1.57k forks source link

Image colors getting inverted by leptonica converter #1136

Open nehabagalkot opened 5 years ago

nehabagalkot commented 5 years ago

I am using LeptonicaFrameConverter c = new LeptonicaFrameConverter(); Frame f = c.convert(src); BufferedImage img = Java2DFrameUtils.toBufferedImage(f); But the output image has inverted colors, ie 1->0 and vice versa. using it for .tiff format. How can I resole this? TIA.

saudet commented 5 years ago

We can easily fix that with some simple post-processing, but could you please attach a sample TIFF file that does that here so I can have a look? Thanks!

nehabagalkot commented 5 years ago

New folder.zip Attached zip with input and output image.

saudet commented 5 years ago

Thanks! This is a 1-bit image and we're using pixConvert1To8() for that in LeptonicaFrameConverter: https://github.com/bytedeco/javacv/blob/master/src/main/java/org/bytedeco/javacv/LeptonicaFrameConverter.java#L91 If that doesn't work properly, I'm afraid this will need to be fixed in Leptonica. Could you please report this issue upstream?

nehabagalkot commented 5 years ago

Its not only for 1-bit image, I tried it for color image, it inverts colors for it as well. color_input ![Uploading color_output.jpg…]()

saudet commented 5 years ago

Could you also provide a small but complete code snippet to reproduce this issue?

nehabagalkot commented 5 years ago
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

import org.bytedeco.javacpp.lept;
import org.bytedeco.javacpp.lept.PIX;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameUtils;
import org.bytedeco.javacv.LeptonicaFrameConverter;

public class POCByteArray {

    public static void main(String[] args) throws Exception {
        PIX src = lept.pixRead("RotationTest.tiff");

        LeptonicaFrameConverter c = new LeptonicaFrameConverter();
        Frame f = c.convert(src);
        BufferedImage img = Java2DFrameUtils.toBufferedImage(f);
        ImageIO.write(img, "jpg", new File("RotationTestPIXbyte.jpg"));

    }

}
saudet commented 5 years ago

Right, Leptonica loads all color images with 4 channels, but the order of the color channels doesn't necessarily map well with OpenCV or Java2D, see issue #1075.