Answer To: 7060 Image Sensors & Processing ELEC ENG 061/7060 Image Processing Page 1 of 7 7060 Image Sensors &...
Anupam answered on Aug 26 2020
A1_code/adap_med_filt.asv
% Adaptive median filter. Adaptively use up to 7x7 median filtering to
% remove salt and pepper noise from an image
%
function Iamf = adap_med_filt(I)
if (isa(I,'uint8'))
I=double(I(:,:,1))/255;
end
Iamf=I;
% find the nxn local maxima and minima images (required for your solution)
Imax = max_filter(I,3);
Imin = min_filter(I,3);
% compute 3x3, 5x5 and 7x7 median images (required for your solution)
Imed3 = med_filter(I,3);
Imed5 = med_filter(I,5);
Imed7 = med_filter(I,7);
% ---- ADD YOUR CODE HERE ----
% SEE Assignment sheet for algorithm. In short if point is same as local max
% or min use med3x3, if still max or min use med5x5 else med7x7.
[rows,cols]=size(I)
for i=4:rows-4
for j=4:cols-4
if Iamf(i,j)==Imax(i,j) || Iamf(i,j)==Imin(i,j)
if Imed3(i,j)==Imax(i,j) || Imed3(i,j)==Imin(i,j)
if Imed5(i,j)==Imax(i,j) || Imed5(i,j)==Imin(i,j)
if Imed7(i,j)==Imax(i,j) || Imed7(i,j)==Imin(i,j)
Iamf=I;
%Iamf = rand(size(I)); % <-- dummy random noise result
% ---- ADD YOUR CODE HERE ----
return
% HINT: the basic implementation for the max,min and median functions is
% the same, so once yolu have one going the other two should be
% straightforward. I suggest you use a for loop structure similar to the one
% you would have used for the earlier noise filter.
% ----------------------------------------------------------------------
% MEDIAN FILTER
function Imed = med_filter(I,n)
halfn = floor(n/2);
Imed=zeros(size(I));
% ---- ADD YOUR CODE HERE ----
[rows, cols]=size(I);
% --- YOUR CODE TO GO HERE ---
for i=1+halfn :rows-halfn
for j=1+halfn:cols-halfn
window=I(i-halfn:i+halfn,j-halfn:j+halfn);
window_reshaped=reshape(window,1,n^2);
Imed(i,j)=median(window_reshaped);
end
end
%Imed = rand(size(I)); % <-- dummy random noise result
% ---- ADD YOUR CODE HERE ----
return
% ----------------------------------------------------------------------
% MAX FILTER - generate an image containing the max value over the nxn
% neighbourhood around each point in the image.
function Imax = max_filter(I,n)
halfn = floor(n/2);
Imax=zeros(size(I));
% ---- ADD YOUR CODE HERE ----
[rows, cols]=size(I);
% --- YOUR CODE TO GO HERE ---
for i=1+halfn :rows-halfn
for j=1+halfn:cols-halfn
window=I(i-halfn:i+halfn,j-halfn:j+halfn);
window_reshaped=reshape(window,1,n^2);
Imax(i,j)=max(window_reshaped);
end
end
%Imax = rand(size(I)); % <-- dummy random noise result
% ---- ADD YOUR CODE HERE ----
return
% ----------------------------------------------------------------------
% MIN FILTER- generate an image containing the min value over the nxn
% neighbourhood around each point in the image.
function Imin = min_filter(I,n)
halfn = floor(n/2);
Imin=zeros(size(I));
% ---- ADD YOUR CODE HERE ----
[rows, cols]=size(I);
% --- YOUR CODE TO GO HERE ---
for i=1+halfn :rows-halfn
for j=1+halfn:cols-halfn
window=I(i-halfn:i+halfn,j-halfn:j+halfn);
window_reshaped=reshape(window,1,n^2);
Imin(i,j)=min(window_reshaped);
end
end
%Imin = rand(size(I)); % <-- dummy random noise result
% ---- ADD YOUR CODE HERE ----
return
% ----------------------------------------------------------------------
A1_code/adap_med_filt.m
% Adaptive median filter. Adaptively use up to 7x7 median filtering to
% remove salt and pepper noise from an image
%
function Iamf = adap_med_filt(I)
if (isa(I,'uint8'))
I=double(I(:,:,1))/255;
end
Iamf=I;
% find the nxn local maxima and minima images (required for your solution)
Imax = max_filter(I,3);
Imin = min_filter(I,3);
% compute 3x3, 5x5 and 7x7 median images (required for your solution)
Imed3 = med_filter(I,3);
Imed5 = med_filter(I,5);
Imed7 = med_filter(I,7);
% ---- ADD YOUR CODE HERE ----
% SEE Assignment sheet for algorithm. In short if point is same as local max
% or min use med3x3, if still max or min use med5x5 else med7x7.
[rows,cols]=size(I);
for i=4:rows-4
for j=4:cols-4
if Iamf(i,j)==Imax(i,j) || Iamf(i,j)==Imin(i,j)
if Imed3(i,j)==Imax(i,j) || Imed3(i,j)==Imin(i,j)
if Imed5(i,j)==Imax(i,j) || Imed5(i,j)==Imin(i,j)
if Imed7(i,j)==Imax(i,j) || Imed7(i,j)==Imin(i,j)
Iamf(i,j)=I(i,j);
else
Iamf(i,j)=Imed7(i,j);
end
else
Iamf(i,j)=Imed5(i,j);
end
else
Iamf(i,j)=Imed3(i,j);
end
else
Iamf(i,j)=I(i,j);
end
end
end
%Iamf = rand(size(I)); % <-- dummy random noise result
% ---- ADD YOUR CODE HERE ----
return
% HINT: the basic implementation for the max,min and median functions is
% the same, so once yolu have one going the other two should be
% straightforward. I suggest you use a for loop structure similar to the one
% you would have used for the earlier noise filter.
%...