MATLAB: Calculating black pixels in colour image

blackcolourimagepixels

Hi, does anyone know how do I calculate black pixels in a colour image using Matlab? Thank you.

Best Answer

Find pixels that are black in all three color channels. One way to do it is:
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1) == 0;
greenChannel = rgbImage(:, :, 2) == 0;
blueChannel = rgbImage(:, :, 3) == 0;
blackPixelImage = redChannel & greenChannel & blueChannel;
numBlackPixels = sum(blackPixelImage(:));
message = sprintf('The number of pure black pixels = %d', numBlackPixels);
uiwait(helpdlg(message));