Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Assertion failed roi out of range

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?