Clear Filters
Clear Filters

How to find the colour of an image at specific pixel values?

19 views (last 30 days)
I have an image with 16 region props. I have found the centroids of my desired region.
I am now required to find the colour of the image at each specific region. How would I go ahead doing this?
My centoids are (y1,x1;y2,x2;,...,y16,x16)
I am trying to identify the colour that the pixel is for each centroid. available colours are : white,blue,yellow,green and red.
Thank you

Accepted Answer

Adam Danz
Adam Danz on 14 Mar 2022
Edited: Adam Danz on 15 Mar 2022
The solution depends on the image type you're using but this demo should get you started. See in-line comments for more information.
The demo shows the normalized RGB values but if you want to classify colors by color names you'll need to add an additional step.
Pure color definitions in RGB space are
  • White [1 1 1]
  • Blue: [0 0 1]
  • Yellow [1 1 0]
  • Green [0 1 0]
  • Red [1 0 0]
But unless you're using exactly those colors you'll need a more rigorous color classification that classifies floating point RGB values into one of the color categories.
% Read and display image
I = imread('peppers.png');
imshow(I)
axis on
% Identify regions of interest
% coordinates should be the [x,y] areas of the image and in this example
% coordinates is also the image indicies. If you image specifies x and
% y values such that the indicies do not equal the x and y data, you'll
% need an additional step to get convert the (x,y) coordinates to indicies.
coordinates = [320 150; 100 230; 250 335];
hold on
plot(coordinates(:,1), coordinates(:,2), 'wo', 'LineWidth', 2)
% Get color values at specified indicies
cval = I(coordinates(:,2), coordinates(:,1), :);
RGB = [diag(cval(:,:,1)), diag(cval(:,:,2)), diag(cval(:,:,3))];
% Convert to normalized RGB values - you may not need this step if your
% image already uses normalized RGB values.
rgbNorm = double(RGB)./ double(intmax('like',RGB));
rgbStr = compose('[%.2f, %.2f, %.2f]',rgbNorm);
% Display color values next to selected areas - for demo purposes only.
text(coordinates(:,1)+10, coordinates(:,2)-10, rgbStr, ...
'Color', 'W', ...
'VerticalAlignment','baseline', ...
'BackgroundColor', 'k')
% Show the three colors
figure()
hold on
x = 1:size(rgbNorm,1);
scatter(x, 0.5, 1500, rgbNorm, 'filled')
xlim([-3,5])
axis off

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!