How to get luminance of an image?

조회 수: 19 (최근 30일)
AK
AK 2021년 6월 17일
답변: Image Analyst 2021년 6월 19일
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!

채택된 답변

Image Analyst
Image Analyst 2021년 6월 19일
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');

추가 답변 (1개)

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

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by