How can I get the result of filter2d is equivalent to filter2 in Matlab

The filter2 in Matlab gives an option ‘same’ to Return the central part of the filtered data, which is the same size as X .
filter2() usage in Matlab
Y = filter2(H,X,shape)

opencv’s filter2D will always return a Mat with the same size as the input.
(depending on what BORDER_TYPE you use, it will get padded internally with Ksize/2, but the result gets cropped back to original size)