MATLAB: RGB Image subtracting mean intensity

image processingImage Processing Toolboxintensityrgb

Hi, so I just started using MATLAB, and I need to solve this exercise: "Given an RGB image, write a MATLAB code that, for each color channel, every pixel is modified by subtracting to its intensity the mean intensity value of the channel. ". Any help is very appreciated, thanks in advance!

Best Answer

Try this:
rgbImage = im2double(imread('peppers.png'));
subplot(1, 2, 1);
imshow(rgbImage);
title('Original Image', 'FontSize', 20);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
meanR = mean2(redChannel)
meanG = mean2(greenChannel)
meanB = mean2(blueChannel)
rgbImage = cat(3, redChannel - meanR, greenChannel - meanG, blueChannel - meanB);
subplot(1, 2, 2);
imshow(rgbImage);
title('Means Subtracted', 'FontSize', 20);