The output of the function is unrecognized in the function

조회 수: 1 (최근 30일)
Elijah Sapp
Elijah Sapp 2020년 2월 7일
답변: Bhargavi Maganuru 2020년 2월 11일
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
per isakson
per isakson 2020년 2월 8일
Did you mean
output = input;
rather than
input = output;
?
Walter Roberson
Walter Roberson 2020년 2월 8일
Notice that every iteration you are overwriting ALL of A with a scalar value.

댓글을 달려면 로그인하십시오.

답변 (1개)

Bhargavi Maganuru
Bhargavi Maganuru 2020년 2월 11일
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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by