path_directory='tomato';
original_files=dir([path_directory '/*.jpg']);
for k=1:length(original_files)
filename=[path_directory '/' original_files(k).name];
image_ogi=imread(filename);
gray=rgb2gray(image_ogi);
gmag=imgradient(gray);
L=watershed(gmag);
Lrgb=label2rgb(L);
se=strel('disk',20);
Io=imopen(gray,se);
Ie=imerode(gray,se);
Iobr=imreconstruct(Ie,gray);
Iobrd=imdilate(Iobr,se);
Iobrcbr=imreconstruct(imcomplement(Iobrd),imcomplement(Iobr));
Iobrcbr=imcomplement(Iobrcbr);
fgm=imregionalmax(Iobrcbr);
I2=labeloverlay(gray,fgm);
se2=strel(ones(5,5));
fgm2=imclose(fgm,se2);
fgm3=imerode(fgm2,se2);
fgm4=bwareaopen(fgm3,20);
I3=labeloverlay(gray,fgm4);
bw=imbinarize(Iobrcbr);
D=bwdist(bw);
DL=watershed(D);
bgm=DL==0;
gmag2=imimposemin(gmag,bgm | fgm4);
L=watershed(gmag2);
labels=imdilate(L==0,ones(3,3))+2*bgm+3*fgm4;
I4=labeloverlay(gray,labels);
Lrgb=label2rgb(L,'jet','w','shuffle');
himage=imshow(Lrgb);
himage.AlphaData=0.3;
title('Colored Labels Superimposed Transparently on Original Image')
destination='C:\Users\TIET\Desktop\tomato_output\im';
imwrite(himage,[destination,num2str(k),'.jpg']);
end
Best Answer