假设您已经有了 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);
请注意,这不会复制图像数据。两者,image
并croppedImage
共享相同的底层原始数据(详细示例可以在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
// ...
我希望这有帮助。