2D/3D plot of image processing filters
convolution, image-processing, matlab, plot
Solution
You can use interp2 to find intermediate values on the same grid size for visualization purposes
step = 0.1; % granularity
[xn,yn] = meshgrid(1:step:5); % define finer grid
zn = interp2(x,y,z,xn,yn); % get new z values
surf(xn,yn,zn);
Note that you will obtain the closest approximation to your original kernel using the default linear interpolation method, i.e. `interp2(x,y,z,xn,yn,'linear')`. Using other methods will result in smoother kernels to use, but their 3D shape will differ. So it depends on your use and application.
Update:
You can by-pass the ill-posed problem of up-sampling to a much higher resolution (the inverse reconstruction is possible only if the hypothetical "downsampling respects the Nyquist sampling rate) by trying to approximate your data with a known kernel, which then you can tune.
For example, since you are give an example of a symmetric kernel, that decays isotropicaly around a maximum value, you can use a Gaussian function. MATLAB does so through `fspecial` function.
Assume the underlying function (e.g. Gaussian) and use parameters defined from your current kernel (i.e. fitting a function to your data)
% use max location, amplitude and std from your kernel
max_z = max(z(:));
std_z = std(z(:));
% Set of tunable parameters (size of grid & granularity)
bounds_grid = [30 30]; grid bounds
step = 0.5; % resolution
% Grid
siz = (bounds_grid-1)/2;
[x,y] = meshgrid(-siz(2):step:siz(2),-siz(1):step:siz(1));
% Gaussian parameters
s = std_z; m = 0;
% Analytic function
g = exp(-((x-m).^2 + (y-m).^2)/(2*s*s));
g(g<eps*max(g(:))) = 0;
g = max_z*g./max(g(:));
surf(g);
This way you respect the parameters of the kernel in the Gaussian lobe, but control the grid-size and resolution of the final Gaussian kernel.
Some examples:
Problem
i tried to understand the 2D & 3D plotting function in Matlab, regarding to the image processing filters, like box-plots, gauss, mexican hats and so on... I only got the kernel for the filters, e.g. a 5x5 matrix with the coefficents of each cell. ezsurfc won't work and I don't understand it. surf instead works, but I got no clue about the grids and how to make it more granular and smooth? My understanding of surf is, that I need the same dimensions for each param, so how should I do it without making my kernel a 20x20 or even larger? The idea is, that I get an output like the examples, i've posted. I mentioned the 20x20 grid mask of the filter, because it looks like that the smoothness and the flattening needs more coefficients than just 5x5... am I right or totally wrong? I already tried the following matlab code, example for a laplace filter: ``` [x,y] = meshgrid(1:1:5); %create a 5x5 matrix for x and y (meshes) z = [0 1 2 1 0; 1 3 5 3 1;2 5 9 5 2; 1 3 5 3 1;0 1 2 1 0]; % kernel 5x5 surf(x,y,z); ``` That gives me that output: So how do I generate a fine and granular 2D and a 3D plot out of that 5x5 kernel information? Big thanks in advance! P.S.: Hopefully my code indentions aren't messed up... otherwise feel free to edit - it's my first post on StackOverflow. :-) What I want to get, is like these two examples: