# Creating a sub array of a Mat throws assertion error

Hey there,

In my program, I am looping through a 2D Mat, using a rectangle with width 5 and height 1, and checking float point values in this sub array. My code goes as follow:

for (int i = 0; i < dist.rows; ++i) {
for (int j = 2; j < dist.cols -2; ++j) {
search_mask = dist(Rect(i, j - 2, 5, 1).clone();
if (local_max.x == i) {
skeleton.at<uchar>(i, j) = 255;
}
}
}


My original picture has size 640*363. When the first loop reached the value i = 359, I get an assertion 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

As far as I can tell, the problem is, that when i = 359, roi.x = 359, roi. width = 5 and so roi.x + roi.width = 364, and m.cols = 363 and so 364 <= 363 is false.

My first question is, am I doing something wrong here?

My second question, why the hell would the number of rows matter when it comes to deciding if the number of columns in the sub array is not greater, than the number of columns in the original image.

Also, sorry if I have left something important out.

edit retag close merge delete

Sort by » oldest newest most voted If you want to extract a rectangular area from dist, the recommended way is

dist.operator()(Rect(i,j-2,5,1))


search_mask = dist.operator()(Rect(i, j - 2, 5, 1)).clone();

more

Thank you, but unfortunately this does not solve my problem.

1

Rect should be defined with Rect(j-2,i,5,1). You are using row number in place of x.

You are right, now I just don't understand why they would mark the y coordinate with x and vice versa...

2

You can establish a convention to use (c,r) for (column,row) in place of (x,y) to avoid confusion.

What I don't understand is, that OpenCV has the (0,0) coordinate at the upper left corner, the first coordinate is for the row, the second is for the column, and the Rect function asks for an x coordinate first, then a y, so it should ask for the row first. Yet, it works only, when I provide the column first. Or am I missing out on something?

If you take your origin at the top left corner, then getting a (x,y) coordinate is exactly (cols,rows). Your x axis is always the horizontal one, and y the vertical one. Do you get it now?

You see, my problem is, that if you have the following code for example:

    for (int r = 0; r < image.rows; ++r) {
for (int c = 0; c < image.cols; ++c) {
image.at<type>(r,c) = something here;
}
}


Then you are accessing pixels using (row, column) not (column, row).

UPDATE:

I have just done some simple drawings, which resulted in exactly what you said, the origin is at the top left corner, and coordinates can be accessed using (column, row).

Considering this, you see, why it is confusing, in my code, you can access pixels using (row, column)

Official site

GitHub

Wiki

Documentation