Write a matlab function, image processing
8 views (last 30 days)
DGM on 26 Nov 2022
Edited: DGM on 26 Nov 2022
There are countless sliding window filter examples here. I'm not going to write another.
I don't have much patience for assignments that avoid opportunities to teach basic lessons. It's a dead-simple box averaging filter, but you're instructed (implicitly, I admit) to do the division and inversion inside the loop, when both of those things should be outside the loop. The fact that this simplification can be done should be part of the lesson, because writing two loops isn't much of a lesson by itself.
% a grayscale image (uint8)
A = imread('cameraman.tif');
% filter setup
w = [11 11];
% a better way
% the division is done numel(fk) times
% the inversion is done numel(A) times
fk = ones(w)/prod(w);
Af1 = imcomplement(imfilter(A,fk));
% a dumb way that's still less dumb than the assignment
% the division is done numel(A) times
% the inversion is done numel(A)*numel(fk) times
f = @(x) sum(1-x,'all')/prod(w);
Af2 = nlfilter(im2double(A),w,f);
Af2 = im2uint8(Af2);
% show that they're the same
% so why would you do it the dumb way?
% and why would you reinvent the wheel to make the dumb way even harder?
I can't run this in the forum editor, since nlfilter() uses a dialog box. On my hardware, with these inputs, the second example takes well over 300x as long to process the image. It's not a small difference.
You probably are restricted from using imfilter() or conv2(). Even if you have to write it the tedious way with loops, you can still use this basic simplification. When you write your code or report, explain why you wrote it the way you did and show that it's equivalent.
and related threads.
In the first example, I inverted the output instead of inverting the input. Bear in mind that nlfilter() and imfilter() zero-pad the array by default. What would happen if I'd inverted the input instead of the output? If you had wanted to invert the input, what should the padding value be? If both cases had padded the array by edge replication, would the order of operations been an issue?
This looks like a homework problem. If you have any questions ask your instructor or read the link below to get started:
Obviously we can't give you the full solution because you're not allowed to turn in our code as your own.