How to apply a mask only on saturation in a hsv image

조회 수: 4 (최근 30일)
gaetano mallardo
gaetano mallardo 2019년 2월 3일
답변: DGM 2022년 12월 1일
I have an rgb image converted n hsv. I need just the saturation level (sIm)
hsvIm = rgb2hsv(im); % Convert RGB to HSV
hIm = hsvIm(:,:,1); % Get the hue
sIm = hsvIm(:,:,2); % Get the saturation
vIm = hsvIm(:,:,3); % Get the value
Now i would need to apply a threshold on the sIm and i am trying using this function which i created modifying once which I generated by image toolbox
function [BW,maskedhsvImage] = satMask(sIm)
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 0.404;
I = sIm;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max);
BW = sliderBW;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 1])) = 0;
end
The probem is that when i call the function i have this error.
% Index in position 3 exceeds array bounds (must not exceed 1).
% Error in satMask (line 23)
% sliderBW = (I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max);
Where i am wrong?

답변 (1개)

DGM
DGM 2022년 12월 1일
It's not at all clear what the goals even were, but considering that other people are looking at this question and upvoting it, I guess someone has an idea.
I'm going to omit the broken junk that used to be an exported function from ColorThresholder. If you want to rewrite it as a function, that's up to you.
RGBimage = imread('peppers.png');
HSVimage = rgb2hsv(RGBimage);
% Define thresholds
Srange = [0.000 0.7];
% Create mask
mask = (HSVimage(:,:,2) >= Srange(1)) & (HSVimage(:,:,2) <= Srange(2));
% Do you want to ...?
% Set background S to 0
maskedS = HSVimage(:,:,2);
maskedS(~mask) = 0;
outpict1 = HSVimage;
outpict1(:,:,2) = maskedS;
outpict1 = im2uint8(hsv2rgb(outpict1));
% Set background of all HSV channels to 0
outpict2 = HSVimage;
outpict2(repmat(~mask,[1 1 3])) = 0;
outpict2 = im2uint8(hsv2rgb(outpict2));
% Set background of parent RGB image to 0
outpict3 = RGBimage;
outpict3(repmat(~mask,[1 1 3])) = 0;
% the results
OP = [outpict1 outpict2 outpict3];
imshow(OP)
You'll have to decide what you think it means to "apply a mask" and specifically, what it means to "apply a mask only on saturation".
See also:

카테고리

Help CenterFile Exchange에서 Image Segmentation and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by