Example usage for org.opencv.core CvType channels

List of usage examples for org.opencv.core CvType channels

Introduction

In this page you can find the example usage for org.opencv.core CvType channels.

Prototype

public static final int channels(int type) 

Source Link

Usage

From source file:com.jiminger.image.ImageFile.java

License:Open Source License

public static BufferedImage readBufferedImageFromFile(final String filename) throws IOException {
    LOGGER.trace("Reading image from {}", filename);
    final File f = new File(filename);
    if (!f.exists())
        throw new FileNotFoundException(filename);
    BufferedImage ret = ImageIO.read(f);
    if (ret == null) {
        LOGGER.info("Failed to read '{}' using ImageIO", filename);
        try (Closer closer = new Closer()) {
            final Mat mat = Imgcodecs.imread(filename, IMREAD_UNCHANGED);
            if (mat == null)
                throw new IllegalArgumentException("Can't read '" + filename
                        + "' as an image. No codec available in either ImageIO or OpenCv");
            if (filename.endsWith(".jp2") && CvType.channels(mat.channels()) > 1)
                Imgproc.cvtColor(mat, mat, Imgproc.COLOR_RGB2BGR);
            ret = Utils.mat2Img(mat);/*from  w  w  w.  j a va  2s .  c o  m*/
        }
    }
    LOGGER.trace("Read {} from {}", ret, filename);
    return ret;
}

From source file:com.jiminger.image.ImageFile.java

License:Open Source License

public static CvRaster readMatFromFile(final String filename, final Closer closer) throws IOException {
    LOGGER.trace("Reading image from {}", filename);
    final File f = new File(filename);
    if (!f.exists())
        throw new FileNotFoundException(filename);

    final CvRaster ret;

    try (Closer cx = new Closer()) {
        final Mat mat = Imgcodecs.imread(filename, IMREAD_UNCHANGED);
        if (mat == null) {
            LOGGER.debug("Failed to read '" + filename + "' using OpenCV");
            ret = Utils.img2CvRaster(ImageIO.read(f));
        } else {/*from   www .j  a v a  2s  . c o  m*/
            if (filename.endsWith(".jp2") && CvType.channels(mat.channels()) > 1)
                Imgproc.cvtColor(mat, mat, Imgproc.COLOR_RGB2BGR);
            ret = CvRaster.move(mat, closer);
        }
    }
    LOGGER.trace("Read {} from {}", ret, filename);
    return ret;
}