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
  댓글 수: 2
Megan Wang
Megan Wang 2017년 6월 20일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (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