opencv从raspberry-pi摄像机捕获

aoyhnmkz  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(383)

我该如何从插在覆盆子pi3b上的标准相机模块v2中捕获一幅图像,以便在opencv中使用?我一直在尝试让高帧速率的视频工作,但使用opencv的 VideoCapture 总是给我一个空的 Mat 当我尝试索引设备时 0 ,这个和这个都产生了每秒1帧或者更糟。我也研究过如何产生 raspivid 过程,但我看不到一个方法来获得opencv的 VideoCapture 从这个过程的输出中读取。
如何在java中获得高fps帧捕获?有没有办法让opencv从 OutputStream 我从另一个过程中得到的?
编辑:下面是我的代码的简化版本。我想知道如何填充 startRecording() 该类中的函数

abstract class Main {
    public static Mat currentCapture = null;
    public static final double FOV_HEIGHT = 48.8;
    public static final int WIDTH = 480;
    public static final int HEIGHT = 384;
    public static final int VIDEO_WIDTH = WIDTH * 2;
    public static final int VIDEO_HEIGHT = HEIGHT * 2;

    static {
        System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);
    }

    public static void main(String[] args) {
        client = new VisionClientTable("10.55.6.4", 5506);
        new Thread(new VisionThread(VisionThread.Mode.VIDEO)).start();
        new Thread(new VisionThread(VisionThread.Mode.TARGETING)).start();
    }

    private static class VisionThread implements Runnable {
        private final Mode mode;

        enum Mode {
            VIDEO,
            TARGETING
        }

        public VisionThread(Mode mode) {
            this.mode = mode;
        }

        @Override
        public void run() {
            if (mode == Mode.TARGETING)
                startTracking();
            else if (mode == Mode.VIDEO)
                startRecording();
        }
    }

    public static void startTracking() {
        /* this thread repeatedly captures currentCapture and processes it */
    }

    // this is where I need help
    public static void startRecording() {
        try {
            VideoWriter video = new VideoWriter("/home/pi/vision/videos/video-" + Files.list(Paths.get("/home/pi/vision/captures")).count() + ".mp4", VideoWriter.fourcc('X', '2', '6', '4'), 30, new Size(VIDEO_WIDTH, VIDEO_HEIGHT), true);
            VideoCapture capture = new VideoCapture(0);
            capture.set(Videoio.CAP_PROP_FRAME_WIDTH, VIDEO_WIDTH);
            capture.set(Videoio.CAP_PROP_FRAME_HEIGHT, VIDEO_HEIGHT);
            Thread.sleep(2000);
            while (true) {
                long start = System.currentTimeMillis();
                Mat mat = new Mat();
                capture.read(mat);
                if (!mat.empty()) { // mat is always empty
                    Mat downscaled = new Mat();
                    Imgproc.resize(mat, downscaled, new Size(WIDTH, HEIGHT), 0, 0, Imgproc.INTER_NEAREST);
                    currentCapture = downscaled;
                }
                video.write(mat);
                long end = System.currentTimeMillis();
                if (end - start < 1000 / 60)
                    Thread.sleep(1000 / 60 - (end - start));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void log(String text) {
        System.out.println(text);
    }
}

我觉得最好的办法就是 raspivid 输出到一个文件,并以某种方式将其输出馈送到opencv中,但我不知道如何做到这一点。
编辑2:到目前为止,我已经尝试使用 Runtime.getRuntime().exec() 运行 raspivid 命令输出到一个文件,这是可行的,但是当我试图用opencv打开这个文件时, capture.isOpened() 仍然为false,即使程序反复尝试打开该文件。

3vpjnl9f

3vpjnl9f1#

从你的问题中不太了解你的代码。以下内容可能会对您有所帮助。
http://bigdinotech.com/tutorials/beaglebone-black-tutorials/building-opencv-on-the-beaglebone-black-or-raspberry-pi/

相关问题