6

I would like to split an image which is captured from a webcam into N*N squares, so that I can process those squares separably.

1 Answer 1

12

You need to use a ROI (Region Of Interest) option of OpenCV image structures. In C interface you need a function cvSetImageROI, in C++ it will be operator() of cv::Mat class. Here is a simple C++ sample for processing image by NxN blocks:

cv::Mat img;
capture >> img;

for (int r = 0; r < img.rows; r += N)
    for (int c = 0; c < img.cols; c += N)
    {
        cv::Mat tile = img(cv::Range(r, min(r + N, img.rows)),
                     cv::Range(c, min(c + N, img.cols)));//no data copying here
        //cv::Mat tileCopy = img(cv::Range(r, min(r + N, img.rows)),
                     //cv::Range(c, min(c + N, img.cols))).clone();//with data copying

        //tile can be smaller than NxN if image size is not a factor of N
        your_function_processTile(tile);
    }

C version:

IplImage* img;
CvRect roi;
CvSize size;
int r, c;

size = cvGetSize(img);
for (r = 0; r < size.height; r += N)
    for (c = 0; c < size.width; c += N)
    {
        roi.x = c;
        roi.y = r;
        roi.width = (c + N > size.width) ? (size.width - c) : N;
        roi.height = (r + N > size.height) ? (size.height - r) : N;

        cvSetImageROI(img, roi);

        processTile(img);
    }
cvResetImageROI(img);
8
  • 1
    Yes, cvSetImageROI is for IplImage. Sep 17, 2011 at 18:01
  • If I've an IplImage, How Can I use the cv::Mat with the above code? Sep 17, 2011 at 18:07
  • 1
    cvarrToMat can do the conversion. Sep 17, 2011 at 18:10
  • Thanks,But how do I access then cvMat tile, I would like to access its row, columns, so that I can play with those tiles... Sep 17, 2011 at 21:19
  • There are a lot of methods... I think you'd better read a couple of OpenCV tutorials before continuing. I recommend to try first two from this page Sep 17, 2011 at 21:37

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.