MATLAB: I have a 512x512x3 grey scale image. i want to count the number of pixel with intensities ranging between 140 – 200 and 0 – 30. how can i do that

image analysisImage Processing Toolbox

i converted the image initially using the rbg2gray command. following that i created a mask. What command should i use to count all values in the mask that have non 0 values?
FirstImage = imread('image1.jpeg');
A = rgb2gray (FirstImage);
mask = A > 140 & A < 200;

Best Answer

You can use nnz() - the function to count the number of non-zeros function in an array:
numberOfNonZeros = nnz(A);
Or you can use sum():
numberOfNonZeros = sum(A(:));