c++ - How to divide an OpenCV Mat in rectangular sub-regions? -


i want divide simple mat (200x200) in different regions (10x10). make 2 loops, create rect indicate variables want in each iteration (x, y, width, height). finally, save region of image inside vector of mats.

but wrong code:

mat face = mat(200, 200, cv_8uc1); vector<mat> regions; mat region_frame; int width = face.cols * 0.05; int heigth = face.rows * 0.05; for(int y=0; y<=(face.rows - heigth); y+=heigth) {     for(int x=0; x<=(face.cols - width); x+=width)     {         rect region = rect(x, y, x+width, y+heigth);         region_frame = face(region);         regions.push_back(region_frame);                 } } 

the problem in final step, it's not working size of new region_frame try create. it's increasing each iteration number of cols.

how can solve this?

opencv rect can constructed as:

rect(int _x, int _y, int _width, int _height); 

so need change line in code as:

rect region = rect(x, y, width, heigth); 

it seems instead passed coordinates of top left , bottom right corners. if want so, use other constructor:

rect(const point& pt1, const point& pt2); 

and can like:

rect region = rect(point(x, y), point(x+width, y+heigth)); 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -