I have a function that splits an image into blocks for further processing, using C++ and OpenCv.
This is my code:
void imageSplit(Mat image)
{
int blockNumber = 8;
// get the image data
int height = image.rows;
int width = image.cols;
//set how many blocks and create vector to store
cv::Size smallSize(height / blockNumber, width / blockNumber);
std::vector < Mat > smallImages;
for (int y = 0; y < image.rows; y += smallSize.height)
{
for (int x = 0; x < image.cols; x += smallSize.width)
{
cv::Rect rect = cv::Rect(x, y, smallSize.width, smallSize.height);
//cout << x << " " << y << " " << smallSize.width << " " << smallSize.height << endl;
smallImages.push_back(cv::Mat(image, rect));
}
}
}
It works fine with a larger region (512 x 512 works) But when i get down to sizes like 100 x 100 px, it gives me:
OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.widt
h <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in
cv::Mat::Mat, file src\matrix.cpp, line 323
default exception.
Is the issue related to a size thing? And if so, is there a way around it?
As berak is notorious for not actually submitting answers to questions..
You code needs to be:
for (int y = 0; y < image.rows-smallSize.height; y += smallSize.height)
{
for (int x = 0; x < image.cols-smallSize.width; x += smallSize.width)
{
cv::Rect rect = cv::Rect(x, y, smallSize.width, smallSize.height);
//cout << x << " " << y << " " << smallSize.width << " " << smallSize.height << endl;
smallImages.push_back(cv::Mat(image, rect));
}
}
}
This is to stop you incrementing to areas where you don't actually have an image.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.