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

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

采纳的回答

Gopichandh Danala
Gopichandh Danala 2017-6-20
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Computer Vision with Simulink 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by