MATLAB Answers

How to automate insert many images at a time to find colour moment for RGB colour channel , instead of insert image one by one?

6 views (last 30 days)
nickson niss
nickson niss on 20 Oct 2017
Commented: Aishwarya Belekar on 23 Apr 2019
Here are the files involved to find colour moment for RGB colour channel of images.
function colorMoments = colorMoments(image)
% input: image to be analyzed and extract 2 first moments from each R,G,B
% output: 1x6 vector containing the 2 first color momenst from each R,G,B
% channel
% extract color channels
R = double(image(:, :, 1));
G = double(image(:, :, 2));
B = double(image(:, :, 3));
% compute 2 first color moments from each channel
meanR = mean( R(:) );
stdR = std( R(:) );
meanG = mean( G(:) );
stdG = std( G(:) );
meanB = mean( B(:) );
stdB = std( B(:) );
% construct output vector
%colorMoments = zeros(1, 6);
%colorMoments(1, :) = [meanR stdR meanG stdG meanB stdB];
colorMoments = [meanR stdR meanG stdG meanB stdB];
% clear workspace
%clear('R', 'G', 'B', 'meanR', 'stdR', 'meanG', 'stdG', 'meanB', 'stdB');
colorMoments = colorMoments(A);

Answers (2)

KSSV on 20 Oct 2017
images = dir('*.jpg') ; % give your image extension here
N = length(images) ; % total number of images
colorMoments = cell(N,1) ;
% loop for each image
for i = 1:N
colorMoments = colorMoments(A);


Show 2 older comments
nickson niss
nickson niss on 23 Oct 2017
The value of N is 100. 100 images in DB_Mango folder are named as 1.jpg, 2.jpg,3.jpg,..100.jpg. Error stated in matlab is in line 7.. "colorMoments =colorMoments(A)"
KSSV on 23 Oct 2017
Run this now:
images = dir('*.jpg') ; % give your image extension here
N = length(images) ; % total number of images
colorMoments = cell(N,1) ;
% loop for each image
for i = 1:N
CM = colorMoments(A);

Sign in to comment.

Image Analyst
Image Analyst on 24 Oct 2017
It doesn't really make sense to analyze "many images at a time". You need to analyze images "one by one" because they're in different image files. So there's no way around it. Sooner or later you're going to have to use imread() to open one image file at a time. You can do that in a loop to process all the files in the folder. Inf the loop, you can store the 6 results in an N-by-6 array if you want:
% Specify the folder where the files live.
myFolder = 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
% 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);
allColorMoments = zeros(length(theFiles), 6); % Preallocate.
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, 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);
theseColorMoments = colorMoments(imageArray); % Process image.
allColorMoments(k, :) = theseColorMoments; % Store these results in our array for all of them.
DO NOT use image as the name of a variable, like you did. image() is the name of an important built-in function that you don't want to override.
Also, I don't know if you know the definition of moments you want to use. You're doing intensity moments - it's all based on intensity. There are other image moments that take into account location - kind of like moments of inertia. See attached demo, and this link: So your moments do not depend on the spatial arrangement of the pixels at all. You could rearrange the image into a ramp, or into essentially a white noise/scrambled looking image and the moments would be exactly the same since they only depends on intensity, not how the image looks. However spatial moments will give different values depending on the arrangement of pixels. For example a centered Gaussian would give a lower moment than if you took all the bright pixels and moved them into a ring around the edge of the image. Same pixels, different locations, and different moments. Versus same pixels, different locations, same moments like you're getting now with your formulas. I don't know which you want, or even if you knew there was a difference.