The output of the function is unrecognized in the function
1 view (last 30 days)
Show older comments
Write a function to "smooth" a black and white image by replacing each pixel by the average of itself and its neighbors. In MATLAB a black and white image is just a matrix of 1s and 0s - 1 represents white, and 0 represents black. To keep the algorithm simple, ignore the edges of the image - that is, do not change the first and last rows and columns.
function name = smooth_image
input argument = input matrix
output argument = output matrix
the algorithm can be described as follows:
- Given a NxM input matrix
- Make a copy of the matrix - this will be the output matrix
- loop over rows 2 to N-1 on the input matrix
- loop over columns 2 to M-1 of the input matrix
- take the average of the 3x3 submatrix centered on the current row & column
- set the corresponding element of the output matri equal to this average
This is what I have so far: (but it keeps returning "unrecognized function or variable 'output'. Error in smooth_image (line 2). input = output;
function output = smooth_image(input)
input = output;
N = length(input(1,:));
M = length(input(:,1));
for i = 2:N-1
for j = 2:M-1
A = input(i,j);
A = mean(i-1:i+1,j-1:j+1);
output = A(i,j);
end
end
end
2 Comments
Walter Roberson
on 8 Feb 2020
Notice that every iteration you are overwriting ALL of A with a scalar value.
Answers (1)
Bhargavi Maganuru
on 11 Feb 2020
Error is because of assigning output of the function to a variable in the 1st line itself. Notice that you’re calculating the mean to the indices and not to the submatrix in the loop.
You can use following code
function output = smooth_image(input)
output = input;
N = length(input(1,:));
M = length(input(:,1));
for i = 2:N-1
for j = 2:M-1
A = mean2(input(i-1:i+1,j-1:j+1));
output(i,j) = A;
end
end
end
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!