2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-05-30 19:12:05 -07:00
|
|
|
|
|
|
|
|
package edu.wpi.cscore;
|
|
|
|
|
|
|
|
|
|
import edu.wpi.cscore.VideoMode.PixelFormat;
|
2020-12-29 22:45:16 -08:00
|
|
|
import org.opencv.core.Mat;
|
2019-05-30 19:12:05 -07:00
|
|
|
|
|
|
|
|
public class RawCVMatSource extends ImageSource {
|
|
|
|
|
/**
|
|
|
|
|
* Create an OpenCV source.
|
|
|
|
|
*
|
|
|
|
|
* @param name Source name (arbitrary unique identifier)
|
|
|
|
|
* @param mode Video mode being generated
|
|
|
|
|
*/
|
|
|
|
|
public RawCVMatSource(String name, VideoMode mode) {
|
2020-12-29 22:45:16 -08:00
|
|
|
super(
|
|
|
|
|
CameraServerJNI.createRawSource(
|
|
|
|
|
name, mode.pixelFormat.getValue(), mode.width, mode.height, mode.fps));
|
2019-05-30 19:12:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create an OpenCV source.
|
|
|
|
|
*
|
|
|
|
|
* @param name Source name (arbitrary unique identifier)
|
|
|
|
|
* @param pixelFormat Pixel format
|
|
|
|
|
* @param width width
|
|
|
|
|
* @param height height
|
|
|
|
|
* @param fps fps
|
|
|
|
|
*/
|
2020-12-29 22:45:16 -08:00
|
|
|
public RawCVMatSource(
|
|
|
|
|
String name, VideoMode.PixelFormat pixelFormat, int width, int height, int fps) {
|
2019-05-30 19:12:05 -07:00
|
|
|
super(CameraServerJNI.createRawSource(name, pixelFormat.getValue(), width, height, fps));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Put an OpenCV image and notify sinks.
|
|
|
|
|
*
|
2020-12-29 22:45:16 -08:00
|
|
|
* <p>Only 8-bit single-channel or 3-channel (with BGR channel order) images are supported. If the
|
|
|
|
|
* format, depth or channel order is different, use Mat.convertTo() and/or cvtColor() to convert
|
|
|
|
|
* it first.
|
2019-05-30 19:12:05 -07:00
|
|
|
*
|
|
|
|
|
* @param image OpenCV image
|
|
|
|
|
*/
|
|
|
|
|
public void putFrame(Mat image) {
|
|
|
|
|
int channels = image.channels();
|
|
|
|
|
if (channels != 1 && channels != 3) {
|
|
|
|
|
throw new VideoException("Unsupported Image Type");
|
|
|
|
|
}
|
|
|
|
|
int imgType = channels == 1 ? PixelFormat.kGray.getValue() : PixelFormat.kBGR.getValue();
|
2020-12-29 22:45:16 -08:00
|
|
|
CameraServerJNI.putRawSourceFrame(
|
|
|
|
|
m_handle,
|
|
|
|
|
image.dataAddr(),
|
|
|
|
|
image.width(),
|
|
|
|
|
image.height(),
|
|
|
|
|
imgType,
|
|
|
|
|
(int) image.total() * channels);
|
2019-05-30 19:12:05 -07:00
|
|
|
}
|
|
|
|
|
}
|