lighthouse.jpg imread into matrix and calculate mean and std? - Homework
24 views (last 30 days)
Show older comments
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?
1 Comment
Accepted Answer
Image Analyst
on 19 Nov 2013
Edited: Image Analyst
on 16 Nov 2024
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);
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]
[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);
More Answers (2)
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
on 16 Nov 2024
can you help me with ghow to get the code for the lighthouse pic and also l,a,b code and pic?
9 Comments
DIMITRIOS
on 20 Dec 2024 at 13:49
hello can you help me out with the following: i will need a MATLAB code regarding lighthouse image to make it grey and the need the gaussian, and salt and pepper matlab codes, also we are asked for the PSNR on all three images: original grey, gaussian, salt and pepper image.
Can you help me out? Please
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!