I want to apply Marker-Controlled Watershed Segmentation on a folder containing .jpg images..So what modifications are required in the following code and I also want to store that results in a folder
3 views (last 30 days)
Show older comments
SHUBHDEEP KAUR
on 14 Sep 2018
Edited: KALYAN ACHARJYA
on 15 Sep 2018
path_directory='C:\Users\TIET\Desktop\tomato'; % 'Folder name' original_files=dir([path_directory '/*.jpg']); %Note on Image Format for k=1:length(original_files) filename=[path_directory '/' original_files(k).name]; image_ogi=imread(filename); gray=rgb2gray(image_ogi); destination='C:\Users\TIET\Desktop\tomato_output\im'; %Complete path of the folder imwrite(gray,[destination,num2str(k),'.jpg']); %Change the image formta as per desired image format end gmag = imgradient(gray); imshow(gmag,[]) title('Gradient Magnitude')
L = watershed(gmag); Lrgb = label2rgb(L); imshow(Lrgb) title('Watershed Transform of Gradient Magnitude')
se = strel('disk',20); Io = imopen(gray,se); imshow(Io) title('Opening')
Ie = imerode(gray,se); Iobr = imreconstruct(Ie,gray); imshow(Iobr) title('Opening-by-Reconstruction')
Ioc = imclose(Io,se); imshow(Ioc) title('Opening-Closing')
Iobrd = imdilate(Iobr,se); Iobrcbr = imreconstruct(imcomplement(Iobrd),imcomplement(Iobr)); Iobrcbr = imcomplement(Iobrcbr); imshow(Iobrcbr) title('Opening-Closing by Reconstruction')
fgm = imregionalmax(Iobrcbr); imshow(fgm) title('Regional Maxima of Opening-Closing by Reconstruction')
I2 = labeloverlay(gray,fgm); imshow(I2) title('Regional Maxima Superimposed on Original Image')
se2 = strel(ones(5,5)); fgm2 = imclose(fgm,se2); fgm3 = imerode(fgm2,se2);
fgm4 = bwareaopen(fgm3,20); I3 = labeloverlay(gray,fgm4); imshow(I3) title('Modified Regional Maxima Superimposed on Original Image')
bw = imbinarize(Iobrcbr); imshow(bw) title('Thresholded Opening-Closing by Reconstruction')
D = bwdist(bw); DL = watershed(D); bgm = DL == 0; imshow(bgm) title('Watershed Ridge Lines)') gmag2 = imimposemin(gmag, bgm | fgm4); L = watershed(gmag2);
labels = imdilate(L==0,ones(3,3)) + 2*bgm + 3*fgm4; I4 = labeloverlay(gray,labels); imshow(I4) title('Markers and Object Boundaries Superimposed on Original Image')
Lrgb = label2rgb(L,'jet','w','shuffle'); imshow(Lrgb) title('Colored Watershed Label Matrix')
figure imshow(gray) hold on himage = imshow(Lrgb); himage.AlphaData = 0.3; title('Colored Labels Superimposed Transparently on Original Image')
1 Comment
Accepted Answer
KALYAN ACHARJYA
on 15 Sep 2018
Edited: KALYAN ACHARJYA
on 15 Sep 2018
path_directory='tomato'; % 'Folder name'
original_files=dir([path_directory '/*.jpg']); %Note on Image Format
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']); %Change the image formta as per desired image format
end
2 Comments
KALYAN ACHARJYA
on 15 Sep 2018
Edited: KALYAN ACHARJYA
on 15 Sep 2018
@ImageAnalyst Yes sir,Thanks I didn't check the code inside.
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!