Error in Program - array exceed

1 次查看(过去 30 天)
DP
DP 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