Borrar filtros
Borrar filtros

Code Troubleshooting for replacing pixels in original image and ending with "index exceeds matrix dimensions."

1 visualización (últimos 30 días)
My image size is 1600x1200 and I'm wondering why it keeps saying "index exceeds matrix dimensions." I want to fill the parts within the yellow triangle and red circles with gray. My image is attached.
I=imread('image2-1.jpg');
Ired=I(:,:,1);
imgray=rgb2gray(I);
BW1 = im2bw(Ired,0.9);
BW1=imfill(BW1,'holes');
figure,imshow(BW1);
c = 1;
I3 = zeros(1600, 1200);
while c <= 1600
d = 1;
while d <= 1200
I3(c, d) = BW1(c, d);
d = d+1;
if BW1(c,d)>0
I(c,d,1)=127;
I(c,d,2)=127;
I(c,d,3)=127;
end
end
c = c+1;
end
figure, imshow(I);

Respuesta aceptada

Gopichandh Danala
Gopichandh Danala el 20 de Jun. de 2017
I checked the attached image: It is not 1600 * 1200
>> I=imread('image2-1.jpg');
>> size(I)
ans =
385 513 3
I resized the image just in case if attaced image is wrong:
I = imresize(I, [1600 1200], 'bilinear');
>> size(I)
ans =
1600 1200 3
Your d-value in the while loop has to be at the end of the inner while loop:
while c <= 1600
d = 1;
while d <= 1200
I3(c, d) = BW1(c, d);
if BW1(c,d)>0
I(c,d,1)=127;
I(c,d,2)=127;
I(c,d,3)=127;
end
d = d+1;
end
c = c+1;
end
But I found few problems in the code..
>> max(BW1(:))
ans =
logical
0
so, there are no values in the BW1 so your ' if condition' is never true and 'I' values doesn't get updated.
check your code where you are computing BW1 where you are expecting to find the marked regions
  2 comentarios
Megan Wang
Megan Wang el 20 de Jun. de 2017
Yeah I actually figured it out a few hours ago but you were right in that the "d = d+1" part was in the wrong place. Thanks!

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by