Batch image processing loop

2 visualizzazioni (ultimi 30 giorni)
Owen Lane
Owen Lane il 12 Mar 2021
Commentato: Owen Lane il 15 Mar 2021
I would like the system to read every image in the folder, which i does correctly but only processes the baseFile. How do i get it to run the below function on every image as it reads it?
%%Load image
rgb = imread(fullFileName);
%%Load image
Folder = 'C:\Users\Owen\Desktop\UNIVERSITY\Year 3\SEM2\KV6003 Induvidual Projects\MATLAB code';
if ~isfolder(Folder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', Folder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(Folder, '*.jpg');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(Folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
imshow(imageArray); % Display image.
%%Find red points
redPoints = rgb(:,:,1)>=100 & rgb(:,:,2)<=149 & rgb(:,:,3)<=140;
percentRed = 100*(sum(sum(redPoints))/(size(rgb,1)*size(rgb,2)));
fprintf('Image has %d red pixels\n',sum(sum(redPoints)))
fprintf('Image is %.2f percent red\n',percentRed)
%%Highlight red on image
rgbRed = uint8(cat(3,redPoints,redPoints,redPoints)).*rgb;
figure(2)
imshow(rgbRed)
%Find green points
greenPoints = rgb(:,:,1)<=104 & rgb(:,:,2)>=0 & rgb(:,:,3)<=100;
percentGreen = 100*(sum(sum(greenPoints))/(size(rgb,1)*size(rgb,2)));
fprintf('Image has %d green pixels\n',sum(sum(greenPoints)))
fprintf('Image is %.2f percent green\n',percentGreen)
%Highlight green on image
rgbGreen = uint8(cat(3,greenPoints,greenPoints,greenPoints)).*rgb;
figure(3)
imshow(rgbGreen)
%Find blue points
bluePoints = rgb(:,:,1)<=111 & rgb(:,:,2)<=112 & rgb(:,:,3)>=0;
percentBlue = 100*(sum(sum(bluePoints))/(size(rgb,1)*size(rgb,2)));
fprintf('Image has %d blue pixels\n',sum(sum(bluePoints)))
fprintf('Image is %.2f percent blue\n',percentBlue)
%Highligh blue on image
rgbBlue = uint8(cat(3,bluePoints,bluePoints,bluePoints)).*rgb;
figure(4)
imshow(rgbBlue)
end

Risposta accettata

Aghamarsh Varanasi
Aghamarsh Varanasi il 15 Mar 2021
Modificato: Aghamarsh Varanasi il 15 Mar 2021
Hi,
The image that you are reading using 'imread' in the for loop is assigned to a variable 'imageArray', whereas the operations are done on a variable named 'rgb'. Renaming the variable 'imageArray' to 'rgb' would solve the issue.
Forth line in the for loop would be,
...
for k = 1:length(jpegFiles)
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(Folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
rgb = imread(fullFileName);
imshow(rgb); % Display image.
...
  1 Commento
Owen Lane
Owen Lane il 15 Mar 2021
Hi,
Thank you for your feedback.
I have tested this and it has worked perfectly. It now identifies and processes each image in the folder.
Thank you again

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by