필터 지우기
필터 지우기

Error in Program - array exceed

조회 수: 1 (최근 30일)
DP
DP 2019년 11월 1일
답변: Walter Roberson 2019년 11월 1일
When I run this program this error showing me that me array exceeds
This Error showing in my program.... Please Help me to slove this error
ERROR===>
Index in position 2 exceeds array bounds (must not exceed 508).
Error in RegGrowSeg (line 21)
if (RegionMap(I, J) == 0 && ImgValue(I, J) <= Threshold)
Error in MAIN (line 37)
ImgSeg = RegGrowSeg (Image, Image_SD, 8, 0.02);
>>
MAIN.M =====>
clc;
close all;
%% Read Input Image - peppers_gray
Oimage=imread('peppers_gray.tif');
[M,N]=size(Oimage);
Image=Oimage(:,1:N/2);
figure(1);imshow(Image);
title('Input Image - Peppers','Color','red');
%%
[m, n] = size (OImage);
for I = 1:m-2
for J = 1:n-2
% Calculate average value
Avg = 0;
Sum = 0;
for K = 0:2
for L = 0:2
Sum = Sum + Image(I+K, J+L);
end
end
% Calculate Standard deviation
Avg = Sum / 9;
Sum = 0;
for K = 0:2
for L = 0:2
Sum = Sum + (Image(I+K, J+L) - Avg)^2;
end
end
Image_SD(I, J) = sqrt(double(Sum/8));
end
end
ImgSeg = RegGrowSeg (Image, Image_SD, 8, 0.02);
figure (2);
imshow (ImgSeg)
RegGrowSeg====>
function ImgSeg = RegGrowSeg (ImgOri, ImgValue, Connect, Threshold)%segamentation fucntion with region growing method
Region = 0;
RegionMap = zeros(size(ImgOri));
[m, n] = size (ImgOri);
if (Connect ~= 4 && Connect ~= 8)
return;
end
if (Connect == 4)
Nbr = [-1 0; 0 -1;0 1; 1 0];
NbrSize = 4;
end
if (Connect == 8)
Nbr = [-1 -1; -1 0; -1 1; 0 -1; 0 1; 1 -1; 1 0; 1 1];
NbrSize = 8;
end
% Choose one pixel which does not be include to any region yet and it's value less than threshold
for I = 1:m;
for J = 1:n;
if (RegionMap(I, J) == 0 && ImgValue(I, J) <= Threshold)
Queue = [I J];
Region = Region+1;
RegionMap(I, J) = Region;
% Start Growing
while ~isempty(Queue)
X = Queue(1, 1);
Y = Queue(1, 2);
Queue(1, :) = [];
if (ImgValue(X, Y) <= Threshold)
%push neighborhood
for K = 1:NbrSize;
Nx = X + Nbr(K, 1);
Ny = Y + Nbr(K, 2);
if (Nx > 0 && Nx< m+1 && Ny > 0 && Ny < n+1)
if (RegionMap(Nx, Ny) == 0)
RegionMap(Nx, Ny) = Region;
Queue = [Queue ; [Nx Ny]];
end
end
end
end
end
end
end
end
ImgSeg = RegionMap/Region;
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 11월 1일
Oimage=imread('peppers_gray.tif');
Are you sure that peppers_gray.tif is a grayscale image, and not an RGB image that happens to include only gray tones?

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

답변 (1개)

Walter Roberson
Walter Roberson 2019년 11월 1일
Image=Oimage(:,1:N/2);
So Image is only half as wide as Oimage
[m, n] = size (OImage);
So n runs to the full width of OImage
for I = 1:m-2
for J = 1:n-2
J can be as large as two columns less than Oimage
Sum = Sum + Image(I+K, J+L);
but you use J (plus up to 2) to index Image, which is only half as wide as OImage

카테고리

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