0

What I want to do:

Convert a ROI of the Nao Robot camera image on the OpenCV::Mat format. Later I will make use of this OpenCV::Mat

The situation:

Nao SDK provide the image in a format called ALImage. It is possible to convert the ALImage to OpenCV::Mat format, but I do not need all the image, only a small ROI. Despite ALImage provides its own ROI, the methods to use it are not really helpful:

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

The question:

How can I use these ROIs?

Manuel
  • 2,236
  • 2
  • 18
  • 28

1 Answers1

0

Assuming you already have the coordinates of your ROI, you can crop your cv::Mat like this:

// 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);

Note that this does not copy the image data. Both, image and croppedImage share the same underlying raw data (a detailed example can be found in the opencv documentation). When you're done with the big source image, you can do image.release() and croppedImage = croppedImage.clone(); to deallocate all unnecessary data (outside of your ROI).

EDIT:

I didn't work with AL::ALImage::ROI yet, but the definition in alimage/alimage.h looks familiar to cv::Rect. So you can probably do the following:

// 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
// ...

I hope this helps.

Community
  • 1
  • 1
Gerrit-K
  • 1,298
  • 2
  • 15
  • 30
  • I am not asking how to use an OpenCV ROI, I am asking how to use the ALImage ROI. Please, read more carefully all the question. – Manuel Feb 05 '14 at 11:00
  • @Manuel Then please be a little more specific in what you have and what you actually want, because 'use the ALImage ROI' is pretty abstract. – Gerrit-K Feb 05 '14 at 11:14