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!