# how compute the pixel sum for image?

69 views (last 30 days)
houssem rouine on 11 Apr 2018
Commented: Walter Roberson on 28 Oct 2019
Hello freinds, I am a beginner in matlab and i want know how compute the pixel sum for image
##### 2 CommentsShowHide 1 older comment
houssem rouine on 11 Apr 2018
thank you so much for your helping.

Pawel Jastrzebski on 11 Apr 2018
1. Use imread - output to load the image into Matlab and store it as a matrix - it can be anything between 2d and 4D matrix
2. Dimension 1 and 2 of the matrix are width and height of the image in pixels
% load the image into the matrix
dims = size(A)
NoOfPixels = prod(dims)
% Note if 'dims' is bigger than 2 elements then:
% NoOfPixels = prod(dims(1:2))
##### 2 CommentsShowHide 1 older comment
Walter Roberson on 28 Oct 2019
No, in that code, NoOfPixels is the number of array elements in the image. It is not necessarily the number of pixels: the number of pixels would be dims(1)*dims(2) . The difference comes about for RGB, in which there are 3 array elements per pixel.
If you are looking for the sum of the pixels, you need to define what that means for RGB.
sum(A, [1 2]) %since R2019a