MATLAB Answers

Selection of roi for multiple images

68 views (last 30 days)
swati mane
swati mane on 17 Dec 2019
Commented: swati mane on 19 Dec 2019
I have 50 images in one folder. For one image I can select roi by clicking the mouse but now i need automatic selection of roi for multiple images.How to do this ?

  0 Comments

Sign in to comment.

Accepted Answer

Rik
Rik on 17 Dec 2019
If you want to apply the same ROI to all images: export the ROI as a logical array.
If you want to select a different ROI for each image, generate the list of file names with dir() and loop through the elements.

  5 Comments

Show 2 older comments
swati mane
swati mane on 18 Dec 2019
To select roi , I have to click by mouse .I want the parallelogram shaped roi for my project.
Thanks in advance.
This code I am using for the selection of ROI.(numFiles=50)
%Selection of ROI
for m=1:1:numFiles;
a=imread(imgFiles(m).name);% read RGB image (frame no. 1)
imshow(a);
%Selection of ROI
[r c]=ginput(4);
BW=roipoly(a,r,c);
figure;imshow(BW);
ROIArea = bwarea(BW);
[R C]=size(BW);
for i=1:R
for j=1:C
if BW(i,j)==1
Out(i,j)=a(i,j);
else
Out(i,j)=0;
end
end
end
figure;imshow(Out,[]);title('Output Image');
Rik
Rik on 18 Dec 2019
The point is to create the ROI only once. Something like the code below should be easy to adapt to your needs.
numFiles=numel(imgFiles);
for m=1:numFiles
a=imread(imgFiles(m).name);% read RGB image (frame no. 1)
if m==1
%generate ROI for first image
figure(1),clf(1)
imshow(a);
%Selection of ROI
[r,c]=ginput(4);
ROI=roipoly(a,r,c);
%ROI is a 2D binary mask
ROI_RGB=repmat(ROI,1,1,3);
end
Out=a;
Out(~ROI_RGB)=0;%set all pixels outside the mask to 0
figure(m),clf(m)
imshow(Out,[])
title(sprintf('Output Image (%d)',m))
end

Sign in to comment.

More Answers (0)

Sign in to answer this question.