How do I fix error Index in position 2 exceeds array bounds?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello, I have an error on my code, it says: Index in position 2 exceeds array bounds.
Error in Contenedoressssssss (line 76)
x = min(E(:,1))
My code is as follow:
function det = segcolor(f)
%Función que selecciona contenedores.
%Utiliza segmentación de color y
%transformaciones morfológicas.
%f = im2double(imread('.\lena.jpg'));
f = im2double(imread('C:\Users\aaron\OneDrive\Escritorio\utp 2023\Segundo Semestre\Tópicos especiales de control\Tarea 1 contenedores\camaraNivelada.jpg'));
g = rgb2gray(f);
mask = roimouse1(f); %selecciona el contenedor.
red = immultiply(mask, f(:, :, 1));
green = immultiply(mask, f(:, :, 2));
blue = immultiply(mask, f(:, :, 3));
g = cat(3, red, green, blue);
figure, imshow(g)
[M, N, K] = size(g);
I = reshape(g, M*N, 3);
idx = find(mask);
I = double(I(idx, 1:3));
C = cov(I);% C es la covarianza
m= mean(I);% m es la media
d = diag(C);
sd = sqrt(d)'
sd0 = max(sd)+0.06;
%Realiza la segmentación en gray.
T = graythresh(g);
um = im2bw(g, T);
figure, imshow(um), title('ímagen umbralizada')
%Realiza la segmentación de color.
E25 = colorseg('euclidean', f, sd0, m);
figure, imshow(E25), title('región');
%define elementos estructurales.
se90 = strel('line', 3, 90);
se0 = strel('line', 3, 0);
seq = strel('square', 3);
seq5 = strel('square', 10);
%Realiza las transformaciones morfológicas.
%E25dill = imdilate(E25, [se90 se0]);
E25dill = imdilate(E25, seq);
figure, imshow(E25dill), title('región dilatada');
E25fill = imfill(E25dill, 'holes');
figure, imshow(E25fill), title('región rellena');
im = E25fill;
for k=1:10
im = imopen(im, seq5);
end
figure, imshow(im), title('contenedor open');
E25fill = im;
marker = immultiply(mask, E25fill);
mask = E25fill;
im = imreconstruct(marker, mask, 4);
figure, imshow(im), title('contenedor seleccionado');
for k=1:10
im = imclose(im, seq);
end
figure, imshow(im), title('contenedor close');
im = imdilate(im, seq);
%im = imopen(im, seq);
figure, imshow(im), title('contenedor delineado');
%Encuentra el contorno y dibuja sobre la imagen original.
[B, L] = bwboundaries(im);
figure, imshow(f),title('cámara nivelada'); ;
hold on;
%for k = 1:length(B)
% boundary = B{k};
%plot(boundary(:,2),boundary(:,1), 'r', 'LineWidth', 3)
%end
%L = bwlabel(L)
D = regionprops(L, 'area', 'extrema', 'majorAxisLength', 'minorAxisLength', 'boundingBox', 'convexhull', 'centroid', 'convexarea');
A = [D.Area]
A2 = [D.ConvexArea]
%A3 = mean(A, A2)
E = [D.Extrema]
MA = [D.MajorAxisLength]
MI = [D.MinorAxisLength]
x = min(E(:,1))
y1 = min(E(:,2))
w = A ./(MI)
v = [D.BoundingBox]
%H = [D.ConvexHull]
CE = [D.Centroid]
[r, c] = find(E == x)
ym = min(E(r(:),2))
if E(r(1),2) >= y1
v(3) = v(3)
end
y2 = CE(2) - MI./2
w2 = 3.35*(CE(1) - x)
y3 = CE(2) + MI./2
%[x2, x3] = find(H(:,2) >= y3)
rectangle('Position',[x,v(2),v(3),v(4)],...
'Curvature',[0,0],...
'LineWidth',2,'LineStyle','-','EdgeColor',[0 1 0])
plot(x,v(2),'marker','O','MarkerEdgeColor','k','MarkerFaceColor','k','MarkerSize',8)
plot(x,v(2), 'marker','*','MarkerEdgeColor','w')%Especifica el color.
plot(x,y3,'marker','O','MarkerEdgeColor','k','MarkerFaceColor','k','MarkerSize',8)
plot(x,y3, 'marker','*','MarkerEdgeColor','w')%Especifica el color.
plot(w2,v(2),'marker','O','MarkerEdgeColor','k','MarkerFaceColor','k','MarkerSize',8)
plot(w2,v(2), 'marker','*','MarkerEdgeColor','w')%Especifica el color.
plot(w2,y3,'marker','O','MarkerEdgeColor','k','MarkerFaceColor','k','MarkerSize',8)
plot(w2,y3, 'marker','*','MarkerEdgeColor','w')%Especifica el color.
%daspect([1,1,1])
end
Can someone please help me? Or can give me a hint in order to solve this crazy issue :(
2 comentarios
DGM
el 12 de Sept. de 2023
Editada: DGM
el 12 de Sept. de 2023
The error happens because there are no blobs in the label image L, thus the prop struct D is empty, and so are all the derived arrays.
I can guess what the missing functions are, but I can't guess what the missing image is or where you created the mask. If I had to guess, I'd try this:
f = im2double(imread('peppers.png'));
g = rgb2gray(f);
mask = roimouse1(f); %selecciona el contenedor.
red = immultiply(mask, f(:, :, 1));
green = immultiply(mask, f(:, :, 2));
blue = immultiply(mask, f(:, :, 3));
g = cat(3, red, green, blue);
figure, imshow(g)
[M, N, K] = size(g);
I = reshape(g, M*N, 3);
idx = find(mask);
I = double(I(idx, 1:3));
C = cov(I);% C es la covarianza
m = mean(I);% m es la media
d = diag(C);
sd = sqrt(d)'
sd0 = max(sd)+0.06;
%Realiza la segmentación en gray.
T = graythresh(g);
um = im2bw(g, T);
figure, imshow(um), title('ímagen umbralizada')
%Realiza la segmentación de color.
E25 = colorseg('euclidean', f, sd0, m);
figure, imshow(E25), title('región');
%define elementos estructurales.
se90 = strel('line', 3, 90);
se0 = strel('line', 3, 0);
seq = strel('square', 3);
seq5 = strel('square', 10);
%Realiza las transformaciones morfológicas.
%E25dill = imdilate(E25, [se90 se0]);
E25dill = imdilate(E25, seq);
figure, imshow(E25dill), title('región dilatada');
E25fill = imfill(E25dill, 'holes');
figure, imshow(E25fill), title('región rellena');
im = E25fill;
for k=1:10
im = imopen(im, seq5);
end
figure, imshow(im), title('contenedor open');
E25fill = im;
marker = immultiply(mask, E25fill);
mask = E25fill;
im = imreconstruct(marker, mask, 4);
figure, imshow(im), title('contenedor seleccionado');
for k=1:10
im = imclose(im, seq);
end
figure, imshow(im), title('contenedor close');
im = imdilate(im, seq);
%im = imopen(im, seq);
figure, imshow(im), title('contenedor delineado');
%Encuentra el contorno y dibuja sobre la imagen original.
[B, L] = bwboundaries(im);
figure, imshow(f),title('cámara nivelada'); ;
hold on;
%for k = 1:length(B)
% boundary = B{k};
%plot(boundary(:,2),boundary(:,1), 'r', 'LineWidth', 3)
%end
%L = bwlabel(L)
D = regionprops(L, 'area', 'extrema', 'majorAxisLength', 'minorAxisLength', 'boundingBox', 'convexhull', 'centroid', 'convexarea');
A = [D.Area]
A2 = [D.ConvexArea]
%A3 = mean(A, A2)
E = [D.Extrema]
MA = [D.MajorAxisLength]
MI = [D.MinorAxisLength]
x = min(E(:,1))
y1 = min(E(:,2))
w = A ./(MI)
v = [D.BoundingBox]
%H = [D.ConvexHull]
CE = [D.Centroid]
[r, c] = find(E == x)
ym = min(E(r(:),2))
if E(r(1),2) >= y1
v(3) = v(3)
end
y2 = CE(2) - MI./2
w2 = 3.35*(CE(1) - x)
y3 = CE(2) + MI./2
%[x2, x3] = find(H(:,2) >= y3)
rectangle('Position',[x,v(2),v(3),v(4)],...
'Curvature',[0,0],...
'LineWidth',2,'LineStyle','-','EdgeColor',[0 1 0])
plot(x,v(2),'marker','O','MarkerEdgeColor','k','MarkerFaceColor','k','MarkerSize',8)
plot(x,v(2), 'marker','*','MarkerEdgeColor','w')%Especifica el color.
plot(x,y3,'marker','O','MarkerEdgeColor','k','MarkerFaceColor','k','MarkerSize',8)
plot(x,y3, 'marker','*','MarkerEdgeColor','w')%Especifica el color.
plot(w2,v(2),'marker','O','MarkerEdgeColor','k','MarkerFaceColor','k','MarkerSize',8)
plot(w2,v(2), 'marker','*','MarkerEdgeColor','w')%Especifica el color.
plot(w2,y3,'marker','O','MarkerEdgeColor','k','MarkerFaceColor','k','MarkerSize',8)
plot(w2,y3, 'marker','*','MarkerEdgeColor','w')%Especifica el color.
%daspect([1,1,1])
function mask = roimouse1(inpict)
imshow(inpict);
ROI = drawrectangle(gca);
mask = createMask(ROI);
end
function mask = colorseg(~,inpict,dist,point);
% assuming both image and tuple are unit-scale float!
point = permute(point,[1 3 2]);
mask = sqrt(sum((inpict-point).^2,3)) <= dist;
% i'm only guessing that this is how the threshold was used
end
This creates no error, so either your missing functions are different, or the code is sensitive to your particular image/mask set.
Respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!