如何從標準相機模塊v2中插入Raspberry Pi 3B來捕獲在OpenCV中使用的圖像?我一直試圖讓高幀率視頻工作,但使用OpenCV的VideoCapture
總是給我一個空的Mat
當我嘗試索引設備0
和this和this都產生1幀每秒或更糟糕時,我嘗試。我也研究過生成raspivid
進程,但我沒有看到讓OpenCV的VideoCapture
從該進程的輸出中讀取的方法。來自樹莓派相機的OpenCV捕獲
如何在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()
仍然假的,即使在程序反覆嘗試打開文件。
請問您可以分享一下您的代碼。 –
我編輯了我的問題以包含代碼。 – tupperkion