0

我想做的事:

以 OpenCV::Mat 格式转换 Nao Robot 相机图像的 ROI。稍后我将使用这个 OpenCV::Mat

情况:

Nao SDK 以一种名为ALImage的格式提供图像。可以将 ALImage 转换为 OpenCV::Mat 格式,但我不需要所有图像,只需要一个小的 ROI。尽管 ALImage 提供了自己的ROI,但使用它的方法并没有真正的帮助:

int        getNumOfROIs () const 
const ROI* getROI (int index) const
void       addROI (const ROI &rect)
void       cleanROIs ()
void       setEnableROIs (bool enable)
bool       isROIEnabled () const 

问题:

如何使用这些 ROI?

4

1 回答 1

0

假设您已经有了 ROI 的坐标,您可以cv::Mat这样裁剪:

// your source image
cv::Mat image(imagesource); 

// a rectangle with your ROI coordinates
cv::Rect myROI(10, 10, 100, 100);

// a "reference" to the image data within the rectangle
cv::Mat croppedImage = image(myROI);

请注意,这不会复制图像数据。两者,imagecroppedImage共享相同的底层原始数据(详细示例可以在opencv 文档中找到)。完成大源图像后,您可以执行image.release()croppedImage = croppedImage.clone();取消分配所有不必要的数据(在您的 ROI 之外)。

编辑:

我还没有使用过AL::ALImage::ROI,但是alimage/alimage.h 中的定义看起来很熟悉cv::Rect。因此,您可能可以执行以下操作:

// let's pretend you already got your image ...
AL::ALImage yourImage;
// ... and a ROI ...
AL::ALImage::ROI yourROI;
// ... as well as a Mat header with the dimensions and type of yourImage
cv::Mat header;

// then you can convert the ROI, ...
cv::Rect cvROI(yourROI.x, yourROI.y, yourROI.w, yourROI.h);
// ... wrap ALImage to a Mat (which should not copy anything) ...
header.data = yourImage.getData();
// ... and then proceed with the steps mentioned above to crop your Mat
cv::Mat cropped = header(cvROI);
header.release();
cropped = cropped.clone();

// ... 
// your image processing using cropped
// ...

我希望这有帮助。

于 2014-02-05T10:25:44.003 回答