繁体   English   中英

从树莓派相机捕获OpenCV

[英]OpenCV capture from raspberry pi camera

我该如何从插入Raspberry Pi 3B的标准相机模块v2中捕获要在OpenCV中使用的图像? 我一直试图让高帧速率视频的工作,但使用的OpenCV的VideoCapture总是给我一个空Mat ,当我尝试索引设备0 ,而两个产生每秒或当我试图更糟1帧。 我也研究过产生一个raspivid进程,但是我没有找到一种方法来使OpenCV的VideoCapture从该进程的输出中读取。

如何在Java中获得较高的FPS帧捕获? 有没有办法让我从另一个进程获取的OutputStream中读取OpenCV?

编辑 :下面是我的代码的简化版本。 我想知道如何在此类中填充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。

从您的问题中获得关于代码的太多想法。 以下内容可能会对您有所帮助。

http://bigdinotech.com/tutorials/beaglebone-black-tutorials/building-opencv-on-the-beaglebone-black-or-raspberry-pi/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM