Get rid off white border in spectrum

1 visualización (últimos 30 días)
Dominik Deml
Dominik Deml el 26 de Jun. de 2023
Editada: DGM el 26 de Jun. de 2023
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 comentario
VBBV
VBBV el 26 de Jun. de 2023
Editada: VBBV el 26 de Jun. de 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))))
h =
Image with properties: CData: [30×30 double] CDataMapping: 'scaled' Show all properties
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

Iniciar sesión para comentar.

Respuesta aceptada

Dyuman Joshi
Dyuman Joshi el 26 de Jun. de 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 comentarios
Image Analyst
Image Analyst el 26 de Jun. de 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
DGM el 26 de Jun. de 2023
Editada: DGM el 26 de Jun. de 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.

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 26 de Jun. de 2023
Try cropping off the last row and column
p = p(1:end-1, 1:end-1)
then call fft2.

Community Treasure Hunt

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

Start Hunting!

Translated by