RGB Image subtracting mean intensity

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!

 Risposta accettata

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);

5 Commenti

Thank you, but can you please comment on the various steps of this solution so that I can understand it? Thanks!
I thought the comments should have done it already but if you want super verbose and explicit comments on every line, then see this:
% Read in image from disk and convert to double
% and to the range 0-1 so we can subtract the mean without clipping to 0.
rgbImage = im2double(imread('peppers.png'));
% Display the image in the left of a new figure window.
subplot(1, 2, 1); % Tell it to go on the left.
imshow(rgbImage); % Here is the actual display
% Give a title/caption above the image.
title('Original Image', 'FontSize', 20);
% Extract the individual red, green, and blue color channels.
% First get the red channel.
redChannel = rgbImage(:, :, 1);
% Next get the green channel.
greenChannel = rgbImage(:, :, 2);
% Finally get the blue channel.
blueChannel = rgbImage(:, :, 3);
% Find the means of each color channel.
% First get the mean of the red channel.
meanR = mean2(redChannel)
% Next get the mean of the green channel.
meanG = mean2(greenChannel)
% Next get the mean of the blue channel.
meanB = mean2(blueChannel)
% Now concatenate the individual color channels back into a single RGB image.
rgbImage = cat(3, redChannel - meanR, greenChannel - meanG, blueChannel - meanB);
% Display the new image in the right side of a new figure window.
subplot(1, 2, 2); % Tell it to go on the right.
imshow(rgbImage); % Here is the actual display
% Give a title/caption above the image.
title('Means Subtracted', 'FontSize', 20);
If it now does what you need, can you "Accept this answer" by clicking on the link?
No. When you subtract the mean from any distribution, you will get values both positive and negative of course. See code where I prove this below:
% Read in image from disk and convert to double
% and to the range 0-1 so we can subtract the mean without clipping to 0.
rgbImage = im2double(imread('peppers.png'));
% Display the image in the left of a new figure window.
subplot(1, 2, 1);
imshow(rgbImage);
% Give a title/caption above the image.
title('Original Image', 'FontSize', 20);
% Extract the individual red, green, and blue color channels.
% First get the red channel.
redChannel = rgbImage(:, :, 1);
% Next get the green channel.
greenChannel = rgbImage(:, :, 2);
% Finally get the blue channel.
blueChannel = rgbImage(:, :, 3);
% Find the means of each color channel.
% First get the mean of the red channel.
meanR = mean2(redChannel)
% Next get the mean of the green channel.
meanG = mean2(greenChannel)
% Next get the mean of the blue channel.
meanB = mean2(blueChannel)
% Get the max and min of the difference channels.
newR = redChannel - meanR; % Create a new image of the difference.
% Print out the min value in the red difference channel.
fprintf('The min of "redChannel - meanR" is %f.\n', min(newR(:)));
% Print out the max value in the red difference channel.
fprintf('The max of "redChannel - meanR" is %f.\n', max(newR(:)));
% Get the max and min of the difference channels.
newG = greenChannel - meanG; % Create a new image of the difference.
% Print out the min value in the green difference channel.
fprintf('The min of "greenChannel - meanG" is %f.\n', min(newG(:)));
% Print out the min value in the green difference channel.
fprintf('The max of "greenChannel - meanG" is %f.\n', max(newG(:)));
% Get the max and min of the difference channels.
newB = blueChannel - meanB; % Create a new image of the difference.
% Print out the min value in the blue difference channel.
fprintf('The min of "blueChannel - meanB" is %f.\n', min(newB(:)));
% Print out the max value in the blue difference channel.
fprintf('The max of "blueChannel - meanB" is %f.\n', max(newB(:)));
% Now concatenate the individual color channels back into a single RGB image.
rgbImage = cat(3, newR, newG, newB);
% Display the new image in the right side of a new figure window.
subplot(1, 2, 2);
imshow(rgbImage);
% Give a title/caption above the image.
title('Means Subtracted', 'FontSize', 20);
I called im2double() so that imshow() could show the image. To allow it to have both positive and negative values I had to conver tot double. However, if an image is double, imshow() will only show values between 0 and 1. So leaving it in the original range would have been okay if you just wanted to do math on the image, but to display it, everything above 1 would be white and everything below 0 would be black. By using im2double, at least the positive differences can be displayed. Unfortunately, the negative differences are clipped to 0 on display, though not clipped in the variable. If you want to see the negative pixels not as black, you'll have to change the imshow to this:
imshow((rgbImage+1)/2, []);
Hi! I would like to imshow a color channel defined by these equation:
128*(((greenChannel-redChannel)/(greenChannel+redChannel))+1)
Can someone please help me? Thanks.
Try using ./ instead of / and assigning it to a variable then using imshow with brackets:
newImage = 128*(((greenChannel-redChannel) ./ (greenChannel+redChannel))+1);
imshow(newImage, []);

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Programming in Centro assistenza e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by