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
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 Comments
Andrey Kamaev
Yes, cvSetImageROI is for IplImage.
Andre Ahmed
If I've an IplImage, How Can I use the cv::Mat with the above code?
Andrey Kamaev
cvarrToMat can do the conversion.
Andre Ahmed
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...
Andrey Kamaev
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
|