How do I change a 512*512 image code to any size code?
2 次查看(过去 30 天)
显示 更早的评论
I wrote the code to find the lowpass filter of grayscale image of size 512*512 pixels. How to change it to any other size?
A=imread('lena.png'); % read 512512 8bit image
A=double(A);
A=A/255;
SP=fftshift(fft2(fftshift(A)));
D=abs(SP);
D=D(129:384,129:384);
figure;imshow(A);
title('Original image')
figure;imshow(30.*mat2gray(D)); % spectrum
title('Original spectrum')
c=1:512;
r=1:512;
[C, R]=meshgrid(c, r);
CI=((R-257).^2+(C-257).^2);
filter=zeros(512,512);
% produce a high-pass filter
for a=1:512;
for b=1:512;
if CI(a,b)>=20^2; %filter diameter
filter(a,b)=0;
else
filter(a,b)=1;
end
end
end
G=abs(filter.*SP);
G=G(129:384,129:384);
figure;imshow(30.*mat2gray(G));
title('Low-pass spectrum')
SPF=SP.*filter;
E=abs(fftshift(ifft2(fftshift(SPF))));
figure;imshow(mat2gray(E));
title('Low-pass image')
1 个评论
Walter Roberson
2021-5-12
You did not post the code, so we do not know what implementation you used.
采纳的回答
DGM
2021-5-13
This should be a start
% this presumes the image is uint8, so be careful
% if you have IPT, you can use im2double() instead
A=double(A);
A=A/255;
% ...
% these numbers in the following rows are functions
% of the presumed image geometry and/or the size of the filter
% rewrite them in general terms; for example:
% [h w nc] = size(A);
% padsize = floor([h w]/4); % assuming the same ratio
% D = D((1+padsize):(h-padsize),(1+padsize):(w-padsize));
D=D(129:384,129:384);
% ...
c=1:512; % 1:w
r=1:512; % 1:h
% ...
CI=((R-257).^2+(C-257).^2); % 257 is ((h or w)/2 + 1)
filter=zeros(512,512); % filter=zeros(h,w);
for a=1:512 % 1:h
for b=1:512 % 1:w
% maybe filter diameter should be defined earlier as a parameter
if CI(a,b)>=20^2
% ...
end
end
end
% ...
G=G(129:384,129:384); % etc
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Read, Write, and Modify Image 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!