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
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))))
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
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
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
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
2023 年 6 月 26 日
Try cropping off the last row and column
p = p(1:end-1, 1:end-1)
then call fft2.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!