OpenCv图像块,大小错误?
OpenCv image blocks, size error?
我有一个函数可以使用 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 像素这样的尺寸时,它给了我:
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));
}
}
}
这是为了阻止您递增到实际上没有图像的区域。
我有一个函数可以使用 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 像素这样的尺寸时,它给了我:
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));
}
}
}
这是为了阻止您递增到实际上没有图像的区域。