I have a segmented image I, I have used labeling to find the segmented objects, and using region props function I have extracted the centroid pixel value.
My question is:
Is there a way to find the hue value of this specific pixel value in my case the centroid ?

 Respuesta aceptada

Image Analyst
Image Analyst el 1 de Nov. de 2017

0 votos

Yes
hsvValues = rgb2hsv(rgbValues);
hueValue = hsvValues(:,:,1);

5 comentarios

Tasneem Al-Tmimi
Tasneem Al-Tmimi el 1 de Nov. de 2017
can you explain more please I didn't get it
Try this:
rgbImage = imread('peppers.png');
imshow(rgbImage);
% Transform image into HSV color space.
hsvValues = rgb2hsv(rgbImage);
% Get hue at row 20, column 30
hueValue = hsvValues(20, 30,1)
Yes I have reached to this at end yesterday and it works :D
One more thing in the attached image, I have segmented the two items
For the first item (the left one) the centroid is (256, 268) and the hue value is 1 so the color is red but the second one (the right one) the centroid is (564, 261) and the hue value is 0 so the color should be red also but when I use hueValue = hsvValues(264, 261,1) an error shown that is
Error Message:
Index exceeds matrix dimensions.
I was working the whole night on this point I figured out because the value is 0 so the masked Hue image does not have this index !
is there any way to solve this part ??
this is my code for this part
hueMatrix = zeros(numberOfBlobs, 1);
for i=1:numberOfBlobs
hueMatrix(i,1) = hsvImage((xyCentroid(i,1)),(xyCentroid(i,2)),1);
Ndecimals = 2;
f = 10.^Ndecimals ;
hueMatrix = round(f*hueMatrix)/f;
end
where hsvImage is my HSV color map image, xyCentroid is the matrix contains each blobs x y centroid values and finally hueMatrix is the matrix contains the hue value for the specific pixel in our case the centroid
A Final question can I find the pixels around the centroid to find their mean Hue value ?
Thank you in advance :)
Image Analyst
Image Analyst el 2 de Nov. de 2017
Centroid will be a floating point number with fractional values, so you need to round it to the nearest integers.
Yes I know in the xyCentroid matrix I did this
xyCentroid = zeros(numberOfBlobs, 2);
for i=1:numberOfBlobs
x = 2*i-1;
y = 2*i;
xyCentroid(i,1) = floor(blobCentroid(x,1));
xyCentroid(i,2) = floor(blobCentroid(y,1));
end
But the problem is when having an image with two blobs (objects) I got this error
Error Message:
Index exceeds matrix dimensions.
That is related to the centroid pixels matrix
In the original image the hue value of the centroid pixel = 0 is this related ??
I have attached the image :)

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 4 de Nov. de 2017

0 votos

Why not just get the mean hue of the whole thing?
clc; % Clear the command window.
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 = 25;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'IMG_7379_2.jpg';
% Get the full filename, with path prepended.
folder = pwd
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
%===============================================================================
% Read in demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the original image.
subplot(1, 2, 1);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Create the region of interest (ROI)
Igray = rgb2gray(rgbImage);
binaryImage = ~imbinarize(Igray);
% Take largest 2 blobs only
binaryImage = bwareafilt(binaryImage, 2);
% Fill them to get rid of noise.
binaryImage = imfill(binaryImage, 'holes');
% Display the mask image.
subplot(1, 2, 2);
imshow(binaryImage, []);
axis on;
caption = sprintf('Binary Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;
% Convert rgb to hsv
hsvImage = rgb2hsv(rgbImage);
hueImage = hsvImage(:, :, 1);
% Compute the mean hue within the mask:
meanHue = mean(hueImage(binaryImage))
message = sprintf('The mean hue angle is %f', meanHue);
uiwait(helpdlg(message));
% Measure properties of the blobs.
% props = regionprops(binaryImage, 'Centroid');

5 comentarios

Tasneem Al-Tmimi
Tasneem Al-Tmimi el 5 de Nov. de 2017
Yes I have tried this way, but check the color is red the hue values range of red color is from 0-30 and from 330-360 the mean value is 0.18 that is out of the range so if i want to detect the color exactly it gives me other color that is Yellow
so that I have think about the hue value of centroid that 90% always correct as I tested it
But some times the centroid hue value is 0 in this case I got an error
am working on it if i got a solution I will share it :D
Thank you
Tasneem Al-Tmimi
Tasneem Al-Tmimi el 5 de Nov. de 2017
I have reached to the error exactly where ! But I can't get the solution
For the above Image I have two object each object has a centroid the first object centroid is always accessible that is (257,268) and I can get the hue value of this pixel by this line hsvImage(257,268,1) but for the second object that has a centroid at pixel (563,261) I can't access it using the same statement that is hsvImage(563,261,1) it shows an error 'Index exceeds matrix dimensions.'
I have attached my lines you can check them, I see the problem is when having two objects I guess there is a mistake in my matrix assignment BUT i can't figure it out because when working with only one object it always works otherwise there is an error
Image Analyst
Image Analyst el 5 de Nov. de 2017
Editada: Image Analyst el 5 de Nov. de 2017
I don't know what you're doing with f. What's the point of that? If you just want to round to two decimal places, use round(number, 2)..
Red is a tricky color to get the mean hue of because the hue values are like less than 0.1 and more than 0.9. So if you want the mean accurately, you'd have to convert the hues to the range -0.5 to +0.5. by subtracting 0.5 from it. For example if you have a red pixel with hue 0.2 and a purplish pixel with value 0.9, then mean would be 0.55, which is green. But if you subtracted 0.5, then you'd average -0.3 and +0.4 to get a mean of 0.05, which is red. Honestly, you'd be better off computing the mean in LAB color space, then taking the chroma value = sqrt(a^2+b^2).
I don't know why you want the hue only at the centroid. That is so variable as to make it a worthless measurement. I suggested you to use the mean hue of the entire object, but you ignored that and went back to the hue of a single point, which can vary widely depending on how the object was positioned.
Tasneem Al-Tmimi
Tasneem Al-Tmimi el 6 de Nov. de 2017
Editada: Tasneem Al-Tmimi el 6 de Nov. de 2017
The f is related to rounding number :)
Yes you got the point that am stuck on it, that sometimes when I get the mean the value detect it as a green color not red and the main objective of my code is to detect the color of my object that is Date Fruit, sometimes it is green, yellow, red, brown my problem is always related to the red color !
My question is that trick by subtracting -0.5 from the values I have ??
And in LAB color space you mean taking the mean of L ??
Image Analyst
Image Analyst el 6 de Nov. de 2017
No, the square root of a squared and b squared, like I said. LAB color space should work for any color.

Iniciar sesión para comentar.

Preguntada:

el 1 de Nov. de 2017

Comentada:

el 6 de Nov. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by