How to get luminance of an image?

13 views (last 30 days)
AK
AK on 17 Jun 2021
Answered: Image Analyst on 19 Jun 2021
Hello,
I have a directory of images and I want to group and save the images by luminance. However, I'm not sure how to calculate the luminance of an image. How would i go about this?
Thank you!

Accepted Answer

Image Analyst
Image Analyst on 19 Jun 2021
Code samples in the FAQ:
So, try this if you have PNG files, otherwise change the extension to whatever you want.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
% Specify the folder where the files live.
myFolder = pwd; % or 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.png'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray = imread(fullFileName);
imshow(imageArray); % Display image.
caption = sprintf('#%d of %d : %s', k, length(theFiles), theFiles(k).name);
title(caption, 'FontSize', fontSize);
drawnow; % Force display to update immediately.
[rows, columns, numberOfColorChannels] = size(imageArray);
if numberOfColorChannels == 3
theMeans(k) = mean2(rgb2gray(imageArray));
else
theMeans(k) = mean2(imageArray);
end
end
cla reset;
bar(theMeans);
grid on;
title('Image Means', 'FontSize', fontSize);
xlabel('Image Number', 'FontSize', fontSize);
ylabel('Mean Gray Level', 'FontSize', fontSize);
fprintf('Done! Thanks Image Analyst!\n');

More Answers (1)

Jonas
Jonas on 19 Jun 2021
concerning luminance have a look into this post
do you also need help in writing a loop for image loading/analysis?

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!