Replacing pixel value in a loop

15 visualizaciones (últimos 30 días)
minehaha
minehaha el 19 de Feb. de 2019
Comentada: Rik el 19 de Feb. de 2019
Hi everyone, Im trying to replace all pixels in rbg image, where the sum of r,g,b components is lower or equal 10 with zero value ([r,b,g]=[0,0,0]) .
If not I want to keep the value from MyImage.
Here is my attempt ...unfortunately not working
MyImage=imread('003264663547_60.jpeg');
RGBsum=(sum(MyImage,3));
NewImage = zeros(size(MyImage));
[a b c]= size(NewImage);
[row column depth]=size(MyImage);
for l=1:depth
for i=1:row
for j=1:column,
if RGBsum <= 10;
NewImage(a,b,c)=0;
else
NewImage(a,b,c)=MyImage(row,column,depth);
end
end
end
end
imshow(NewImage);

Respuesta aceptada

Rik
Rik el 19 de Feb. de 2019
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.
%Load image
MyImage=imread('003264663547_60.jpeg');
%Copy image
NewImage = MyImage;
%Overflow doesn't matter here, because we will test <=10, so conversion to
%double before summing is not needed
RGBsum=sum(MyImage,3);
%Create a logical mask and replicate to 3D
L=repmat(RGBsum<=10,1,1,size(NewImage,3));
%Use the mask to set pixels to zero
NewImage(L)=0;
  2 comentarios
minehaha
minehaha el 19 de Feb. de 2019
Thank you so much! Thinking the image as a loop looked easier, although didnt work.. there is a long way with Matlab in front of me :) thanks again
Rik
Rik el 19 de Feb. de 2019
A loop often is write and understand, but almost never a fast solution. It takes experience to see where you can use a matrix operation instead of loops, but a general rule of thumb is that you shouldn't nest your loops over a single array.
You can also use the profiler to find bottlenecks in your code to see what is actually worth the trouble of optimizing. You might also find interesting and helpful hints on this page.

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