I have to translate from Matlab to C this function:

Code:
     impadded = padarray(impadded, padSzVec, 'replicate', 'both');
Really it pads the array copying the last elements of the object impadded of every row and column(obtening a bigger object).

What I did is: (img is the original image)

Code:
    Mat padded;
    padded.create(img.rows + padSize, img.cols + padSize,img.type());
    padded.setTo(cv::Scalar::all(0));
    padded(Rect(padSize,padSize,img.rows,img.cols);
    img.copyTo(padded);

I suppose I m padding good but inserting 0 to have a bigger object. Someone cal help me to improve the code.