将立体相机帧拆分为左右传感器帧

splitting an stereo camera frame into left and right sensor frame

我正在使用立体相机并使用 opencv 捕获帧。捕获的帧包含来自左传感器和右传感器的图像,这些图像连接在一起成为一个图像。所以在分辨率为 640*480 的情况下,我有一个 1280 列和 480 行的图像。前 640 列属于一个传感器,641 至 1280 列属于第二个传感器。我需要将它分成左框架和右框架。我正在尝试裁剪左右帧,但出现错误。我已经删除了额外的代码并只显示了问题区域。

  cap >> frame; // get a new frame from camera
  Mat fullframe = frame(Rect(0, 0, 1280, 480 )); //only to check that I have 1280 columns and 480 rows.and this line works
  Mat leftframe= frame(Rect(0,0,640,480)); // This also works
  Mat rightframe= frame(Rect(641,0,1280,480));// this gives an error

错误出现在 cmd.exe 并且是:

    OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x +roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <=m.rows) in cv::Mat::Mat, file C:\builds_4_PackSlave-win64-vc11-shared\opencv\modules\core\src\matrix.cpp, line 323

我不明白。如果我有 1280 列,为什么我不能只保留 641 到 1280 列。大于 0 的任何值都会产生相同的错误,所以即使我使用:

    Mat rightframe= frame(Rect(1,0,1280,480)); // I still get same error

有什么帮助吗?

阅读 Rect(x, y, width, height) 的文档,其中 x, y 是左上角的坐标。因此它应该是 Mat rightframe= frame(Rect(641,0,640,480));// this gives an error.

OpenCV 通常假定矩形的上边界和左边界是包含在内的,而右边界和下边界不是。因此,我建议您的代码应如下所示。

cap >> frame;
Mat fullframe = frame(Rect(0, 0, 1280, 480 ));
Mat leftframe= frame(Rect(0, 0, 640, 480));
Mat rightframe= frame(Rect(640, 0, 640, 480));

我知道这有点晚了,但它可能会帮助其他人将来会看这个问题。