Orange thershold rgb image hsv space
3 views (last 30 days)
Show older comments
I wrote this code to detect orange and green objects from a rgb image and to have
- orange objects = white,
- green objects = black and
- the rest greysh,
but I have
- orange objects = gray,
- green objects = white and
- the rest gray. What is wrong in my code?
I attached the original image also.
rgbImage = imread('fullfilename');
[rows, columns, numberOfColorChannels] = size(rgbImage);
[greenMask, maskedRGBImageG] = createGreenMask(rgbImage);
[orangeMask, maskedRGBImageR] = createOrangeMask(rgbImage);
grayMask = true(size(greenMask)) & ~greenMask & ~orangeMask;
output = uint8(255 * greenMask + 70 * grayMask);
%subplot(5, 7, 12);
imshow(output);
title('G-O');
function [BW,maskedRGBImage] = createOrangeMask(RGB)
I = rgb2hsv(RGB);
channel1Min = 0.120;
channel1Max = 0.061;
channel2Min = 0.000;
channel2Max = 1.000;
channel3Min = 0.000;
channel3Max = 1.000;
sliderBW = ((I(:,:,1) >= channel1Min) | (I(:,:,1) <= channel1Max)) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
maskedRGBImage = RGB;
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
imshow(maskedRGBImage);
end
function [BW,maskedRGBImage] = createGreenMask(RGB)
I = rgb2hsv(RGB);
channel1Min = 0.145;
channel1Max = 0.491;
channel2Min = 0.333;
channel2Max = 1.000;
channel3Min = 0.000;
channel3Max = 0.969;
sliderBW = (I(:,:,1) >= channel1Min) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
maskedRGBImage = RGB;
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
0 Comments
Accepted Answer
Image Analyst
on 7 Nov 2021
Too many errors to describe them all. Mostly your equation was wrong, and your creatOrangeMask was wrong. Corrected code:
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 11;
rgbImage = imread('assortimento-fev_mobile.jpeg');
[rows, columns, numberOfColorChannels] = size(rgbImage);
subplot(3, 3, 1);
imshow(rgbImage);
title('Input', 'fontSize', fontSize);
[greenMask, maskedRGBImageG] = createGreenMask(rgbImage);
subplot(3, 3, 2);
imshow(greenMask);
title('Green Mask', 'fontSize', fontSize);
subplot(3, 3, 3);
imshow(maskedRGBImageG);
title('Green Mask', 'fontSize', fontSize);
[orangeMask, maskedRGBImageO] = createOrangeMask(rgbImage);
subplot(3, 3, 5);
imshow(orangeMask);
title('Orange Mask', 'fontSize', fontSize);
subplot(3, 3, 6);
imshow(maskedRGBImageO);
title('Orange Mask', 'fontSize', fontSize);
grayMask = true(size(greenMask)) & ~greenMask & ~orangeMask;
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
maskedGrayImage = bsxfun(@times, rgbImage, cast(grayMask, 'like', rgbImage));
subplot(3, 3, 8);
imshow(grayMask);
title('Gray Mask', 'fontSize', fontSize);
subplot(3, 3, 9);
imshow(maskedGrayImage);
title('Gray Mask', 'fontSize', fontSize);
output = uint8(255 * orangeMask + 128 * grayMask);
subplot(3, 3, 7);
imshow(output);
caption = sprintf('Output: orange = white.\nGreen = black.\nThe Rest = gray');
title(caption, fontSize);
%=============================================================================================
function [BW,maskedRGBImage] = createOrangeMask(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 07-Nov-2021
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.041;
channel1Max = 0.133;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.426;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 1.000;
% 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;
% 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
%=============================================================================================
function [BW,maskedRGBImage] = createGreenMask(RGB)
I = rgb2hsv(RGB);
channel1Min = 0.145;
channel1Max = 0.491;
channel2Min = 0.333;
channel2Max = 1.000;
channel3Min = 0.000;
channel3Max = 0.969;
sliderBW = (I(:,:,1) >= channel1Min) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
maskedRGBImage = RGB;
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
More Answers (0)
See Also
Categories
Find more on Color 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!