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!