There are multiple issues with your code. The code analyzer already picks up 6 warnings, wich might hide a warning you shouldn't ignore. At the same time this is a case where mlint doesn't find all issues: you're not using your loop indices for anything. The content of you code is static.
You are using i j and l as loop indices, which are easy to misread as the imaginary unit, or the number 1, especially with the default font in Matlab.
You are comparing an entire array to 0, not just a single element. I never bothered to check how Matlab treats it, because you should decide that. If you are not using the any or all functions, you should not have an array in your if statement.
They code in the first block shows how you should have written your loop. But that is still not what you should do.
MyImage=imread('003264663547_60.jpeg');
RGBsum=sum(MyImage,3);
NewImage = zeros(size(MyImage));
[row,column,depth]=size(MyImage);
for d=1:depth
for r=1:row
for c=1:column
if RGBsum(r,c) <= 10
NewImage(r,c,d)=0;
else
NewImage(r,c,d)=MyImage(r,c,d);
end
end
end
end
imshow(NewImage);
You should use the power of Matlab: matrix operation. Let Matlab figure out how it should loop under the hood. You can further improve your code by using comments that explain what is happening, so you can understand it if you look at it in a few years time. You should also watch out for data type limitations, as images are generally read in uint8, which means all values greater than 255 or smaller than 0 are truncated, and non-integer values are rounded.
MyImage=imread('003264663547_60.jpeg');
NewImage = MyImage;
RGBsum=sum(MyImage,3);
L=repmat(RGBsum<=10,1,1,size(NewImage,3));
NewImage(L)=0;
Best Answer