MATLAB Answers

How to compare two colors?

27 views (last 30 days)
J D
J D on 20 Jan 2021
Answered: Image Analyst on 21 Jan 2021
I am trying to tell whethere one color is lighter or darker to another color.
So I would like to compare an image of one color to 4 colors (red,blue, green, and yellow), have it select the color closest to it then anaylyze whether it is darker or lighter than the closest color.

  0 Comments

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 20 Jan 2021
There are several ways to do this. For example you can use statistical discriminant analysis in RGB color space, like the attached demo.
Or you can compute the delta E using deltaE():
dE = deltaE(I1,I2) calculates the color difference between two RGB images or sets of colors using the CIE76 standard.

  0 Comments

Sign in to comment.


Image Analyst
Image Analyst on 21 Jan 2021
JD, I haven't heard back from you so I assume you're having a lot of trouble. Maybe start with this simple script where I compare the L value (brightness) of a demo image to the L value of the four pure colors you listed. Please report back if you understand this simple program.
clear all;
close all;
clc;
format long g;
format compact;
fontSize = 18;
fprintf('Beginning to run %s.m ...\n', mfilename);
rgbImage = imread('peppers.png');
subplot(3, 4, 1);
imshow(rgbImage);
title('RGB image', 'FontSize', fontSize);
% Get the L (brightness) values for this RGB image.
labImage = rgb2lab(rgbImage);
lImage = labImage(:, :, 1);
subplot(3, 4, 2);
imshow(lImage, []);
title('L image', 'FontSize', fontSize);
% Get the L (brightness) values for these four primary colors.
labRed = rgb2lab([255, 0, 0])
labGreen = rgb2lab([0, 255, 0])
labBlue = rgb2lab([0, 0, 255])
labYellow = rgb2lab([255, 255, 0])
% Find where the image is brighter than pure red. You need to look at the L channel.
subplot(3, 4, 5);
imshow(lImage > labRed(1));
caption = sprintf('Pixels where image is\nbrighter than pure red.');
title(caption, 'FontSize', fontSize);
% Find where the image is brighter than pure green. You need to look at the L channel.
subplot(3, 4, 6);
imshow(lImage > labGreen(1));
caption = sprintf('Pixels where image is\nbrighter than pure green.');
title(caption, 'FontSize', fontSize);
% Find where the image is brighter than pure blue. You need to look at the L channel.
subplot(3, 4, 7);
imshow(lImage > labBlue(1));
caption = sprintf('Pixels where image is\nbrighter than pure blue.');
title(caption, 'FontSize', fontSize);
% Find where the image is brighter than pure yellow. You need to look at the L channel.
subplot(3, 4, 8);
imshow(lImage > labYellow(1));
caption = sprintf('Pixels where image is\nbrighter than pure yellow.');
title(caption, 'FontSize', fontSize);
% Find where the image is darker than pure red. You need to look at the L channel.
subplot(3, 4, 9);
imshow(lImage <= labRed(1));
caption = sprintf('Pixels where image is\ndarker than pure red.');
title(caption, 'FontSize', fontSize);
% Find where the image is darker than pure green. You need to look at the L channel.
subplot(3, 4, 10);
imshow(lImage <= labGreen(1));
caption = sprintf('Pixels where image is\ndarker than pure green.');
title(caption, 'FontSize', fontSize);
% Find where the image is darker than pure blue. You need to look at the L channel.
subplot(3, 4, 11);
imshow(lImage <= labBlue(1));
caption = sprintf('Pixels where image is\ndarker than pure blue.');
title(caption, 'FontSize', fontSize);
% Find where the image is darker than pure yellow. You need to look at the L channel.
subplot(3, 4, 12);
imshow(lImage <= labYellow(1));
caption = sprintf('Pixels where image is\ndarker than pure yellow.');
title(caption, 'FontSize', fontSize);
g = gcf;
g.WindowState = 'maximized';
fprintf('Done running %s.m.\n', mfilename);
You get:
labRed =
53.2390736041736 80.0977176619316 67.2172100598051
labGreen =
87.7340833084369 -86.1522965123016 83.1853561183118
labBlue =
32.2959590303621 79.1924278969797 -107.862641835025
labYellow =
97.138085602063 -21.5334876785774 94.4869625694341
Where the pixels are pure white is shows those pixels where the condition is true (for example pixels that are brighter than the brightness of pure red color).

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!