Java Code Examples for org.bytedeco.javacv.OpenCVFrameConverter#ToMat

The following examples show how to use org.bytedeco.javacv.OpenCVFrameConverter#ToMat . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HaarFaceDetector.java    From javacv-cnn-example with MIT License 6 votes vote down vote up
public HaarFaceDetector() {
    iplImageConverter = new OpenCVFrameConverter.ToIplImage();
    toMatConverter = new OpenCVFrameConverter.ToMat();

    try {
        File haarCascade = new File(this.getClass().getResource("/detection/haarcascade_frontalface_alt.xml").toURI());
        logger.debug("Using Haar Cascade file located at : {}", haarCascade.getAbsolutePath());
        //haarClassifierCascade = new CvHaarClassifierCascade(cvload(haarCascade.getAbsolutePath()));
        faceCascade = new CascadeClassifier(haarCascade.getCanonicalPath());

    } catch (Exception e) {
        logger.error("Error when trying to get the haar cascade", e);
        throw new IllegalStateException("Error when trying to get the haar cascade", e);
    }
    storage = CvMemStorage.create();
}
 
Example 2
Source File: TestNativeImageLoader.java    From DataVec with Apache License 2.0 5 votes vote down vote up
BufferedImage makeRandomBufferedImage(int height, int width, int channels) {
    Mat img = makeRandomImage(height, width, channels);

    OpenCVFrameConverter.ToMat c = new OpenCVFrameConverter.ToMat();
    Java2DFrameConverter c2 = new Java2DFrameConverter();

    return c2.convert(c.convert(img));
}
 
Example 3
Source File: ResizeImageTransform.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Returns new ResizeImageTransform object
 *
 * @param random Random
 * @param newWidth new Width for the outcome images
 * @param newHeight new Height for outcome images
 */
public ResizeImageTransform(Random random, int newWidth, int newHeight) {
    super(random);

    this.newWidth = newWidth;
    this.newHeight = newHeight;
    this.converter = new OpenCVFrameConverter.ToMat();
}
 
Example 4
Source File: TestNativeImageLoader.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
BufferedImage makeRandomBufferedImage(int height, int width, int channels) {
    Mat img = makeRandomImage(height, width, channels);

    OpenCVFrameConverter.ToMat c = new OpenCVFrameConverter.ToMat();
    Java2DFrameConverter c2 = new Java2DFrameConverter();

    return c2.convert(c.convert(img));
}
 
Example 5
Source File: WarpImageTransform.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance of the ImageTransform.
 *
 * @param random object to use (or null for deterministic)
 * @param dx1    maximum warping in x for the top-left corner (pixels)
 * @param dy1    maximum warping in y for the top-left corner (pixels)
 * @param dx2    maximum warping in x for the top-right corner (pixels)
 * @param dy2    maximum warping in y for the top-right corner (pixels)
 * @param dx3    maximum warping in x for the bottom-right corner (pixels)
 * @param dy3    maximum warping in y for the bottom-right corner (pixels)
 * @param dx4    maximum warping in x for the bottom-left corner (pixels)
 * @param dy4    maximum warping in y for the bottom-left corner (pixels)
 */
public WarpImageTransform(Random random, float dx1, float dy1, float dx2, float dy2, float dx3, float dy3,
                float dx4, float dy4) {
    super(random);
    deltas = new float[8];
    deltas[0] = dx1;
    deltas[1] = dy1;
    deltas[2] = dx2;
    deltas[3] = dy2;
    deltas[4] = dx3;
    deltas[5] = dy3;
    deltas[6] = dx4;
    deltas[7] = dy4;
    this.converter = new OpenCVFrameConverter.ToMat();
}
 
Example 6
Source File: RandomCropTransform.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public RandomCropTransform(Random random, long seed, int height, int width) {
    super(random);
    this.outputHeight = height;
    this.outputWidth = width;
    this.rng = Nd4j.getRandom();
    this.rng.setSeed(seed);
    this.converter = new OpenCVFrameConverter.ToMat();
}
 
Example 7
Source File: VideoPlayer.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 5 votes vote down vote up
private void runVideoMainThread(Yolo yolo, String windowName,
                                String videoFileName,
                                OpenCVFrameConverter.ToMat toMat) throws Exception {
    FFmpegFrameGrabber grabber = initFrameGrabber(videoFileName);
    while (!stop) {
        Frame frame = grabber.grab();

        if (frame == null) {
            log.info("Stopping");
            stop();
            break;
        }
        if (frame.image == null) {
            continue;
        }

        Thread.sleep(60);
        opencv_core.Mat mat = toMat.convert(frame);
        opencv_core.Mat resizeMat = new opencv_core.Mat(yolo.getSelectedSpeed().height,
                yolo.getSelectedSpeed().width, mat.type());
        yolo.push(resizeMat, windowName);
        org.bytedeco.javacpp.opencv_imgproc.resize(mat, resizeMat, resizeMat.size());
        yolo.drawBoundingBoxesRectangles(frame, resizeMat, windowName);
        char key = (char) waitKey(20);
        // Exit this loop on escape:
        if (key == 27) {
            stop();
            break;
        }
    }
}
 
Example 8
Source File: RobotUtils.java    From karate with MIT License 4 votes vote down vote up
public static void show(Mat mat, String title) {
    OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat();
    CanvasFrame canvas = new CanvasFrame(title, 1);
    canvas.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    canvas.showImage(converter.convert(mat));
}
 
Example 9
Source File: TestConversion.java    From konduit-serving with Apache License 2.0 4 votes vote down vote up
@Test
public void testConversion(){
    /*
    ai.konduit.serving.data.javacv.format.JavaCVImageConverters$FrameToMatConverter
    ai.konduit.serving.data.javacv.format.JavaCVImageConverters$MatToFrameConverter
    ai.konduit.serving.data.javacv.format.JavaCVImageConverters$FrameToOpenCVMatConverter
    ai.konduit.serving.data.javacv.format.JavaCVImageConverters$OpenCVMatToFrameConverter
    ai.konduit.serving.data.javacv.format.JavaCVImageConverters$MatToOpenCVMatConverter
    ai.konduit.serving.data.javacv.format.JavaCVImageConverters$OpenCVMatToMatConverter
    ai.konduit.serving.data.javacv.format.JavaCVImageConverters$MatToPng
    ai.konduit.serving.data.javacv.format.JavaCVImageConverters$PngToMat
    ai.konduit.serving.data.javacv.format.JavaCVImageConverters$FrameToPng
    ai.konduit.serving.data.javacv.format.JavaCVImageConverters$PngToFrame
    ai.konduit.serving.data.javacv.format.JavaCVImageConverters$OpenCVMatToPng
    ai.konduit.serving.data.javacv.format.JavaCVImageConverters$PngToOpenCVMat
     */

    File f = Resources.asFile("data/5_32x32.png");

    OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat();
    Mat mat = org.bytedeco.opencv.global.opencv_imgcodecs.imread(f.getAbsolutePath());
    Frame frame = converter.convert(mat);

    Image iMat = checkSize(Image.create(mat));
    Image iFrame = checkSize(Image.create(frame));

    //To Mat:
    Mat m1 = iMat.getAs(Mat.class);
    Mat m2 = iFrame.getAs(Mat.class);
    assertTrue(equalMats(m1, m2));

    //To Frame:
    Frame f1 = iMat.getAs(Frame.class);
    Frame f2 = iFrame.getAs(Frame.class);
    assertTrue(equalFrames(f1, f2));

    checkEncodingConversion(mat, frame, iMat, iFrame, Png.class);
    checkEncodingConversion(mat, frame, iMat, iFrame, Bmp.class);


    //Test Data
    for(Image i : new Image[]{iMat, iFrame}){
        Data d = Data.singleton("myImage", i);
        String json = d.toJson();
        Data dJson = Data.fromJson(json);

        assertEquals(d, dJson);
    }
}
 
Example 10
Source File: NativeCodecRecordReader.java    From DataVec with Apache License 2.0 4 votes vote down vote up
@Override
public void setConf(Configuration conf) {
    super.setConf(conf);
    converter = new OpenCVFrameConverter.ToMat();
    imageLoader = new NativeImageLoader(rows, cols);
}
 
Example 11
Source File: AndroidNativeImageLoader.java    From DataVec with Apache License 2.0 4 votes vote down vote up
public INDArray asMatrix(Bitmap image) throws IOException {
    if (converter == null) {
        converter = new OpenCVFrameConverter.ToMat();
    }
    return asMatrix(converter.convert(converter2.convert(image)));
}
 
Example 12
Source File: Java2DNativeImageLoader.java    From deeplearning4j with Apache License 2.0 3 votes vote down vote up
/**
 * Loads a {@link INDArray} from a {@link BufferedImage}.
 *
 * @param image as a BufferedImage
 * @param flipChannels to have a format like TYPE_INT_RGB (ARGB) output as BGRA, etc
 * @return the loaded matrix
 * @throws IOException
 */
public INDArray asMatrix(BufferedImage image, boolean flipChannels) throws IOException {
    if (converter == null) {
        converter = new OpenCVFrameConverter.ToMat();
    }
    return asMatrix(converter.convert(converter2.getFrame(image, 1.0, flipChannels)));
}
 
Example 13
Source File: BoxImageTransform.java    From DataVec with Apache License 2.0 3 votes vote down vote up
/**
 * Constructs an instance of the ImageTransform.
 *
 * @param random object to use (or null for deterministic)
 * @param width  of the boxed image (pixels)
 * @param height of the boxed image (pixels)
 */
public BoxImageTransform(Random random, int width, int height) {
    super(random);
    this.width = width;
    this.height = height;
    this.converter = new OpenCVFrameConverter.ToMat();
}
 
Example 14
Source File: ScaleImageTransform.java    From deeplearning4j with Apache License 2.0 3 votes vote down vote up
/**
 * Constructs an instance of the ImageTransform.
 *
 * @param random object to use (or null for deterministic)
 * @param dx     maximum scaling in width of the image (pixels)
 * @param dy     maximum scaling in height of the image (pixels)
 */
public ScaleImageTransform(Random random, float dx, float dy) {
    super(random);
    this.dx = dx;
    this.dy = dy;
    this.converter = new OpenCVFrameConverter.ToMat();
}
 
Example 15
Source File: RotateImageTransform.java    From deeplearning4j with Apache License 2.0 3 votes vote down vote up
/**
 * Constructs an instance of the ImageTransform.
 *
 * @param random  object to use (or null for deterministic)
 * @param centerx maximum deviation in x of center of rotation (relative to image center)
 * @param centery maximum deviation in y of center of rotation (relative to image center)
 * @param angle   maximum rotation (degrees)
 * @param scale   maximum scaling (relative to 1)
 */
public RotateImageTransform(Random random, float centerx, float centery, float angle, float scale) {
    super(random);
    this.centerx = centerx;
    this.centery = centery;
    this.angle = angle;
    this.scale = scale;
    this.converter = new OpenCVFrameConverter.ToMat();
}
 
Example 16
Source File: ScaleImageTransform.java    From DataVec with Apache License 2.0 3 votes vote down vote up
/**
 * Constructs an instance of the ImageTransform.
 *
 * @param random object to use (or null for deterministic)
 * @param dx     maximum scaling in width of the image (pixels)
 * @param dy     maximum scaling in height of the image (pixels)
 */
public ScaleImageTransform(Random random, float dx, float dy) {
    super(random);
    this.dx = dx;
    this.dy = dy;
    this.converter = new OpenCVFrameConverter.ToMat();
}
 
Example 17
Source File: RotateImageTransform.java    From DataVec with Apache License 2.0 3 votes vote down vote up
/**
 * Constructs an instance of the ImageTransform.
 *
 * @param random  object to use (or null for deterministic)
 * @param centerx maximum deviation in x of center of rotation (relative to image center)
 * @param centery maximum deviation in y of center of rotation (relative to image center)
 * @param angle   maximum rotation (degrees)
 * @param scale   maximum scaling (relative to 1)
 */
public RotateImageTransform(Random random, float centerx, float centery, float angle, float scale) {
    super(random);
    this.centerx = centerx;
    this.centery = centery;
    this.angle = angle;
    this.scale = scale;
    this.converter = new OpenCVFrameConverter.ToMat();
}
 
Example 18
Source File: Java2DNativeImageLoader.java    From konduit-serving with Apache License 2.0 3 votes vote down vote up
/**
 * Loads a {@link INDArray} from a {@link BufferedImage}.
 *
 * @param image        as a BufferedImage
 * @param flipChannels to have a format like TYPE_INT_RGB (ARGB) output as BGRA, etc
 * @return the loaded matrix
 * @throws IOException if an error occurs creating the {@link INDArray}
 */
public INDArray asMatrix(BufferedImage image, boolean flipChannels) throws IOException {
    if (converter == null) {
        converter = new OpenCVFrameConverter.ToMat();
    }
    return asMatrix(converter.convert(converter2.getFrame(image, 1.0, flipChannels)));
}
 
Example 19
Source File: ColorConversionTransform.java    From DataVec with Apache License 2.0 2 votes vote down vote up
/**
 * Return new ColorConversion object
 *
 * @param random Random
 * @param conversionCode  to transform,
 */
public ColorConversionTransform(Random random, int conversionCode) {
    super(random);
    this.conversionCode = conversionCode;
    converter = new OpenCVFrameConverter.ToMat();
}
 
Example 20
Source File: EqualizeHistTransform.java    From DataVec with Apache License 2.0 2 votes vote down vote up
/**
 * Return contrast normalized object
 *
 * @param random Random
 * @param conversionCode  to transform,
 */
public EqualizeHistTransform(Random random, int conversionCode) {
    super(random);
    this.conversionCode = conversionCode;
    this.converter = new OpenCVFrameConverter.ToMat();
}