MATLAB: Can someone tell me how to find the summation of all the pixel values in an image

abssum

I'm trying to implement a Sum Average Difference (SAD) based code to find the difference between consecutive frames in a video to eliminate those frames which do not contain activity i.e. no motion.

Best Answer

Assuming the frames you want to compare are called img_prev and img_next, both matrices of the same size, the following function computes what you want:
function out = sad(img_prev, img_next)
% First, take the absolute value of the difference at each pixel
myAbsDiff = abs(img_prev - img_next);
% Then, sum over all pixels
out = sum(myAbsDiff(:));
end
Using (:) you can transform matrix myAbsDiff into a vector. Calling sum on a vector computes the sum of all elements in a vector.