Ask Your Question
0

Assertion failed roi out of range

asked 2018-04-13 10:44:00 -0600

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?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-04-13 10:51:01 -0600

berak gravatar image

updated 2018-04-13 10:53:11 -0600

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 !)

edit flag offensive delete link more

Comments

1

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

open_ranger gravatar imageopen_ranger ( 2018-04-13 11:00:36 -0600 )edit

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

berak gravatar imageberak ( 2018-04-13 11:04:57 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-04-13 10:44:00 -0600

Seen: 1,899 times

Last updated: Apr 13 '18