Extract RGB values of multiple images

2 views (last 30 days)
Hello,
I have 300 images that I would like to extract the rgb values of each image individually. How can I do that?
Thanks

Accepted Answer

Image Analyst
Image Analyst on 9 Dec 2022
Use imread to get the RGB values of each image
for k = 1 : 300
filename = whatever
rgbValues = imread(filename);
end
  7 Comments
DGM
DGM on 13 Dec 2022
I don't know what other program has problems separating color channels, but there are a lot of things I don't know. If all you truly need to do is split the images and save the image channels independently, then I don't see why you can't do that in the loop. You'll just have to use imwrite() to write each channel to an appropriate filename and directory.
I will point out that it would be a generally bad idea to use JPG as the output file format. Use PNG if data integrity is of any concern.
Image Analyst
Image Analyst on 13 Dec 2022
Then you'll need to use imwrite instead of stuffing them all into a cell array, which your other program won't understand.
myFolder = '......./Pictures/Fine - 1';
filePattern = fullfile(myFolder, '*.jpg');
theFiles = dir(filePattern);
n = length(theFiles);
R = cell(n,1);
G = cell(n,1);
B = cell(n,1);
for k = 1 : n
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
rgbImage = imread(fullFileName);
[r, g, b] = imsplit(rgbImage);
% Get file parts
[folder, baseFileNameNoExt, ext] = fileparts(fullFileName);
% Save images to disk for the other program that needs them.
% Save red channel.
outputBaseFileName = sprintf('%s_R.png', baseFileNameNoExt);
outputFullFileName = fullfile(folder, outputBaseFileName);
imwrite(r, outputFullFileName);
% Save green channel.
outputBaseFileName = sprintf('%s_G.png', baseFileNameNoExt);
outputFullFileName = fullfile(folder, outputBaseFileName);
imwrite(g, outputFullFileName);
% Save blue channel.
outputBaseFileName = sprintf('%s_R.png', baseFileNameNoExt);
outputFullFileName = fullfile(folder, outputBaseFileName);
imwrite(b, outputFullFileName);
end

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing and Computer Vision in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!