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

조회 수: 28 (최근 30일)
Teshan Rezel
Teshan Rezel . 2020년 7월 14일
댓글: Image Analyst . 2020년 7월 15일
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:
img = imread('25_04_2018_102553.jpg');
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);
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);
maskedImage = img;
maskedImage(repmat(~sliderBW,[1 1 3])) = 0;

채택된 답변

Image Analyst
Image Analyst 2020년 7월 14일
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;
rgbImage = imread('peppers.png');
subplot(3, 2, 1);
imshow(rgbImage, []);
title('Color Image', 'FontSize', fontSize);
greyImage = rgb2gray(rgbImage);
subplot(3, 2, 2);
title('Gray Scale Image', 'FontSize', fontSize);
[counts, ~] = imhist(greyImage, 255);
T = otsuthresh(counts);
mask = imbinarize(greyImage, T);
mask = bwareaopen(mask, 3000);
mask = imfill(mask, 'holes');
mask = bwperim(mask);
mask = imdilate(mask, ones(5));
mask = imerode(mask, ones(3));
mask = imfill(mask, 'holes');
subplot(3, 2, 3);
title('Mask Image', 'FontSize', fontSize);
% 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.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
subplot(3, 2, 4);
title('Masked RGB Image', 'FontSize', fontSize);
% Convert the masked RGB image to HSV color space.
hsvImage = rgb2hsv(maskedRgbImage);
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);
title('MaskH Image', 'FontSize', fontSize);
fprintf('Done running %s.m ...\n', mfilename);
  댓글 수: 2
Image Analyst
Image Analyst 2020년 7월 15일
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.

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

추가 답변 (0개)




Community Treasure Hunt

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

Start Hunting!

Translated by