how to store rgb value of multiple images in matlab?

16 views (last 30 days)
I have 208 images in the folder I want the RGB values of those images present in the folder and to store them ? i was trying with cell function but not able to use properly if someone has better solution?
  2 Comments
Image Analyst
Image Analyst on 13 Aug 2021
@varuna watwe, you should easily be able to adapt my code below. If you can't, then post your attempt in a new question (not here).

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 19 Oct 2015
cell() is not the right function. When you say you want "RGB values of those images", what do you mean by that? Do you want the mean RGB values of each image? Because with an image of say, a megapixel, you will have a million RGB values in your array (after you call imread). So you already have ALL the RGB values, but did you want any particular or special RGB values, like those in some region of the image?
To process all 208 images, see the code in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F. And make sure you don't use image for the name of any of your variables.
  2 Comments
Image Analyst
Image Analyst on 27 Dec 2018
Raman:
Try this:
% User wants this segmentation:
% R>95&G>40&B>20 & max{R,G,B}-min{R,G,B}>15 & |R-G|>20 & R>G & R>B
% 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', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern)
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()
rgbImage = imread(fullFileName);
if ndims(rgbImage) < 3
continue; % Skip gray scale and indexed images.
end
subplot(1, 2, 1);
imshow(rgbImage); % Display image.
caption = sprintf('Original: %s', baseFileName);
title(caption, 'FontSize', 20, 'Interpreter', 'none');
% Extract the individual red, green, and blue color channels.
R = rgbImage(:, :, 1);
G = rgbImage(:, :, 2);
B = rgbImage(:, :, 3);
% Create the mask image.
maxMinusMinImage = max(rgbImage, [], 3) - min(rgbImage, [], 3);
mask = R>95 & G>40 & B>20 & maxMinusMinImage > 15 & ...
abs(double(R-G)) > 20 & (R > G) & (R>B);
subplot(1, 2, 2);
imshow(mask); % Display image.
caption = sprintf('Mask for: %s', baseFileName);
title(caption, 'FontSize', 20, 'Interpreter', 'none');
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.1, 1, 0.9]);
drawnow; % Force display to update immediately.
end

Sign in to comment.

More Answers (1)

Martin Schätz
Martin Schätz on 19 Oct 2015
Hi, you can put each of your images (array [sizex,sizey,3]) in a cell. It is created with {}. So if you have some loop where you load images, you can do it like this:
for i=1:N
images{i}=imread(image);
end
  3 Comments
Image Analyst
Image Analyst on 27 Dec 2018
Not really useful. No need to put into a cell array at all. See better code below:
% User wants this segmentation:
% R>95&G>40&B>20 & max{R,G,B}-min{R,G,B}>15 & |R-G|>20 & R>G & R>B
% 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', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern)
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()
rgbImage = imread(fullFileName);
if ndims(rgbImage) < 3
continue; % Skip gray scale and indexed images.
end
subplot(1, 2, 1);
imshow(rgbImage); % Display image.
caption = sprintf('Original: %s', baseFileName);
title(caption, 'FontSize', 20, 'Interpreter', 'none');
% Extract the individual red, green, and blue color channels.
R = rgbImage(:, :, 1);
G = rgbImage(:, :, 2);
B = rgbImage(:, :, 3);
% Create the mask image.
maxMinusMinImage = max(rgbImage, [], 3) - min(rgbImage, [], 3);
mask = R>95 & G>40 & B>20 & maxMinusMinImage > 15 & ...
abs(double(R-G)) > 20 & (R > G) & (R>B);
subplot(1, 2, 2);
imshow(mask); % Display image.
caption = sprintf('Mask for: %s', baseFileName);
title(caption, 'FontSize', 20, 'Interpreter', 'none');
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.1, 1, 0.9]);
drawnow; % Force display to update immediately.
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!