# Write a matlab function, image processing

8 views (last 30 days)
anonymous on 25 Nov 2022
Edited: DGM on 27 Nov 2022
how can i get negative of image
DGM on 26 Nov 2022
The original question was:
You wrote the function you wrote in the sets m={5,11,17,21,27,33,41} and n={5,11,17,21,27,33,41}.MATLAB command that displays all(m,n)pairs in figure windows on the screen file (script).

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)
% 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?
immse(Af1,Af2)
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.
Bonus questions:
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?
DGM on 27 Nov 2022
Edited: DGM on 27 Nov 2022
Yes, it's unremarkable that the assignment teaches a number of bad ideas. I didn't post my answer for the purpose of respecting the assignment. Anyone else is free to do OP's homework in a manner as poorly instructive as what the assignment requests.
Bear mind the ridiculousness of this entire thread. It's a garbage zero-effort homework question that John closed. I reopened it just to throw shade on the assignment itself. OP then deleted the homework question and replaced it with a filler about inverting an image, at which point I figured I'd at least preserve continuity for future readers by providing links to answer OP's fake replacement question as well. Considering they deleted their own question already, OP and the details of their needs aren't even a part of this conversation anymore.

Image Analyst on 26 Nov 2022
Obviously we can't give you the full solution because you're not allowed to turn in our code as your own.

Image Analyst on 26 Nov 2022
For a uint8 image to invert it
negImage = 255 - grayImage;
which is how I'd do it but it look like your homework wanted you to use for loops.

### Categories

Find more on Image Arithmetic in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!