having problem with error "index out of bounds" numel (Y)=1"
이전 댓글 표시
Hello all! I am trying to write a code that takes the noise out of bitmap images, and re-writes them as a cleaned image afterward. I'm not done with the code entirely yet, but my function file keeps spitting out the error
Attempted to access Y(8,1); index out of bounds because numel(Y)=1.
Error in fixp (line 13)
A = [(Y((i-1), j)),(Y((i-1),(j+1))),(Y(i,(j+1))),(Y((i+1),j)),(Y((i+1),(j+1)))];
Here is what my main file looks like:
clc;
clear;
format compact;
% Image importing
A = imread('J20_pic1_noisy.bmp');
[m,n,p] = size(A);
for k = [1:p];
for j = [1:n];
for i = [1:m];
Y = A(i,j,k);
if Y == 0 | Y == 255;
[B] = fixp(Y,i,j,m,n);
end
end
end
end
and here is what the function file looks like:
function[B] = fixp(Y,i,j,m,n);
B = [];
if i == 1 & j == 1 % top left corner
A = [(Y(i, j+1)),(Y(i+1,j)),(Y(i+1,j+1))];
B(i, j)= mean(median(A));
elseif i == 1 & j > 1 & j < n %top side
A = [(Y(i, j-1)),(Y(i,j+1)),(Y(i+1,j-1)),(Y(i+1,j)),(Y(i+1,j+1))];
B(i, j)= mean(median(A));
elseif i == 1 & j == n % top right corner
A = [(Y(i, j-1)),(Y(i+1,j-1)),(Y(i+1,j))];
B(i, j)= mean(median(A));
elseif i > 1 & i < m & j == 1 % left side
A = [(Y(i-1, j)),(Y(i-1,j+1)),(Y(i,j+1)),(Y(i+1,j)),(Y(i+1,j+1))];
B(i, j)= mean(median(A));
elseif i > 1 & i < m & j == n % right side
A = [(Y(i-1, j-1)),(Y(i-1,j)),(Y(i,j-1)),(Y(i+1,j-1)),(Y(i+1,j))];
B(i, j)= mean(median(A));
elseif i == m & j == 1 %bottom left corner
A = [(Y(i-1, j)),(Y(i-1,j+1)),(Y(i,j+1))];
B(i, j)= mean(median(A));
elseif i == m & j > 1 & j < n %bottom side
A = [(Y(i-1, j-1)),(Y(i-1,j)),(Y(i-1,j+1)),(Y(i,j-1)),(Y(i,j+1))];
B(i, j)= mean(median(A));
elseif i == m & j == n %bottom right corner
A = [(Y(i-1, j-1)),(Y(i-1,j)),(Y(i,j-1))];
B(i, j)= mean(median(A));
elseif i > 1 & i < m & j > 1 & j < n %every other position
A = [(Y(i-1, j-1)),(Y(i-1,j)),(Y(i-1,j+1)),(Y(i,j-1)),(Y(i,j+1)),(Y(i,j-1)),(Y(i,j+1)),(Y(i+1,j-1)),(Y(i+1,j)),(Y(i+1,j+1))];
B(i, j)= mean(median(A));
end
end
Does anyone know what is causing the error, and subsequently, how to fix it? Thank you!
채택된 답변
추가 답변 (1개)
Iain
2014년 3월 6일
The error is actually quite clear:
Before calling the fixp function, you're setting "Y" to be a scalar.
Inside the fixp function, you're treating Y, as if it is a 3x3 matrix.
If you put
Y = A(:,:,k), then it should work.
카테고리
도움말 센터 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!