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 srcmatrix.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,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- Vulkan验证层不断在VkQueuePresentKHR()上抛出图像布局错误
- 增加 gil 读取图像错误
- 在图像上覆盖文本的程序会产生无休止的字符串错误
- 卤化物:较大图像的去马赛克算法错误.似乎适用于 16x16 图像
- 视觉C++:图像序列加载错误
- SFML 库未加载错误,找不到图像
- 使用 Nvidia NPP 调整图像大小时未记录的大小调整错误
- C++/openCV - 为什么显示图像会产生 Linux 分割错误?(打开CV错误?
- 张量流错误:执行器无法创建内核。没有注册'Snapshot' 适用于 GPU 设备的 OpKernel 运行图像标签示例
- SWscaler 错误的 SRC 图像指针
- Mat的convertTo函数在OpenCV中将灰度图像的类型转换为CV_32F时抱怨断言错误
- 幼稚的过滤返回错误的图像
- OPENCV IMREAD 8BPP PNG图像错误
- TBB免费图像lambda阵列比较错误
- GTK图像突然不会刷新任何错误或警告
- 试图均衡图像时的分割错误
- glDrawArrays的意外行为-参数顺序错误会产生所需的图像
- 尝试为图像缓冲区分配内存时 ptr 值错误
- OpenCV 错误:图像步长错误(矩阵不连续)