Spline smoothing in images
Show older comments
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
on 8 Feb 2013
0 votes
Example1D.m and Example2D.m in this FEX file might be a good place to start
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
sudesh
on 8 Feb 2013
Image Analyst
on 8 Feb 2013
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);
Mehri Mehrnia
on 7 May 2022
0 votes
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
Image Analyst
on 7 May 2022
You can use scatteredInterpolant or griddedInterpolant. See attached demo.
Walter Roberson
on 7 May 2022
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.
Mehri Mehrnia
on 13 Jun 2022
You mentioned valuable points but I didn't get all of them. what is "Image Analyst posted code for"?
Walter Roberson
on 13 Jun 2022
https://www.mathworks.com/matlabcentral/answers/62692-spline-smoothing-in-images#comment_128156 is the posted code
Categories
Find more on Smoothing and Denoising in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!