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

조회 수: 34(최근 30일)
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
% 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.
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 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.
HSVimage = rgb2hsv(RGBimage);
% Define thresholds
Srange = [0.000 0.7];
mask = (HSVimage(:,:,2) >= Srange(1)) & (HSVimage(:,:,2) <= Srange(2));
% Do you want to ...?
% Set background S to 0
outpict1 = HSVimage;
outpict1 = im2uint8(hsv2rgb(outpict1));
% Set background of all HSV channels to 0
outpict2 = HSVimage;
outpict2 = im2uint8(hsv2rgb(outpict2));
% Set background of parent RGB image to 0
outpict3 = RGBimage;
% 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".

댓글을 달려면 로그인하십시오.

### 범주

Find more on Histograms in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by