How can I do summation pixel by pixel?

Below is my code
im = imread('test.png');
imshow(im);
[rows columns] = size(im);
numberOfPixels = rows*columns;
r_t = 0;
Red = im(:,:,1);
pixel = 0;
for i=1:rows
for j=1:columns
red = impixel(Red,i,j);
if red == 255
r_t = r_t + 0;
else
r_t = r_t + red;
end
end
end
when i type impixel(Red,1,1)
I think the answer should be just 255, but command window display 255 255 255

 Accepted Answer

Matt J
Matt J on 21 Sep 2018
Edited: Matt J on 21 Sep 2018
Red=im(:,:,1);
rt=sum( Red(Red~=255) , 'double')

7 Comments

thanks alot, if I want exclude white pixel value of the image, then it should be like
Red=im(:,:,1);
Green=im(:,:,2);
Blue=im(:,:,3)
rt=sum( (Red(Red~=255) && Green(Green~=255) && Blue(Blue~=255)) , 'double')
?
No, it should be
notwhite = ~( Red==255&Green==255&Blue==255);
rt=sum( Red(notwhite) ,'double') ;
this is for the whole image?
Matt J
Matt J on 21 Sep 2018
Edited: Matt J on 21 Sep 2018
It is the sum of all red channel values in pixels that are not completely white.
is it possible i compare the pixel value of R channel, G channel and B channel at the same time and set a condition where R==255 && G==255 && B==255 then only i sum up the pixel value
That's what we did.
thank you very much

Sign in to comment.

More Answers (0)

Asked:

on 21 Sep 2018

Commented:

on 21 Sep 2018

Community Treasure Hunt

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

Start Hunting!