Azzera filtri
Azzera filtri

Error in Program - array exceed

1 visualizzazione (ultimi 30 giorni)
DP
DP il 1 Nov 2019
Risposto: Walter Roberson il 1 Nov 2019
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 Commento
Walter Roberson
Walter Roberson il 1 Nov 2019
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?

Accedi per commentare.

Risposte (1)

Walter Roberson
Walter Roberson il 1 Nov 2019
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

Categorie

Scopri di più su Image Processing Toolbox in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by