필터 지우기
필터 지우기

having problem with error "index out of bounds" numel (Y)=1"

조회 수: 3 (최근 30일)
Joey
Joey 2014년 3월 6일
댓글: Image Analyst 2014년 3월 7일
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
Joey
Joey 2014년 3월 7일
Hey all, I ended up getting the script to do what I needed to do. Here is my final result (that works).
clc;
clear;
format compact;
% Image importing
fname = uigetfile('.bmp','Pick the file');
A = imread(fname);
[m,n,p] = size(A);
B = A;
figure(1)
image(A)
for k = [1:p];
for j = [1:n];
for i = [1:m];
Y = B(i,j,k);
if Y == 0 | Y == 255;
if i == 1 & j == 1; % top left corner
Z = [(B(i, j+1)),(B(i+1,j)),(B(i+1,j+1))];
B(i,j,k) = median(Z);
elseif i == 1 & j > 1 & j < n; %top side
Z = [(B(i, j-1)),(B(i,j+1)),(B(i+1,j-1)),(B(i+1,j)),(B(i+1,j+1))];
B(i,j,k) = median(Z);
elseif i == 1 & j == n; % top right corner
Z = [(B(i, j-1)),(B(i+1,j-1)),(B(i+1,j))];
B(i,j,k) = median(Z);
elseif i > 1 & i < m & j == 1; % left side
Z = [(B(i-1, j)),(B(i-1,j+1)),(B(i,j+1)),(B(i+1,j)),(B(i+1,j+1))];
B(i,j,k) = median(Z);
elseif i > 1 & i < m & j == n; % right side
Z = [(B(i-1, j-1)),(B(i-1,j)),(B(i,j-1)),(B(i+1,j-1)),(B(i+1,j))];
B(i,j,k) = median(Z);
elseif i == m & j == 1; %bottom left corner
Z = [(B(i-1, j)),(B(i-1,j+1)),(B(i,j+1))];
B(i,j,k) = median(Z);
elseif i == m & j > 1 & j < n; %bottom side
Z = [(B(i-1, j-1)),(B(i-1,j)),(B(i-1,j+1)),(B(i,j-1)),(B(i,j+1))];
B(i,j,k) = median(Z);
elseif i == m & j == n; %bottom right corner
Z = [(B(i-1, j-1)),(B(i-1,j)),(B(i,j-1))];
B(i,j,k) = median(Z);
elseif i > 1 & i < m & j > 1 & j < n; %every other position
Z = [(B(i-1, j-1)),(B(i-1,j)),(B(i-1,j+1)),(B(i,j-1)),(B(i,j+1)),(B(i,j-1)),(B(i,j+1)),(B(i+1,j-1)),(B(i+1,j)),(B(i+1,j+1))];
B(i,j,k) = median(Z);
end
end
end
end
end
figure(2)
image(B)
fnameo = uiputfile('.bmp','Insert desired output filename');
imwrite(B,fnameo);

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

채택된 답변

Image Analyst
Image Analyst 2014년 3월 6일
You're going overall rows and all columns but trying to access a 3x3 window around it. What will happen if you are at the edge of an image? You'd have i or j = -1. There is no -1 row. Try going from i = 1:rows, and j = 2:columns.
  댓글 수: 4
Joey
Joey 2014년 3월 6일
Thanks, I had never seen this function before. I'm in an entry level matlab class, and we're supposed to be using loops to accomplish the image cleaning. We're given both a black and white and a color image with "salt and pepper" noise and tasked with removing the noise and creating an image without the noise. When you say to use i=1:rows and j=2:columns, where should I enter that? I apologize, I am very new to matlab and this is the first project I have ever done with it, so I am not great with syntax yet.

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

추가 답변 (1개)

Iain
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.

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by