lighthouse.jpg imread into matrix and calculate mean and std? - Homework

28 views (last 30 days)
Attached it the lighthouse.jpg. I am trying to use imread to load this picture onto a matrix. Then I am trying to calculate and print the mean separately of the red, green, and blue components in the matrix and also the standard deviation for each. I am having trouble getting the mean and std to work.
This is my script.
im1 = imread('Lighthouse.jpg'); % READ IMAGE
imshow(im1); % SHOW IMAGE
size(im1); % SIZE BY [ROW COLUM COLORMATRIX]
% COLORMATRIX USUALLY 3 WHICH IS FOR
% RGB, AND SHOWS 3 MATRIXES OF
% [ROW COLUM] DIMENTIONS.
imr = im1(:,:,1); % SHOW TRUE COLOR IMAGING FOR RGB IN SUBPLOTS
img = im1(:,:,2);
imb = im1(:,:,3);
immr = mean(mean(imr));
immg = mean(mean(img));
immb = mean(mean(imb));
imsr = std(std(imr));
imsg = std(std(img));
imsb = std(std(imb));
subplot(3,3,1);
imshow(im1);
subplot(3,3,2);
imshow(immr);
subplot(3,3,3);
imshow(immg);
subplot(3,3,4);
imshow(immb);
subplot(3,3,5);
What happens to be the problem?

Accepted Answer

Image Analyst
Image Analyst on 19 Nov 2013
Edited: Image Analyst on 16 Nov 2024 at 22:54
The standard deviation of the standard deviation of the columns is not the standard deviation of the entire image! It's not like the max, min, and mean functions. Try a simple example and you'll see. Just do
%imsr = std(imr(:));
to get the standard deviation of an entire array (a single color channel). imr(:) converts the 2D array into a 1D array.
% Demo by Image Analyst.
% Initialization steps.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
Beginning to run LiveEditorEvaluationHelperEeditorId.m ...
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
% Read in original image.
rgbImage = imread('lighthouse.png'); % READ IMAGE
[rows, columns, numberOfColorChannels] = size(rgbImage) % SIZE IS [ROWS COLUMNS NUMCOLORs]
rows = 640
columns = 480
numberOfColorChannels = 3
[redChannel, greenChannel, blueChannel] = imsplit(rgbImage);
meanRedValue = mean(redChannel, 'all');
meanGreenValue = mean(greenChannel, 'all');
meanBlueValue = mean(blueChannel, 'all');
sdRed = std(double(redChannel(:)));
sdGreen = std(double(greenChannel(:)));
sdBlue = std(double(blueChannel(:)));
subplot(2, 2, 1);
imshow(rgbImage);
title('Original Image');
subplot(2, 2, 2);
imshow(redChannel);
caption = sprintf('Red Channel.\nMean = %.2f, SD = %.2f', meanRedValue, sdRed);
title(caption);
subplot(2, 2,3);
imshow(greenChannel);
caption = sprintf('Green Channel.\nMean = %.2f, SD = %.2f', meanGreenValue, sdGreen);
title(caption);
subplot(2, 2,4);
imshow(blueChannel);
caption = sprintf('Blue Channel.\nMean = %.2f, SD = %.2f', meanBlueValue, sdBlue);
title(caption);
  6 Comments
DGM
DGM on 17 Nov 2024 at 0:28
Edited: DGM on 17 Nov 2024 at 0:31
inpict = imread('lighthouse.png');
inpict = reshape(inpict,[],size(inpict,3));
channelwisemean = mean(inpict,1)
channelwisemean = 1×3
106.4334 150.0902 171.3984
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
channelwisestd = std(double(inpict),0,1)
channelwisestd = 1×3
70.6482 66.4553 87.9043
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 19 Nov 2013
The mean and standard deviations are scalar values; you would not show a scalar value as an image. You might fprintf() the values out on a single line (or two) though.

Dimitrios
Dimitrios on 16 Nov 2024 at 19:21
can you help me with ghow to get the code for the lighthouse pic and also l,a,b code and pic?
  5 Comments
Image Analyst
Image Analyst on 17 Nov 2024 at 15:20
@Dimitrios, @DGM pretty much already gave you the code. If you'd like a more fleshed out demo, then try this:
% Demo by Image Analyst.
% Initialization steps.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
Beginning to run LiveEditorEvaluationHelperEeditorId.m ...
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Read in original image.
rgbImage = imread('lighthouse.png'); % READ IMAGE
[rows, columns, numberOfColorChannels] = size(rgbImage) % SIZE IS [ROWS COLUMNS NUMCOLORs]
rows = 640
columns = 480
numberOfColorChannels = 3
[redChannel, greenChannel, blueChannel] = imsplit(rgbImage);
meanRedValue = mean(redChannel, 'all');
meanGreenValue = mean(mean(greenChannel));
meanBlueValue = mean(mean(blueChannel));
sdRed = std(double(redChannel(:)));
sdGreen = std(double(greenChannel(:)));
sdBlue = std(double(blueChannel(:)));
subplot(2, 2, 1);
imshow(rgbImage);
title('Original Image');
subplot(2, 2, 2);
imshow(redChannel);
impixelinfo; % Let user mouse around and see values displayed on the figure.
caption = sprintf('Red Channel.\nMean = %.2f, SD = %.2f', meanRedValue, sdRed);
title(caption);
subplot(2, 2, 3);
imshow(greenChannel);
impixelinfo; % Let user mouse around and see values displayed on the figure.
caption = sprintf('Green Channel.\nMean = %.2f, SD = %.2f', meanGreenValue, sdGreen);
title(caption);
subplot(2, 2, 4);
imshow(blueChannel);
impixelinfo; % Let user mouse around and see values displayed on the figure.
caption = sprintf('Blue Channel.\nMean = %.2f, SD = %.2f', meanBlueValue, sdBlue);
title(caption);
% Convert RGB image into LAB image.
labImage = rgb2lab(rgbImage);
[lImage, aImage, bImage] = imsplit(labImage);
% Display them
figure; % Open up a new window.
subplot(2, 2, 1);
imshow(rgbImage);
title('Original Image');
subplot(2, 2, 2);
imshow(lImage, []);
impixelinfo; % Let user mouse around and see values displayed on the figure.
title('L Image');
subplot(2, 2, 3);
imshow(aImage, []);
impixelinfo; % Let user mouse around and see values displayed on the figure.
title('A Image');
subplot(2, 2, 4);
imshow(bImage, []);
impixelinfo; % Let user mouse around and see values displayed on the figure.
title('B Image');
To learn other fundamental concepts, invest 2 hours of your time here:
DGM
DGM on 18 Nov 2024 at 5:10
@Dimitrios Repeating it doesn't clarify anything. You haven't answered any of the questions I've asked you, so there's nothing new to tell you other than the same guesses. If that's not what you want, you have to explain why.
To repeat:
There are multiple standard images of lighthouses. Which one? Does it matter? If it doesn't matter, why does it even need to be a lighthouse?
If you're asking for "the l, a and b", I have to assume you're talking about CIELAB. If you are, then we've both given you examples. That much might seem obvious, but arguably the most common standard "lighthouse picture" is grayscale, so it's plausible that the request for the LAB image doesn't make sense in context.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!