Spline smoothing in images

Can anyone guide me how we use splines for interpolation in images? I mean how we relate value of x,y of spline function in an image that has its spatial coordinates and intensity values.

Answers (3)

Matt J
Matt J on 8 Feb 2013

0 votes

2 Comments

You would have to change 1 line to convert Example1D.m from an interpolating spline to a smoothing spline
sFit = BasisFine*(BasisFine\sFine);
and similarly for Example2D.m
how can we smooth a local window(like 3X3) of an image using this spline? As I got till now that x and y parameters of spline function are row/column of image/window_of_image.

Sign in to comment.

Image Analyst
Image Analyst on 8 Feb 2013

0 votes

A 2D Savitzky Golay filter is similar, and I already have code for that. Basically it replaces each pixel with a polynomial fit over a sliding window. It gives the effect of smoothing. Let me know if you have the Signal Processing Toolbox and would like my demo.

2 Comments

I would like to see the demo. I have the signal processing toolbox but can't it be shown just using image processing toolbox. Its like I am purely from computer science background and it takes some time to me to understand the signal part. But I will try to grasp the point.
The sgolay function is not contained in the Image Processing Toolbox. What is does is fit a 25 element 1D vector of gray levels to a polynomial (I used 1 but you can use order 3 if you want) and then it replaced the center value with the value from the fitted curve. It does this column by column (sliding the 25 element window down the rows in each column), and then it does it row by row (blurring across columns) to get a 2D blur.
Try this:
% Filter using Savitzky-Golay filtering.
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
% Check that user has the Image Processing Toolbox installed.
hasSPT = license('test', 'signal_toolbox');
if ~hasSPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Signal Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in standard MATLAB gray scale demo image.
% imageArray = imread('football.jpg');
imageArray = imread('cameraman.tif');
imageArray = double(imageArray);
[rows columns numberOfColorBands] = size(imageArray);
subplot(2, 2, 1);
imshow(imageArray, [0 255]);
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full screen.
set(gcf,'name','Savitzky-Golay Filter Image Analysis Demo','numbertitle','off')
% Apply the Savitzky-Golay filter.
% First apply it in the vertical (row) direction.
k = 1; % Order of the polynomial
windowSize = 25;
verticallySmoothedImage = sgolayfilt(imageArray, k, windowSize, [], 1);
subplot(2, 2, 2);
imshow(verticallySmoothedImage, [0 255]);
title('Savitzky-Golay filtered in the vertical direction only', 'FontSize', fontSize);
% Apply the Savitzky-Golay filter.
% First apply it in the vertical (row) direction.
k = 1; % Order of the polynomial
windowSize = 25;
horizontallySmoothedImage = sgolayfilt(imageArray, k, windowSize, [], 2);
subplot(2, 2, 3);
imshow(horizontallySmoothedImage, [0 255]);
title('Savitzky-Golay filtered in the horizontal direction only', 'FontSize', fontSize);
doublySmoothedImage = sgolayfilt(verticallySmoothedImage, k, windowSize, [], 2);
subplot(2, 2, 4);
imshow(doublySmoothedImage, [0 255]);
title('Savitzky-Golay filtered in both directions', 'FontSize', fontSize);

Sign in to comment.

Mehri Mehrnia
Mehri Mehrnia on 7 May 2022
I have the same question. In all spline questins, we map coordinates of points and then interpolate.
But, for imaging, how I can map the matrix arrays and interpolate????

4 Comments

You can use scatteredInterpolant or griddedInterpolant. See attached demo.
When you are smoothing an image, there are two different approaches.
In one approach, you somehow locate a line in the image. The location of the pixels in the line would then be the coordinates for spline purposes. You might do this if the image is a picture of a line plot.
In the second approach, which is the one Image Analyst posted code for, you take a sliding window onto an image, and everything inside the window is information used to do a fitting and predict a new center value. When this approach is used, then the coordinates you would use would be the relative offset of the pixel compared to the center pixel that is the focus at the time. Absolute coordinates would not be needed.
You mentioned valuable points but I didn't get all of them. what is "Image Analyst posted code for"?
https://www.mathworks.com/matlabcentral/answers/62692-spline-smoothing-in-images#comment_128156 is the posted code

Sign in to comment.

Tags

Asked:

on 8 Feb 2013

Commented:

on 13 Jun 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!