How to get luminance of an image?

46 次查看(过去 30 天)
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 中查找有关 Image Segmentation and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by