Image 组合面具
我正在尝试获取一个图像,其中除了几个彩色对象外,其他所有对象都变灰,如下所示: 我的原始图像是这样的(瓶盖的颜色与上面的示例略有不同): 我尝试应用阈值处理,然后对图像进行二值化,结果如下(掩码在左侧,乘法结果在右侧):Image 组合面具,image,matlab,image-processing,mask,image-thresholding,Image,Matlab,Image Processing,Mask,Image Thresholding,我正在尝试获取一个图像,其中除了几个彩色对象外,其他所有对象都变灰,如下所示: 我的原始图像是这样的(瓶盖的颜色与上面的示例略有不同): 我尝试应用阈值处理,然后对图像进行二值化,结果如下(掩码在左侧,乘法结果在右侧): 现在我试着把所有的面具组合起来。我应该使用if循环将其组合成单个图像,还是有更好的方法?我尝试使用(&,&,&),但它变成了黑色图像。您的原始图像有7个不同的区域:5个彩色提示、手和背景。接下来的问题是,我们如何忽略墙壁和手,这两个区域恰好是最大的两个区域,而只保留尖端的
现在我试着把所有的面具组合起来。我应该使用
if
循环将其组合成单个图像,还是有更好的方法?我尝试使用(&,&,&)
,但它变成了黑色图像。您的原始图像有7个不同的区域:5个彩色提示、手和背景。接下来的问题是,我们如何忽略墙壁和手,这两个区域恰好是最大的两个区域,而只保留尖端的颜色
如果您的MATLAB许可证允许,我建议使用(colorThresholder
),它允许您在图像中找到颜色的合适表示形式。我可以说,L*a*b*
颜色空间允许区域/颜色之间的良好分离:
然后我们可以导出此函数,得到以下结果:
function [BW,maskedRGBImage] = createMask(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 25-Dec-2018
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2lab(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.040;
channel1Max = 88.466;
% Define thresholds for channel 2 based on histogram settings
channel2Min = -4.428;
channel2Max = 26.417;
% Define thresholds for channel 3 based on histogram settings
channel3Min = -12.019;
channel3Max = 38.908;
% 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;
% 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;
end
这将产生:
您的原始图像有7个不同的区域:5个彩色提示、手和背景。接下来的问题是,我们如何忽略墙壁和手,这两个区域恰好是最大的两个区域,而只保留尖端的颜色 如果您的MATLAB许可证允许,我建议使用(
colorThresholder
),它允许您在图像中找到颜色的合适表示形式。我可以说,L*a*b*
颜色空间允许区域/颜色之间的良好分离:
然后我们可以导出此函数,得到以下结果:
function [BW,maskedRGBImage] = createMask(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 25-Dec-2018
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2lab(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.040;
channel1Max = 88.466;
% Define thresholds for channel 2 based on histogram settings
channel2Min = -4.428;
channel2Max = 26.417;
% Define thresholds for channel 3 based on histogram settings
channel3Min = -12.019;
channel3Max = 38.908;
% 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;
% 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;
end
这将产生:
要组合掩码,请使用
|
(元素逻辑OR),而不是和(逻辑AND)
要组合掩码,请使用|
(元素逻辑OR),而不是&
(逻辑AND)
您应该始终描述您正在尝试做什么(为什么也有帮助),而不是直接跳到代码。请注意,在这种情况下,您应该使用|
。@Dev-iL感谢您的编辑,我将花更多的时间格式化我的问题。您应该始终描述您正在尝试做什么(为什么也有帮助),而不是直接跳到代码。请注意,在这种情况下,您应该使用|
。@Dev-iL感谢您的编辑,我将花更多时间格式化我的问题。
mask = mask1 | mask2 | mask3;