Azzera filtri
Azzera filtri

How do I change a 512*512 image code to any size code?

4 visualizzazioni (ultimi 30 giorni)
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')

Risposta accettata

DGM
DGM il 13 Mag 2021
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

Più risposte (0)

Categorie

Scopri di più su Read, Write, and Modify Image in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by