Suppose I have a Mat variable small_image with dimensions 98x158x32 (type float). Now I want to zero pad this image (ie. add a border of zeros to the image). I want to add 7 zeros above and below the image, and 12 zeros left and right of the image. The first idea was to use the cv copyMakeBorder (see copyMakeBorder doc), which seems perfect for this:
int old_size[3];
old_size[0] = 98;
old_size[1] = 158;
old_size[2] = 32;
int pad_size[3];
pad_size[0] = old_size[0] + 2 * 7;
pad_size[1] = old_size[1] + 2 * 12;
pad_size[2] = old_size[2];
cv::Mat image_padded(3, pad_size, CV_32FC1, cv::Scalar(0)); //Initialize the larger Mat to 0
copyMakeBorder(small_image,image_padded,7,7,12,12,BORDER_CONSTANT,Scalar(0));
However, this code gives a memcopy error. Does anyone see the problem here?
The alternative as described in this post does not work either:
cv::Rect roi( cv::Point( 12, 7 ), small_image.size() );
small_image.copyTo( image_padded( roi ) );
It claims that "Assertion failed (m.dims <= 2)", while both Mat variables are 3D matrices.
Any help to achieve the zero padding would be greatly appreciated!