0
$\begingroup$

I'm trying to replicate MATLAB's imresize behavior in Python using skimage.transform.resize. Here's my test case:

Python

delta = np.array([
    [0.1639, 0.2759, 0.4142, 0.1323, 0.1234, 0.2311, 0.3421],
    [0.5213, 0.5566, 0.6580, 0.2131, 0.1923, 0.3422, 0.4312],
    [0.7943, 0.8750, 1.0000, 0.4231, 0.1233, 0.2312, 0.5123],
    [0.1212, 0.0012, 0.1232, 0.1021, 0.1530, 0.2313, 0.3412],
    [0.4321, 0.5123, 0.6123, 0.3121, 0.2132, 0.3212, 0.4321],
    [0.2312, 0.3412, 0.4512, 0.2211, 0.1234, 0.2345, 0.3456],
    [0.6543, 0.7654, 0.8765, 0.4321, 0.3212, 0.4321, 0.5432]
])


filter_kernel = np.array([
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0.4050, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0]
])

# Step 1: Convolve input with the delta filter
ys_filtered = convolve2d(delta, filter_kernel, mode='same', boundary='symm')

# Resize to 5x5 using bilinear interpolation
delta_resized = resize(
    ys_filtered,
    (5, 5),
    order=1,          # bilinear
    mode='wrap',      # mimic MATLAB edge replication
    anti_aliasing=False,  # no anti-aliasing to match MATLAB closely
    preserve_range=False
)

print(delta_resized)

Matlab

delta = zeros(7,7);
delta(4,4) = 1;  % single pixel in the middle
delta = [
    0.1639, 0.2759, 0.4142, 0.1323, 0.1234, 0.2311, 0.3421;
    0.5213, 0.5566, 0.6580, 0.2131, 0.1923, 0.3422, 0.4312;
    0.7943, 0.8750, 1.0000, 0.4231, 0.1233, 0.2312, 0.5123;
    0.1212, 0.0012, 0.1232, 0.1021, 0.1530, 0.2313, 0.3412;
    0.4321, 0.5123, 0.6123, 0.3121, 0.2132, 0.3212, 0.4321;
    0.2312, 0.3412, 0.4512, 0.2211, 0.1234, 0.2345, 0.3456;
    0.6543, 0.7654, 0.8765, 0.4321, 0.3212, 0.4321, 0.5432
];
resized = imresize(delta, [5,5], 'bilinear');  % or your target size
disp(resized)

Problem: Even though I’m using bilinear interpolation and trying different modes in Python (mode='edge', mode='reflect', etc.), the output from skimage.transform.resize does not match MATLAB's imresize.

I’ve also tried cv2.resize and scipy.ndimage.zoom, but the results are still different.

Question: Here i have generated a output for 7*7 matrix in matlab consisting of all 0 and center pixel 1 and used that output in python by first convolving it and then resizing it , but still for the same array in matlab and python output is different

New contributor
Researcher is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
$\endgroup$
1
  • $\begingroup$ @Marcus Müller Tried what i understood from your comment, is this what you wanted me to do? Else can you kindly explain a bit more? $\endgroup$ Commented Nov 18 at 9:52

1 Answer 1

1
$\begingroup$

The choice of anti-alias filter is arbitrary, i.e., different tools use different filters, depending on what they were optimized for (or, quite honestly, especially with older tools, what the author had lying around).

Is there a way to exactly replicate MATLAB’s imresize behavior in Python for arbitrary arrays?

Sure, actually apply the anti-aliasing filter yourself during resampling. It should be fairly easy to reverse-engineer that filter's impulse response by feeding in an all-0-but-1-pixel image into imresize.

$\endgroup$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.