我有一个功能,使用C和OpenCv将图像分割成块以进行进一步处理.
这是我的代码:
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));
}
}
}
它适用于更大的区域(512 x 512工作)但是当我达到100 x 100 px的尺寸时,它给了我:
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.
问题与尺寸有关吗?如果是这样,有办法吗?
最佳答案 因为berak因为没有真正提交问题答案而臭名昭着.
您的代码需要是:
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));
}
}
}
这是为了阻止您增加到实际上没有图像的区域.