First time here? Check out the FAQ!

Ask Your Question
0

Assertion failed roi out of range

asked Apr 13 '18

open_ranger gravatar image

I'm trying to make a mask with black margin around edges so I type the follwoing code:

image=imread("/home/wxh/Desktop/Photo/1.jpg",IMREAD_UNCHANGED);
width=image.cols;
height=image.rows;
edge_left=image.cols*0.05;edge_top=image.rows*0.05;
mask=Mat::zeros(width,height,CV_8UC1);

printf("Mask rows:   %d\t cols: %d\t type:  %d\t channels: %d\t\n",mask.rows,mask.cols,mask.type(),mask.channels());
printf("Rectangle x: %d\t    y: %d\t width: %d\t height:   %d\t\n",edge_left,edge_top,width-2*edge_left,height-2*edge_top);

mask2=mask(Rect(edge_left,edge_top,width-2*edge_left,height-2*edge_top));

The console output give me this

Mask rows:   3264    cols: 2448  type:  0    channels: 1    
Rectangle x: 163        y: 122   width: 2938     height:   2204 
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 Mat, file /home/wxh/opencv-3.2.0/modules/core/src/matrix.cpp, line 522
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/wxh/opencv-3.2.0/modules/core/src/matrix.cpp:522: error: (-215) 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 function Mat

Basiclly it says the rectangle roi is out of image range but I dont see how. Anyboady can point out the error for me?

Preview: (hide)

1 answer

Sort by » oldest newest most voted
0

answered Apr 13 '18

berak gravatar image

updated Apr 13 '18

you swapped rows & cols here:

mask = Mat::zeros(width,height,CV_8UC1);

it must be:

mask = Mat::zeros(height,width,CV_8UC1);

(common pitfall here !)

Preview: (hide)

Comments

1

sometimes the error just right in front of me but I just cant see.

open_ranger gravatar imageopen_ranger (Apr 13 '18)edit

that's why we have this site, no ? more eyeballs render anybug shallow.

berak gravatar imageberak (Apr 13 '18)edit

Question Tools

1 follower

Stats

Asked: Apr 13 '18

Seen: 2,011 times

Last updated: Apr 13 '18