How to make this code in loop - Image processing

6 ビュー (過去 30 日間)
YEMY
YEMY 2020 年 11 月 2 日
コメント済み: YEMY 2020 年 11 月 3 日
Hello, I have to process in loop many drone images.
I succeeded to get a script that works, but I could not make it work in loop to process the whole images in the folder.
I need to save the mask 2 as a JPG image after each loop. For opening multiple images in one folder, that I know how to do it. for the rest I don't know how to make a loop.
close all, clear all
cd 'D:\TEST\1-1000_0.35ms_200625\';
im = imread('DJI_0204.jpg');
lab = rgb2lab(im);
ab = lab(:,:,2:3);
ab = im2single(ab);
nColors = 2;
% repeat the clustering 3 times to avoid local minima
pixel_labels = imsegkmeans(ab,nColors,'NumAttempts',2);
imshow(pixel_labels,[])
%%
mask1 = pixel_labels==1;
cluster1 = im .* uint8(mask1);
imshow(cluster1)
title('Objects in Cluster 1');
%%
mask2 = pixel_labels==2;
cluster2 = im .* uint8(mask2);
imshow(cluster2)
title('Objects in Cluster 2');
imwrite(cluster2, 'mask_test1.jpg')

回答 (1 件)

Sudhakar Shinde
Sudhakar Shinde 2020 年 11 月 3 日
Loop to read images:
my_folder =pwd; %Folder path where images are present
filenames=dir(fullfile(my_folder,'*.jpg'));
Image = {};
for n = 1:numel(filenames)
fullname=fullfile(my_folder,filenames(n).name);
Image{end+1} = imread(fullname);
%Use your code here from converting to lab colors
%....
% ...
%
end
You can use your code in above for loop .
  1 件のコメント
YEMY
YEMY 2020 年 11 月 3 日
I didn't have problems with importing files. I had issues with making for loops to automate the processing. By the way I think I managed to do it after several tries. Thank you anyway.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by