Filter löschen
Filter löschen

How to ensure only first image is used in a function???

1 Ansicht (letzte 30 Tage)
Ellis Berry
Ellis Berry am 25 Mai 2016
Beantwortet: Guillaume am 25 Mai 2016
So I have this short bit of code which produces a binary image based on thresholds I have selected. I want to choose a file ( theFile = uigetdir Infolder = dir(theFile)... for example) and then run the threshold on all the images that are in that file.
BUT, before I do this I want to use the 'roipoly' function to select a region of interest. I want to use the first image only, to use roipoly on, choose the area of interest, save the vertex values and then crop every other image to these values I have found.
how do I ensure that the roipoly function is used for the first image only and not for every image in the loop?
Here is my code so far that if I put in a loop, would make me use roipoly for EVERY image in the file:
[BW,xi,yi] = roipoly(RGB);
xMin = min(xi);
xMax = max(xi);
yMin = min(yi);
yMax = max(yi);
newRGB = imcrop(RGB,[xMin yMin xMax-xMin yMax-yMin]);
% Convert RGB image to chosen color space
I = rgb2hsv(newRGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.053;
channel1Max = 0.083;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.116;
channel2Max = 0.130;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.608;
channel3Max = 0.643;
% Create mask based on chosen histogram thresholds
BW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
% Invert mask
BW = ~BW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;

Antworten (1)

Guillaume
Guillaume am 25 Mai 2016
You simply put a condition in your loop so that it only asks for the roi on the first iteration:
%... get list of files any way you want, e.g.
files = dir(fullfile(somefolder, '*.png'));
for fileidx = 1 : numel(files);
%load file any way you want e.g.
rgb = imread(fullfile(somefolder, files(fileidx).name));
%only ask for the roi on 1st iteration
if fileidx == 1
[BW, xi, yi] = roipoly(RGB);
xMin = min(xi);
xMax = max(xi);
yMin = min(yi);
yMax = max(yi);
end
%on other iterations, it simply reuses the xi and yi of previous loop
%continue with code as normal
newRGB = imcrop(RGB,[xMin yMin xMax-xMin yMax-yMin]);
%...
end

Community Treasure Hunt

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

Start Hunting!

Translated by