[英]OpenCV Error: Assertion failed in cv::Mat line 522
I am trying to write a function that returns smaller parts of an image. 我正在尝试编写一个返回图像较小部分的函数。 I am using the function Rect() for cutting.
我正在使用函数Rect()进行剪切。 I get no error when debugging but when I try to execute the function I get the following error:
调试时没有错误,但是当我尝试执行该功能时,出现以下错误:
OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in cv::Mat::Mat, file C:\\opencv\\opencv-master\\modules\\core\\src\\matrix.cpp, line 522
OpenCV错误:断言失败(0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows)在cv :: Mat :: Mat中,文件C:\\ opencv \\ opencv-master \\ modules \\ core \\ src \\ matrix.cpp,第522行
Here is my code: 这是我的代码:
void divideImage(Mat input_image, vector<Mat> output_images, int width_fraction, int height_fraction ) {
int width = input_image.rows / width_fraction - 1;
int height = input_image.cols / height_fraction - 1;
for (int w = 0; w < input_image.rows; w+=width) {
for (int h = 0; h < input_image.cols; h+=height) {
Mat tiles = input_image(Rect(w, h, width, height));
output_images.push_back(tiles);
}
}
}
int main(int argc, char** argv)
{
// Get parameters from command line
CommandLineParser parser(argc, argv, keys);
String image_path1 = parser.get<String>(0);
if (image_path1.empty())
{
help();
return -1;
}
// Load image
cv::Mat img_1_rgb = imread(image_path1, 1);
Mat img_1;
cvtColor(img_1_rgb, img_1, CV_BGR2GRAY);
vector<Mat> output_images(4);
divideImage(img_1, output_images, 2, 2);
Seems like my ROI is somehow out of bounds. 好像我的投资回报率超出范围。
With the help of api55 I present the correct loop: 借助api55,我展示了正确的循环:
void divideImage(Mat input_image, vector<Mat> output_images, int width_fraction, int height_fraction ) {
int width = (input_image.cols / width_fraction) - 1;
int height = (input_image.rows / height_fraction) - 1;
for (int w = 0; w < input_image.cols-width_fraction*width_fraction; w+=width) {
for (int h = 0; h < input_image.rows-height_fraction*height_fraction; h+=height) {
Mat tiles = input_image(Rect(w, h, width, height));
output_images.push_back(tiles);
//cout << w << " " << h << " " << width << " " << height << " " << endl;
}
}
} }
Your code is wrong. 您的代码是错误的。 Let me give you an example:
让我给你举个例子:
Lets say your image is of a size of 640x480 假设您的图片尺寸为640x480
now lets calculate the width variable of your function with the same parameters you use 现在让我们使用与您使用的相同参数来计算函数的width变量
int width = 640 / 2 - 1; // 319
now lets start our loop, for the first time w=0
and you get something like 现在让我们开始循环,第一次
w=0
,你会得到类似
Rect(0, 0, 319, 239)
Then for the next width iteration you will have w+=width
which is w=319
and something like 然后对于下一次宽度迭代,您将得到
w+=width
,即w=319
,类似
Rect(319, 0, 319, 239)
The second iteration will have w+=width
again which is w=638
, as you clearly can see 638 is less than the rows of my image (640), thus it will try to do 第二次迭代将再次具有
w+=width
,即w=638
,您可以清楚地看到638小于我图像的行数(640),因此它将尝试执行
Rect(638, 0, 319, 239)
which will jump the assert mentioned, since 这将跳过提到的断言,因为
roi.x + roi.width <= m.cols
roi.x + roi.width <= m.cols
will be translated to 将被翻译成
638 + 319 <= 640
which is false. 这是错误的。
You have to change how it is looping, also in the best case scenario which it works, you will loose n columns/rows, being n the number of divisions. 您必须更改其循环方式,在最佳情况下,它也会起作用,您将丢失n列/行,即n个分割数。 (you can try to put a limit like
(您可以尝试设置一个
input_image.rows - width_fraction
in the for loop check, if you do not care for the dropped columns. 在for循环检查中,如果您不关心删除的列。
Further suggestions, learn how to use the debugger!! 进一步的建议,学习如何使用调试器! it should jump on the assert, unless that you run it in release mode, if not, something is wrong, the code should fail always.
它应该跳到断言上,除非您在发布模式下运行它,否则,代码应该总是失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.