Get rid off white border in spectrum

1 次查看(过去 30 天)
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 个评论
VBBV
VBBV 2023-6-26
编辑:VBBV 2023-6-26
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

请先登录,再进行评论。

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-6-26
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 个评论
Image Analyst
Image Analyst 2023-6-26
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 2023-6-26
编辑:DGM 2023-6-26
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.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2023-6-26
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