# How can I display only one channel of an HSV image?

82 views (last 30 days)
Teshan Rezel on 14 Jul 2020
Commented: Image Analyst on 15 Jul 2020
Hi folks, is there a way to threshold out a single channel from an image in the HSV colour space? I am trying to mask it over the original image but to no avail. Below is my code, taken straight from the colour thresholding app, but it only produces a black image while the app produces an HSV image for the same function:
imgGrey = rgb2gray(img);
[counts, ~] = imhist(imgGrey, 255);
T = otsuthresh(counts);
BW = imbinarize(imgGrey, T);
BW = bwareaopen(BW, 3000);
BW = imfill(BW, 'holes');
BW = bwperim(BW);
BW = imdilate(BW, ones(5));
BW = imerode(BW, ones(3));
BW = imfill(BW, 'holes');
img(~BW(:,:,[1 1 1])) = 0;
baseHSV = rgb2hsv(img);
H=baseHSV(:,:,1);
S=baseHSV(:,:,2);
V=baseHSV(:,:,3);
channel1Min = 0.768;
channel1Max = 0.868;
channel2Min = 0.586;
channel2Max = 0.720;
channel3Min = 0.769;
channel3Max = 0.876;
sliderBW = ...
(H >= channel1Min ) & (H <= channel1Max) & ...
(S >= channel2Min ) & (S <= channel2Max) & ...
(V >= channel3Min ) & (V <= channel3Max);
imshow(img);

Image Analyst on 14 Jul 2020
I really have no idea what you're wanting to do. Especially with this line:
maskH = img.*repmat(uint8(H),[1 1 3]); % What the heck is this!?!?
What's that all about? You already masked the RGB image with a thresholded version of its gray scale version. Then you're wanting to mask it or overlay it or something with the hue channel, but I can't figure it out because what you did makes little to no sense. Anyway, let's start with this improved code and then finish it once you explain better what you want to accomplish in creating the maskH image.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 15;
subplot(3, 2, 1);
imshow(rgbImage, []);
title('Color Image', 'FontSize', fontSize);
greyImage = rgb2gray(rgbImage);
subplot(3, 2, 2);
imshow(greyImage);
title('Gray Scale Image', 'FontSize', fontSize);
[counts, ~] = imhist(greyImage, 255);
T = otsuthresh(counts);
subplot(3, 2, 3);
% Mask the RGB image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
subplot(3, 2, 4);
% Convert the masked RGB image to HSV color space.
hueChannel = hsvImage(:,:,1);
subplot(3, 2, 5);
imshow(hueChannel, []);
title('Hue Channel Image', 'FontSize', fontSize);
maskH = rgbImage .* repmat(uint8(hueChannel),[1 1 3]); % What the heck is this!?!?
subplot(3, 2, 6);
fprintf('Done running %s.m ...\n', mfilename);
##### 2 CommentsShowHide 1 older comment
Image Analyst on 15 Jul 2020
No problem. I would help more, but honestly I don't know what you intend to do, so I don't know how to do it for you.

R2019b

### Community Treasure Hunt

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

Start Hunting!