Get rid off white border in spectrum
1 view (last 30 days)
Show older comments
Hi there, I want to plot the
spectrum of a grayscale image.
Unfortunately there is a white border on the right side
and at the bottom.
My code:
x = 1:1:30; % vorher>: 0:1:30;
y = 1:1:30; % vorher: 0:1:30;
[X, Y] = meshgrid(x, y);
% Sinusfunktion für diagonale Linien
fx = 0.1; % Ortsfrequenz (1/Pixel)
fy = 0.2; % Ortsfrequenz (1/Pixel)
p = cos(2*pi*(fx*X + fy*Y)) + 1;
y1 = fft2(p);
freq_x = fftfreq(length(x));
freq_y = fftfreq(length(y));
figure;
imagesc(freq_x, freq_y, log10(1 + abs(fftshift(y1))));
line([0,0], [-0.5,0.5], 'Color', 'r', 'LineWidth',1);
line([-0.5,0.5], [0,0], 'Color', 'r', 'LineWidth',1);
axis image
colormap('gray');
The spectrum currently looks like this:
This is my fftfreq function:
function freqs = fftfreq(n, fs)
end
if nargin < 2
fs = 1;
end
results = zeros(1, n);
N = fix((n-1)/2) + 1;
results(1:N) = 0:N-1;
results(N+1:end) = (-fix(n/2):-1);
freqs = fftshift(results * (fs / n));
How can I remove the white border?
1 Comment
VBBV
on 26 Jun 2023
Edited: VBBV
on 26 Jun 2023
Choose min & max range values for the X & Y scales in the axis function instead of default scaling of axis image
x = 1:1:30; % vorher>: 0:1:30;
y = 1:1:30; % vorher: 0:1:30;
[X, Y] = meshgrid(x, y);
% Sinusfunktion für diagonale Linien
fx = 0.1; % Ortsfrequenz (1/Pixel)
fy = 0.2; % Ortsfrequenz (1/Pixel)
p = cos(2*pi*(fx*X + fy*Y)) + 1;
y1 = fft2(p);
freq_x = fftfreq(length(x));
freq_y = fftfreq(length(y));
figure;
h = imagesc(freq_x, freq_y, log10(1 + abs(fftshift(y1))))
line([0,0], [-0.5,0.5], 'Color', 'r', 'LineWidth',1);
line([-0.5,0.5], [0,0], 'Color', 'r', 'LineWidth',1);
axis([min(freq_x) max(freq_x) min(freq_y) max(freq_y) ]);
colormap('gray');
function freqs = fftfreq(n, fs)
if nargin < 2
fs = 1;
end
results = zeros(1, n);
N = fix((n-1)/2) + 1;
results(1:N) = 0:N-1;
results(N+1:end) = (-fix(n/2):-1);
freqs = fftshift(results * (fs / n));
end
Accepted Answer
Dyuman Joshi
on 26 Jun 2023
It's not a border, it's the area of figure where there is no data to plot, and the white background is showing.
You can either remove/modify the axis command, and/or update the x and y limits manually
x = 1:1:30; % vorher>: 0:1:30;
y = 1:1:30; % vorher: 0:1:30;
[X, Y] = meshgrid(x, y);
% Sinusfunktion für diagonale Linien
fx = 0.1; % Ortsfrequenz (1/Pixel)
fy = 0.2; % Ortsfrequenz (1/Pixel)
p = cos(2*pi*(fx*X + fy*Y)) + 1;
y1 = fft2(p);
freq_x = fftfreq(length(x));
freq_y = fftfreq(length(y));
figure;
imagesc(freq_x, freq_y, log10(1 + abs(fftshift(y1))));
line([0,0], [-0.5,0.5], 'Color', 'r', 'LineWidth',1);
line([-0.5,0.5], [0,0], 'Color', 'r', 'LineWidth',1);
%axis command commented out
%axis image
colormap('gray');
function freqs = fftfreq(n, fs)
if nargin < 2
fs = 1;
end
results = zeros(1, n);
N = fix((n-1)/2) + 1;
results(1:N) = 0:N-1;
results(N+1:end) = (-fix(n/2):-1);
freqs = fftshift(results * (fs / n));
end
4 Comments
Image Analyst
on 26 Jun 2023
fft2 operates on the whole image, not just what is visible.
p = cos(2*pi*(fx*X + fy*Y)) + 1;
y1 = fft2(p);
So how will changing the tick marks or axis limits after the above code change y1 at all?
DGM
on 26 Jun 2023
Edited: DGM
on 26 Jun 2023
The goal isn't to change y1 at all. This is apparently a matter of axes extents. The range of freq_x and freq_y are both [-0.5 0.4667]. That is the extent of the image object in the current axes. The two red lines span [-0.5 0.5], forcing the axes to be larger than the image object. Like @Dyuman Joshi says, there is no border to crop off.
More Answers (1)
Image Analyst
on 26 Jun 2023
Try cropping off the last row and column
p = p(1:end-1, 1:end-1)
then call fft2.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!