如何批处理从颜色阈值函数创建蒙版?
How can I batch process creating masks from the color thresholder function?
我使用颜色阈值应用程序创建了一个遮罩功能。我想通过函数 运行 多个图像而不是单独的每个图像。例如,'Control_1 Negative P001(30).png'、'Control_1 Positive P001(30).png' 等,直到 Control_41 个正负 .png 文件。我假设您会使用 for 循环,但我不确定从那里去哪里。
function [BW,maskedRGBImage] = GlassBrainMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 19-Feb-2021
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.002;
channel1Max = 0.119;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.322;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% 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;
end
contdir = dir('Control_*');
for i = 1:length(contdir)
RGB = % Control_* .png file
[BW Control_*, maskedControl_*Image] = GlassBrainMask(RGB)
end
要读取各个文件,dir()
函数返回的对象的 .name
property/member 可以与 imread()
函数结合使用。要存储 GlassBrainMask()
函数的输出,使用 struct()
。
Files = dir('Control_*');
Results_Structure = struct('BW',[],'maskedRGBImage',[]);
for i = 1:length(Files)
RGB = imread(Files(i).name);
[Output_BW, Output_maskedRGBImage] = GlassBrainMask(RGB);
Results_Structure(i).BW = Output_BW;
Results_Structure(i).maskedRGBImage = Output_maskedRGBImage;
end
%Showing the first result%
Image_Number = 1;
subplot(1,2,1); imshow(Results_Structure(Image_Number).BW);
subplot(1,2,2); imshow(Results_Structure(Image_Number).maskedRGBImage);
我使用颜色阈值应用程序创建了一个遮罩功能。我想通过函数 运行 多个图像而不是单独的每个图像。例如,'Control_1 Negative P001(30).png'、'Control_1 Positive P001(30).png' 等,直到 Control_41 个正负 .png 文件。我假设您会使用 for 循环,但我不确定从那里去哪里。
function [BW,maskedRGBImage] = GlassBrainMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 19-Feb-2021
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.002;
channel1Max = 0.119;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.322;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% 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;
end
contdir = dir('Control_*');
for i = 1:length(contdir)
RGB = % Control_* .png file
[BW Control_*, maskedControl_*Image] = GlassBrainMask(RGB)
end
要读取各个文件,dir()
函数返回的对象的 .name
property/member 可以与 imread()
函数结合使用。要存储 GlassBrainMask()
函数的输出,使用 struct()
。
Files = dir('Control_*');
Results_Structure = struct('BW',[],'maskedRGBImage',[]);
for i = 1:length(Files)
RGB = imread(Files(i).name);
[Output_BW, Output_maskedRGBImage] = GlassBrainMask(RGB);
Results_Structure(i).BW = Output_BW;
Results_Structure(i).maskedRGBImage = Output_maskedRGBImage;
end
%Showing the first result%
Image_Number = 1;
subplot(1,2,1); imshow(Results_Structure(Image_Number).BW);
subplot(1,2,2); imshow(Results_Structure(Image_Number).maskedRGBImage);