Skip to main content
tried the suggestion
Source Link
import numpy as np from skimage import transform ysdelta = np.array([ [0.1639, 0.2759, 0.4142, 0.1323, 0.1234]1234, 0.2311, 0.3421], [0.5213, 0.5566, 0.6580, 0.2131, 0.1923]1923, 0.3422, 0.4312], [0.7943, 0.8750, 1.0000, 0.4231, 0.1233]1233, 0.2312, 0.5123], [0.1212, 0.0012, 0.1232, 0.1021, 0.1530]1530, 0.2313, 0.3412], ] [0.4321, dtype=np0.float645123, 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] ]) ys_resized filter_kernel = transformnp.resizearray([ ys[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(2delta, 2filter_kernel, mode='same', boundary='symm') # Resize to 5x5 using bilinear interpolation delta_resized = resize( ys_filtered, (5, 5), order=1,  # bilinear mode='edge'mode='wrap', # mimic MATLAB edge replication anti_aliasing=Trueanti_aliasing=False, # no anti-aliasing to match MATLAB closely preserve_range=Truepreserve_range=False )   print(ys_resizeddelta_resized) 
imgdelta = zeros(7,7); delta(4,4) = 1; [0% single pixel in the middle delta = [ 0.1639, 0.2759, 0.4142, 0.1323, 0.1234;1234, 0.2311, 0.3421; 0.5213, 0.5566, 0.6580, 0.2131, 0.1923;1923, 0.3422, 0.4312; 0.7943, 0.8750, 1.0000, 0.4231, 0.1233;1233, 0.2312, 0.5123; 0.1212, 0.0012, 0.1232, 0.1021, 0.1530];1530, 0.2313, 0.3412; res  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(imgdelta, [2,2][5, "Method"5], 'bilinear'); % or your target size disp(resized) 

Question: Is thereHere i have generated a way to exactly replicate MATLAB’s imresize behavior in Pythonoutput for arbitrary arrays? Are there any subtle differences7*7 matrix in how MATLABmatlab consisting of all 0 and Python handle bilinear interpolation,center pixel centers, or anti-aliasing1 and used that I should be aware of?output in python by first convolving it and then resizing it , but still for the same array in matlab and python output is different

import numpy as np from skimage import transform ys = np.array([ [0.1639, 0.2759, 0.4142, 0.1323, 0.1234], [0.5213, 0.5566, 0.6580, 0.2131, 0.1923], [0.7943, 0.8750, 1.0000, 0.4231, 0.1233], [0.1212, 0.0012, 0.1232, 0.1021, 0.1530] ], dtype=np.float64) ys_resized = transform.resize( ys, (2, 2), order=1, # bilinear mode='edge', anti_aliasing=True, preserve_range=True ) print(ys_resized) 
img = [0.1639, 0.2759, 0.4142, 0.1323, 0.1234; 0.5213, 0.5566, 0.6580, 0.2131, 0.1923; 0.7943, 0.8750, 1.0000, 0.4231, 0.1233; 0.1212, 0.0012, 0.1232, 0.1021, 0.1530]; res = imresize(img, [2,2], "Method", 'bilinear'); 

Question: Is there a way to exactly replicate MATLAB’s imresize behavior in Python for arbitrary arrays? Are there any subtle differences in how MATLAB and Python handle bilinear interpolation, pixel centers, or anti-aliasing that I should be aware of?

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) 
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) 

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

Source Link

Why does Python skimage.transform.resize give different results from MATLAB imresize for the same array?

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

Python

import numpy as np from skimage import transform ys = np.array([ [0.1639, 0.2759, 0.4142, 0.1323, 0.1234], [0.5213, 0.5566, 0.6580, 0.2131, 0.1923], [0.7943, 0.8750, 1.0000, 0.4231, 0.1233], [0.1212, 0.0012, 0.1232, 0.1021, 0.1530] ], dtype=np.float64) ys_resized = transform.resize( ys, (2, 2), order=1, # bilinear mode='edge', anti_aliasing=True, preserve_range=True ) print(ys_resized) 

Matlab

img = [0.1639, 0.2759, 0.4142, 0.1323, 0.1234; 0.5213, 0.5566, 0.6580, 0.2131, 0.1923; 0.7943, 0.8750, 1.0000, 0.4231, 0.1233; 0.1212, 0.0012, 0.1232, 0.1021, 0.1530]; res = imresize(img, [2,2], "Method", 'bilinear'); 

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: Is there a way to exactly replicate MATLAB’s imresize behavior in Python for arbitrary arrays? Are there any subtle differences in how MATLAB and Python handle bilinear interpolation, pixel centers, or anti-aliasing that I should be aware of?