Ask Your Question
2

Resize an image but fill the space with black ?

asked 2013-05-27 13:41:32 -0600

yes123 gravatar image

updated 2016-01-17 19:02:00 -0600

I have a small Image:

Mat myImage;

This image is of Size(100,100). Now I would like to enlarge the dimension of this image to Size(1000,1000) (filling the rest of the space with black)

I could do in this way (but I think there could be a faster solution)

 Mat largerImage(Size(1000,1000),myImage.type());
 largerImage = Scalar(0);
 myImage.copyTo(largerImage(Rect(0,0,myImage.cols,myImage.rows)));

How can I directly "expand" myImage ?

edit retag flag offensive close merge delete

Comments

2

Mat largerImage(Size(1000,1000),myImage.type()); won't fill with zeros(maybe it does for you in DEBUG, but don't rely on that), use:

Mat largerImage = Mat::zeros(Size(1000,1000),myImage.type()); instead

berak gravatar imageberak ( 2013-05-27 13:57:59 -0600 )edit

Yea I forgot to write in the question: largerImage = Scalar(0);

yes123 gravatar imageyes123 ( 2013-05-27 14:16:31 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
7

answered 2013-05-27 14:17:16 -0600

rics gravatar image

You can use the copyMakeBorder function. Border size can be set independently in each direction. Border type can be BORDER_CONSTANT with a black border value scalar.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-05-27 13:41:32 -0600

Seen: 23,971 times

Last updated: Jun 05 '13