Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | package edu.wpi.cscore; |
| 6 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 7 | import edu.wpi.cscore.VideoMode.PixelFormat; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 8 | import org.opencv.core.Mat; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 9 | |
| 10 | public class RawCVMatSource extends ImageSource { |
| 11 | /** |
| 12 | * Create an OpenCV source. |
| 13 | * |
| 14 | * @param name Source name (arbitrary unique identifier) |
| 15 | * @param mode Video mode being generated |
| 16 | */ |
| 17 | public RawCVMatSource(String name, VideoMode mode) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 18 | super( |
| 19 | CameraServerJNI.createRawSource( |
| 20 | name, mode.pixelFormat.getValue(), mode.width, mode.height, mode.fps)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Create an OpenCV source. |
| 25 | * |
| 26 | * @param name Source name (arbitrary unique identifier) |
| 27 | * @param pixelFormat Pixel format |
| 28 | * @param width width |
| 29 | * @param height height |
| 30 | * @param fps fps |
| 31 | */ |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 32 | public RawCVMatSource( |
| 33 | String name, VideoMode.PixelFormat pixelFormat, int width, int height, int fps) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 34 | super(CameraServerJNI.createRawSource(name, pixelFormat.getValue(), width, height, fps)); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Put an OpenCV image and notify sinks. |
| 39 | * |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 40 | * <p>Only 8-bit single-channel or 3-channel (with BGR channel order) images are supported. If the |
| 41 | * format, depth or channel order is different, use Mat.convertTo() and/or cvtColor() to convert |
| 42 | * it first. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 43 | * |
| 44 | * @param image OpenCV image |
| 45 | */ |
| 46 | public void putFrame(Mat image) { |
| 47 | int channels = image.channels(); |
| 48 | if (channels != 1 && channels != 3) { |
| 49 | throw new VideoException("Unsupported Image Type"); |
| 50 | } |
| 51 | int imgType = channels == 1 ? PixelFormat.kGray.getValue() : PixelFormat.kBGR.getValue(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 52 | CameraServerJNI.putRawSourceFrame( |
| 53 | m_handle, |
| 54 | image.dataAddr(), |
| 55 | image.width(), |
| 56 | image.height(), |
| 57 | imgType, |
| 58 | (int) image.total() * channels); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 59 | } |
| 60 | } |