how to store rgb value of multiple images in matlab?
3 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
varuna watwe
2021 年 8 月 13 日
I have 20 images. I am successful in reading all images from folder containing them. What I want is, want RGB values of each image separately in different variables to process them further. Please help.
Image Analyst
2021 年 8 月 13 日
@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).
採用された回答
Image Analyst
2015 年 10 月 19 日
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 件のコメント
Image Analyst
2018 年 12 月 27 日
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
その他の回答 (1 件)
Martin Schätz
2015 年 10 月 19 日
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 件のコメント
Image Analyst
2018 年 12 月 27 日
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
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!