フィルターのクリア

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

1 回表示 (過去 30 日間)
Ellis Berry
Ellis Berry 2016 年 5 月 25 日
回答済み: Guillaume 2016 年 5 月 25 日
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;

回答 (1 件)

Guillaume
Guillaume 2016 年 5 月 25 日
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

カテゴリ

Help Center および File ExchangeImage Filtering and Enhancement についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by