Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can extract rectangular regions from a Mat.

cv::Mat bigImage = cv::Mat::zeros(cv::Size(660,350));
cv::Mat smallImage = cv::Mat(bigImage, cv::Rect(0,0,110,70));

cv::Rect takes the x, y, width, height as constructor parameters.

Note that this will not copy the image data, it just creates another wrapper to the same image data. If you need data copy, use

cv::Mat smallImage = cv::Mat(bigImage, cv::Rect(0,0,110,70)).clone();

Now if you need to extract multiple such image parts, essentially slicing your image with a grid, you can create a for loop as follows.

cv::Size smallSize(110,70);
std::vector<Mat> smallImages;

for (int y = 0; y < bigImage.rows; y += smallSize.height)
    for (int x = 0; x < bigImage.cols; x += smallSize.width)
        cv::Rect rect =  cv::Rect(x,y, smallSize.width, smallSize.height);
        smallImages.push_back(cv::Mat(bigImage, rect));

Here I assumed that the size of the bigImage is an integer multiple of the size of the desired small images.