how to define the red, green, and blue Threshold value?

% Demo macro to very, very simple color detection in RGB color space
% by ImageAnalyst
clc;
close all;
% Read standard MATLAB demo image.
rgbImage = imread('onion.png');
% Display the original image.
subplot(3, 4, 1);
imshow(rgbImage);
title('Original RGB Image');
% Maximize figure.
set(gcf, 'Position', get(0, 'ScreenSize'));
% Split the original image into color bands.
redBand = rgbImage(:,:, 1);
greenBand = rgbImage(:,:, 2);
blueBand = rgbImage(:,:, 3);
% Display them.
subplot(3, 4, 2);
imshow(redBand);
title('Red band');
subplot(3, 4, 3);
imshow(greenBand);
title('Green band');
subplot(3, 4, 4);
imshow(blueBand);
title('Blue Band');
% Threshold each color band.
redthreshold = 68;
greenThreshold = 70;
blueThreshold = 72;
redMask = (redBand > redthreshold);
greenMask = (greenBand < greenThreshold);
blueMask = (blueBand < blueThreshold);
% Display them.
subplot(3, 4, 6);
imshow(redMask, []);
title('Red Mask');
subplot(3, 4, 7);
imshow(greenMask, []);
title('Green Mask');
subplot(3, 4, 8);
imshow(blueMask, []);
title('Blue Mask');
% Combine the masks to find where all 3 are "true."
redObjectsMask = uint8(redMask & greenMask & blueMask);
subplot(3, 4, 9);
imshow(redObjectsMask, []);
title('Red Objects Mask');
maskedrgbImage = uint8(zeros(size(redObjectsMask))); % Initialize
maskedrgbImage(:,:,1) = rgbImage(:,:,1) .* redObjectsMask;
maskedrgbImage(:,:,2) = rgbImage(:,:,2) .* redObjectsMask;
maskedrgbImage(:,:,3) = rgbImage(:,:,3) .* redObjectsMask;
subplot(3, 4, 10);
imshow(maskedrgbImage);
title('Masked Original Image');
hi. i found above coding from imageAnalyst. my question : 1.) how can i define the red, green, and blue Threshold value?any formula to find the value?for example in my image i want to extract the gold color, and how can i know the exact pixel for gold in RGB colorspace? 2.) can someone explain to me how the coding above works?
tq so much

 Respuesta aceptada

Image Analyst
Image Analyst el 6 de Dic. de 2015
Editada: Image Analyst el 6 de Dic. de 2015
Use impixelinfo() to have it display the RGB values as you mouse around the image. See updated demo, attached. for the red, green, and blue threshold for the yellow pepper, try these (that I got from the Color Thresholder App):
% Define thresholds for channel 1 (Red) based on histogram settings
channel1Min = 244.000;
channel1Max = 255.000;
% Define thresholds for channel 2 (Green) based on histogram settings
channel2Min = 131.000;
channel2Max = 247.000;
% Define thresholds for channel 3 (Blue) based on histogram settings
channel3Min = 0.000;
channel3Max = 164.000;

4 comentarios

this red channel min and max value seems very strange, is it not suppose to be between 50-255?
Not sure why you said that. What demo image did you use? Did you actually try it with 50 instead of 85? It does not do a good job of finding the red pixels if you use 50. Just try it and see.
Image Analyst,
Thank you so much for your demo! It is very helpful for understanding color mapping in Matlab.
There is now a nice "Color Thresholder" app on the Apps tab of the tool ribbon that lets you interactively determine the thresholds, and then you can export the code.

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 6 de Dic. de 2015

Comentada:

el 24 de Oct. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by